电视开发,最重要的当然是焦点框的移动,有了焦点框我们才能知道当前选中的是哪一个,我们来看下效果图:
那它是怎么实现的呢,我们一起来看下。
布局上使用一个视图中,背景是。9图片做焦点框,选中一个控件的时候把这个视图移动选中的控件的位置。怎么样,是不是很简单,行动起来。先看下布局
<强>代码
强>
<强>布局:强>
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt; FrameLayout xmlns: android=" http://schemas.android.com/apk/res/android " xmlns:工具=" http://schemas.android.com/tools " android: layout_width=" match_parent " android: layout_height=" match_parent " android:背景=" @color/colorAccent” 工具:上下文=?MainActivity”比; LinearLayout & lt; android: layout_width=" match_parent " android: layout_height=" wrap_content " 面向android:=按怪薄北? & lt; FrameLayout android: id=癅 + id/id_fl” android: layout_width=" 880 dp” android: layout_height=" 76 dp” android: layout_marginLeft=" 208 dp” android: layout_marginTop=" 9 dp " android: focusable=" true "比; & lt; TextView android: layout_width=" wrap_content " android: layout_height=" wrap_content " android: layout_gravity=" center_vertical " android: layout_marginLeft=" 40 dp " android:文本="第一行” android: textSize=" 28 sp "/比; & lt;/FrameLayout> & lt; FrameLayout android: id=癅 + id/id_fl_2” android: layout_width=" 880 dp” android: layout_height=" 76 dp” android: layout_marginLeft=" 208 dp” android: layout_marginTop=" 9 dp " android: focusable=" true "比; & lt; TextView android: layout_width=" wrap_content " android: layout_height=" wrap_content " android: layout_gravity=" center_vertical " android: layout_marginLeft=" 40 dp " android:文本="第二行” android: textSize=" 28 sp "/比; & lt;/FrameLayout> & lt;/LinearLayout> & lt;视图 android: id=癅 + id/id_focus” android: layout_width=" wrap_content " android: layout_height=" wrap_content " android:背景=" @drawable/settings_selector” android:可见性="了"/比; & lt;/FrameLayout> >之前最底下的视图就是我们要用到的焦点框
代码
进口android.app.Activity; 进口android.support.v7.app.AppCompatActivity; 进口android.os.Bundle; 进口android.util.Log; 进口android.view.View; 进口android.widget.FrameLayout; 公共类MainActivity扩展活动实现View.OnFocusChangeListener { 私人字符串标签=皅kmin”; 私人int Layout1=R.id.id_fl; 私人int Layout2=R.id.id_fl_2; 私有视图> 进口android.animation.Animator; 进口android.animation.AnimatorSet; 进口android.animation.ObjectAnimator; 进口android.animation.ValueAnimator; 进口android.view.View; 进口android.view.ViewGroup; 公共类工具{ 私有静态int mFocusWidth; 私有静态int mFoucsHeight; 公共静态孔隙focusAnimator (v视图,视图onFousView) { onFousView focusAnimator (v, 1, 0, 0); } parentView公共静态孔隙focusAnimator(视图,最后查看focusView, int scrollY, int offSetX, int offSetY) { int [] fromLocation=new int [2]; focusView.getLocationOnScreen (fromLocation); int fromWidth=focusView.getWidth (); int fromHeight=focusView.getHeight (); 浮动fromX=fromLocation [0]; 浮动fromY=fromLocation [1]; int [] toLocation=new int [2]; parentView.getLocationOnScreen (toLocation); int toWidth=parentView.getWidth () + offSetX; int toHeight=parentView.getHeight () + offSetY; 浮托克斯=toLocation [0] - offSetX/2; 漂浮玩具=toLocation [1] - offSetY/2; 如果(scrollY==1) { 如果(focusView.getVisibility ()==View.GONE) focusView.setVisibility (View.VISIBLE); } AnimatorSet AnimatorSet=new AnimatorSet (); ObjectAnimator translateXAnimator=ObjectAnimator。fromX ofFloat (focusView,“x”,托克斯); translateXAnimator。addListener(新Animator.AnimatorListener () { @Override 公共空间onAnimationStart(动画动画){ } @Override 公共空间onAnimationRepeat(动画动画){ } @Override 公共空间onAnimationEnd(动画动画){ 如果(focusView.getVisibility ()==View.GONE) { focusView.setVisibility (View.VISIBLE); } } @Override 公共空间onAnimationCancel(动画动画){ 如果(focusView.getVisibility ()==View.GONE) focusView.setVisibility (View.VISIBLE); } }); ObjectAnimator translateYAnimator=ObjectAnimator。fromY ofFloat (focusView“y”,玩具); ValueAnimator scaleWidthAnimator=ObjectAnimator。ofFloat (focusView“宽度”,fromWidth toWidth); scaleWidthAnimator。addUpdateListener(新ValueAnimator.AnimatorUpdateListener () { @Override 公共空间onAnimationUpdate (ValueAnimator动画){ 浮动宽度=(浮动)animation.getAnimatedValue (); mFocusWidth=(int)宽度; ViewGroup。LayoutParams LayoutParams=focusView.getLayoutParams (); layoutParams。宽度=mFocusWidth; layoutParams。身高=mFoucsHeight; focusView.setLayoutParams (layoutParams); } }); ValueAnimator scaleHeightAnimator=ObjectAnimator。ofFloat (focusView,“高度”,fromHeight toHeight); scaleHeightAnimator。addUpdateListener(新ValueAnimator.AnimatorUpdateListener () { @Override 公共空间onAnimationUpdate (ValueAnimator动画){ 浮子高度=(浮动)animation.getAnimatedValue (); mFoucsHeight=(int)高度; ViewGroup。LayoutParams LayoutParams=focusView.getLayoutParams (); layoutParams。宽度=mFocusWidth; layoutParams。null null null null null null null null null null null null null null null null nullAndroid电视焦点框移动的实现方法