FocusBorderView.java 7.12 KB
package com.xgimi.gimicinema.view;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import com.gimi.common.cinema.utils.ScreenUtils;
import com.xgimi.gimicinema.R;

public class FocusBorderView extends Button {

    private int moveCount;
    private View nextView;
    private Context mContext;
    private static int TRAN_DUR_ANIM = 160;

    private Drawable first_Drawable;

    public FocusBorderView(Context context) {
        super(context);
        this.mContext = context;
        initData();
    }

    public FocusBorderView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
        initData();
    }

    public FocusBorderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initData();
    }

    public void runTranslateAnimation(View toView) {
        runTranslateAnimation(toView, 1.03F, 1.05F);
    }

    private void initData() {
//        boolean sqlEdition = XgimiDevice.isBusinessDevice() && (XgimiDB.queryInt(getContext(), XgimiDB.ID_launcheredition) == 0);
//        setBackgroundResource(R.drawable.focus_bound);
        first_Drawable = getBackground();
        setFocusable(false);
        setClickable(false);

    }

    public void runTranslateAnimation(View toView, final float scaleX, final float scaleY) {
        if (toView instanceof AbsListView) {
            AbsListView absListView = (AbsListView) toView;
            absListView.setOnItemSelectedListener(new OnItemSelectedListener() {

                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    runTranslateAnimation(view, scaleX, scaleY);
                }

                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
            toView = absListView.getSelectedView();
        }

        Rect fromRect = findLocationWithView(this);
        if (toView instanceof RelativeLayout || toView instanceof MovieItem || !(toView instanceof Button)) {
            return;
        }
        if (toView == null) {
            return;
        }

        Rect toRect = findLocationWithView(toView);

        nextView = toView;

        int x = toRect.left - fromRect.left;
        int y = toRect.top - fromRect.top;

        int width = (int) (toView.getWidth() * scaleX);
        int height = (int) (toView.getHeight() * scaleY);

        int deltaX = (int) ((toView.getWidth() * Math.abs(scaleX - 1)) / 2.0f);
        int deltaY = (int) ((toView.getHeight() * Math.abs(scaleY - 1)) / 2.0f);

//        Log.e("----widthwidth-", "xxxxx" + width);
//        Log.e("----heightheight-", "xxxxx" + height);
//        Log.e("x", "" + (x - deltaX));
//        Log.e("y", "" + (y - deltaY));

        if (Math.abs(ScreenUtils.getDensityInt(getContext(), 489) - width) > 5) {
            deltaY += ScreenUtils.getDensityInt(getContext(), 15);
        }

//        if (toView.getId() == R.id.user_update_information) {
//            height = 0;
//            deltaY = 0;
//            y = ScreenUtils.getDensityInt(getContext(), 400);
//        }

        switch (toView.getId()) {
//            case R.id.settings_network:
//            case R.id.settings_about:
//                flyWhiteBorder(width + 25, height + 17, x - deltaX - 10, y - deltaY + 6);
//                break;
//            case R.id.settings_bluetooth:
//                flyWhiteBorder(width + 12, height + 42, x - deltaX - 6, y - deltaY - 6);
//                break;
//            case R.id.settings_networka:
//            case R.id.settings_xgimitv:
//                flyWhiteBorder(width + 42, height + 40, x - deltaX - 22, y - deltaY - 6);
//                break;

            default:
                flyWhiteBorder(width + getDimen(R.integer.default_width_add), height + getDimen(R.integer.default_height_add),
                        x + getDimen(R.integer.default_x_add), y + getDimen(R.integer.default_y_add));
//                flyWhiteBorder(width + 20, height + 30, x - 20, y - 20);
        }
//        flyWhiteBorder(width , height, x - deltaX , y - deltaY );
    }

    private int getDimen(int id) {
        return (int) mContext.getResources().getInteger(id);
    }

    /**
     * 白色焦点框飞动、移动、变大
     *
     * @param width  白色框的宽(非放大后的)
     * @param height 白色框的高(非放大后的)
     * @param x      x坐标偏移量,相对于初始的白色框的中心点
     * @param y      y坐标偏移量,相对于初始的白色框的中心点
     */
    @SuppressLint("NewApi")
    private void flyWhiteBorder(int width, int height, float x, float y) {
        AnimatorSet animSet = new AnimatorSet();
        animSet.play(ObjectAnimator.ofFloat(this, "x", x)).with(ObjectAnimator.ofFloat(this, "y", y))
                .with(ObjectAnimator.ofInt(this, "width", this.getWidth(), width))
                .with(ObjectAnimator.ofInt(this, "height", this.getHeight(), height));
        animSet.setInterpolator(new DecelerateInterpolator());
        animSet.addListener(flyListener);
        animSet.setDuration(TRAN_DUR_ANIM).start();
    }

    /**
     * 获取View的位置
     *
     * @param view 获取的控件
     * @return 位置
     */
    public Rect findLocationWithView(View view) {
        ViewGroup root = (ViewGroup) this.getParent();
        Rect rect = new Rect();
        root.offsetDescendantRectToMyCoords(view, rect);
        if (view instanceof EditText) {
            int[] location = new int[2];
            view.getLocationOnScreen(location);
            rect.left = location[0];
        }
        return rect;
    }

    private Animator.AnimatorListener flyListener = new Animator.AnimatorListener() {

        public void onAnimationCancel(Animator arg0) {
        }

        public void onAnimationEnd(Animator arg0) {
            moveCount++;
//            if (nextView instanceof XgimiButton || nextView instanceof XgimiEditText) {
//                setVisibility(View.INVISIBLE);
//            } else {
            setVisibility(View.VISIBLE);
//            }

//            if (nextView.getId() == R.id.user_update_information) {
//                setVisibility(View.INVISIBLE);
//            }

        }

        public void onAnimationRepeat(Animator arg0) {
        }

        public void onAnimationStart(Animator arg0) {
            if (moveCount != 0 && getVisibility() != View.VISIBLE) {
                setVisibility(View.VISIBLE);
            }
//            if (nextView.getId() == R.id.user_update_information) {
//                setVisibility(View.INVISIBLE);
//            }
        }
    };
}