VideoFocusBorderView.java 4.53 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.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnHoverListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import com.xgimi.gimicinema.R;

public class VideoFocusBorderView extends Button implements OnHoverListener{
	
	private String TAG = "FocusBorderView";
	private Drawable first_Drawable;
	private View nextView;
	
	private static int TRAN_DUR_ANIM = 0;//160;

	public VideoFocusBorderView(Context context) {
		super(context);
		loseFocuse();
	}

	public VideoFocusBorderView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		loseFocuse();
	}

	public VideoFocusBorderView(Context context, AttributeSet attrs) {
		super(context, attrs);
		loseFocuse();
	}

	private void loseFocuse() {
		setBorderViewBackground();
		setFocusable(false);
		setClickable(false);
		setFocusableInTouchMode(false);
	}

	public void runTranslateAnimation(View toView) {
		runTranslateAnimation(toView, 1.0F, 1.0F);
	}
	
	public void runTranslateAnimation(View toView, View fromView) {
		
		Rect fromRect = findLocationWithView(fromView);
		
		if (toView == null)
			return;
		Rect toRect = findLocationWithView(toView);
		int x = toRect.left - fromRect.left;
		int y = toRect.top - fromRect.top;
		int width = toView.getWidth();
		int height = toView.getWidth();
		flyWhiteBorder(width, height, x , y );
	}
	
	/**
	 * 重新设置borderView 的颜色
	 */
	public void setBorderViewBackground(){
		setBackgroundResource(R.drawable.focus_bound);
//		setBackgroundResource(R.drawable.ic_dark_shadow);
		first_Drawable = getBackground();
	}
	

	public void runTranslateAnimation(View toView, float scaleX, float scaleY) {
		if (toView == null){
			Log.e(TAG,"toView =================================null");
			setBackgroundColor(Color.TRANSPARENT);
			return;
		}
		if (!(toView instanceof HomeItemContainer)) {
			return;
		}
		setBackground(first_Drawable);
		Rect fromRect = findLocationWithView(this);	
		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);

		flyWhiteBorder(width, height, x - deltaX - 16, y - deltaY);
	}

	/**
	 * 白色焦点框飞动、移动、变大
	 * 
	 * @param width
	 *            白色框的宽(非放大后的)
	 * @param height
	 *            白色框的高(非放大后的)
	 * @param width
	 *            x坐标偏移量,相对于初始的白色框的中心点
	 * @param height
	 *            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.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) {
			setVisibility(View.VISIBLE);
		}

		public void onAnimationRepeat(Animator arg0) {
		}

		public void onAnimationStart(Animator arg0) {
		}
	};

	@Override
	public boolean onHover(View v, MotionEvent event) {
		// Auto-generated method stub
		this.setVisibility(View.INVISIBLE);
		return false;
	}
}