Android Open Source - sony-camera-remote-java Radial Selector






From Project

Back to project page sony-camera-remote-java.

License

The source code is released under:

Apache License

If you think the Android project sony-camera-remote-java listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.praetoriandroid.widget;
/*from  www. j ava  2 s . c om*/
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageButton;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Blah-blah-blah...
 *
 * @param <Value> item data value type.
 */
public abstract class RadialSelector<ItemView extends View, Value>
        extends ImageButton
        implements ItemViewCreator<ItemView, Value> {
    public interface OnValueSelectedListener<Value> {
        public void onValueSelected(Value value);
    }

    private static final int DEFAULT_COVER_COLOR = 0x80000000;
    private static final float DEFAULT_DISTANCE_MULTIPLIER = 1.5f;
    private static final float DEFAULT_ARC_SPACE_MULTIPLIER = 1.1f;

    private OnValueSelectedListener<Value> onValueSelectedListener;
    private List<Value> itemValues = Collections.emptyList();

    public RadialSelector(Context context) {
        super(context);
        init();
    }

    public RadialSelector(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RadialSelector(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        super.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showItems(itemValues);
            }
        });
    }

    public void setItemValues(List<Value> itemValues) {
        this.itemValues = itemValues;
    }

    public void setOnValueSelectedListener(OnValueSelectedListener<Value> listener) {
        onValueSelectedListener = listener;
    }

    private void showItems(List<Value> values) {
        final ViewGroup parent = (ViewGroup) getParent();
        if (parent == null) {
            throw new NullPointerException();
        }

        int size = Math.max(getWidth(), getHeight());
        int selectorX = getLeft() + getWidth() / 2;
        int selectorY = getTop() + getHeight() / 2;

        final FrameLayout cover = new FrameLayout(getContext());
        cover.setBackgroundColor(DEFAULT_COVER_COLOR);
        cover.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                parent.removeView(cover);
            }
        });
        parent.addView(cover, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        int itemsNumber = values.size();
        List<ItemView> itemViews = new ArrayList<ItemView>(itemsNumber);
        for (final Value value : values) {
            ItemView itemView = createItemView(parent, value);
            itemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    parent.removeView(cover);
                    if (onValueSelectedListener != null) {
                        onValueSelectedListener.onValueSelected(value);
                    }
                }
            });
            cover.addView(itemView);
            itemViews.add(itemView);
        }

        Log.d("@@@@@", "size: " + size);
        int parentWidth = parent.getWidth();
        int parentHeight = parent.getHeight();
        RadialFreeSpace freeSpace = RadialFreeSpace
                .fromCoordinates(parentWidth, parentHeight, selectorX, selectorY, size);
        Log.d("@@@@@", "freeSpace: " + freeSpace);
        float distance = size * DEFAULT_DISTANCE_MULTIPLIER;
        int line = 1;
        for (int i = 0; i < itemsNumber; distance += size * DEFAULT_DISTANCE_MULTIPLIER, line++) {
            float arcLength = freeSpace.sectorSize() * distance; //(float) (distance * freeSpace.sectorSize() / (2 * Math.PI));
            Log.d("@@@@@", "distance: " + distance + ", sector size: " + freeSpace.sectorSize() + ", arc length: " + arcLength);
            int maxRays = (int) (arcLength / size / DEFAULT_ARC_SPACE_MULTIPLIER);
            Log.d("@@@@@", line + ": max rays: " + maxRays + ", itemsNumber: " + itemsNumber);

            int rowItemsNumber = (itemsNumber - i) >= maxRays ? maxRays : itemsNumber % maxRays;
            Log.d("@@@@@", "rowItemsNumber: " + rowItemsNumber);
            float step = rowItemsNumber == 1 ? 0 : freeSpace.sectorSize() / (rowItemsNumber - 1);
            Log.d("@@@@@", "step: " + step + ", MAX_RAYS_ANGLE: " + RadialFreeSpace.MAX_RAYS_ANGLE);
            if (step > RadialFreeSpace.MAX_RAYS_ANGLE) {
                step = RadialFreeSpace.MAX_RAYS_ANGLE;
            }
            Log.d("@@@@@", "step after correction: " + step);
            float angle = freeSpace.mainAxis() - step * (rowItemsNumber - 1) / 2;
            Log.d("@@@@@", "angle: " + angle + ", main axis: " + freeSpace.mainAxis());
            for (int ray = 0; ray < maxRays && i < itemsNumber; ray++, i++, angle += step) {
                Log.d("@@@@@", "i: " + i + ", angle: " + angle);
                int x = (int) Math.round(selectorX + distance * Math.cos(angle));
                int y = (int) Math.round(selectorY + distance * Math.sin(angle));
                setViewPosition(itemViews.get(i), x, y);
            }
        }
    }

    private void setViewPosition(View view, int cX, int cY) {
        view.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int w = view.getMeasuredWidth();
        int h = view.getMeasuredHeight();
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(cX - w / 2, cY - h / 2, 0, 0);
        view.setLayoutParams(lp);
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        throw new RuntimeException("Do not use setOnClickListener() with this class! See class JavaDoc for more details.");
    }
}




Java Source Code List

com.praetoriandroid.cameraremote.BadHttpStatusException.java
com.praetoriandroid.cameraremote.DeviceDescription.java
com.praetoriandroid.cameraremote.HttpClient.java
com.praetoriandroid.cameraremote.InvalidDataFormatException.java
com.praetoriandroid.cameraremote.LiveViewDisconnectedException.java
com.praetoriandroid.cameraremote.LiveViewFetcher.java
com.praetoriandroid.cameraremote.Logger.java
com.praetoriandroid.cameraremote.ParseException.java
com.praetoriandroid.cameraremote.RpcClient.java
com.praetoriandroid.cameraremote.RpcException.java
com.praetoriandroid.cameraremote.ServiceNotSupportedException.java
com.praetoriandroid.cameraremote.SsdpClient.java
com.praetoriandroid.cameraremote.SsdpException.java
com.praetoriandroid.cameraremote.app.LiveView.java
com.praetoriandroid.cameraremote.app.MainActivity.java
com.praetoriandroid.cameraremote.app.Rpc.java
com.praetoriandroid.cameraremote.app.SelfTimerSelector.java
com.praetoriandroid.cameraremote.app.ThisApp.java
com.praetoriandroid.cameraremote.rpc.ActTakePictureRequest.java
com.praetoriandroid.cameraremote.rpc.ActTakePictureResponse.java
com.praetoriandroid.cameraremote.rpc.AwaitTakePictureRequest.java
com.praetoriandroid.cameraremote.rpc.BaseRequest.java
com.praetoriandroid.cameraremote.rpc.BaseResponse.java
com.praetoriandroid.cameraremote.rpc.CustomizableEntity.java
com.praetoriandroid.cameraremote.rpc.EventEntity.java
com.praetoriandroid.cameraremote.rpc.GetApplicationInfoRequest.java
com.praetoriandroid.cameraremote.rpc.GetApplicationInfoResponse.java
com.praetoriandroid.cameraremote.rpc.GetAvailableApiListRequest.java
com.praetoriandroid.cameraremote.rpc.GetAvailableApiListResponse.java
com.praetoriandroid.cameraremote.rpc.GetAvailableSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.GetAvailableSelfTimerResponse.java
com.praetoriandroid.cameraremote.rpc.GetEventRequest.java
com.praetoriandroid.cameraremote.rpc.GetEventResponse.java
com.praetoriandroid.cameraremote.rpc.GetExposureModeRequest.java
com.praetoriandroid.cameraremote.rpc.GetMethodTypesRequest.java
com.praetoriandroid.cameraremote.rpc.GetMethodTypesResponse.java
com.praetoriandroid.cameraremote.rpc.GetSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.GetSupportedSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.GetSupportedSelfTimerResponse.java
com.praetoriandroid.cameraremote.rpc.GetVersionsRequest.java
com.praetoriandroid.cameraremote.rpc.GetVersionsResponse.java
com.praetoriandroid.cameraremote.rpc.IllegalResultSizeException.java
com.praetoriandroid.cameraremote.rpc.MethodTypesEntity.java
com.praetoriandroid.cameraremote.rpc.RpcMethod.java
com.praetoriandroid.cameraremote.rpc.SetSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.SimpleResponse.java
com.praetoriandroid.cameraremote.rpc.StartLiveViewRequest.java
com.praetoriandroid.cameraremote.rpc.StartLiveViewResponse.java
com.praetoriandroid.cameraremote.rpc.StartMovieRecRequest.java
com.praetoriandroid.cameraremote.rpc.StartRecModeRequest.java
com.praetoriandroid.cameraremote.rpc.StopLiveviewRequest.java
com.praetoriandroid.cameraremote.rpc.StopMovieRecRequest.java
com.praetoriandroid.cameraremote.rpc.StopMovieRecResponse.java
com.praetoriandroid.cameraremote.rpc.StopRecModeRequest.java
com.praetoriandroid.cameraremote.rpc.ValidationException.java
com.praetoriandroid.cameraremote.rpc._DummyResponse.java
com.praetoriandroid.cameraremote.tool.Cache.java
com.praetoriandroid.cameraremote.tool.RemoteControl.java
com.praetoriandroid.widget.ItemViewCreator.java
com.praetoriandroid.widget.RadialFreeSpace.java
com.praetoriandroid.widget.RadialSelector.java