2023年7月15日发(作者:)
AndroidTV焦点框移动的实现⽅法Tv开发,最重要的当然是焦点框的移动,有了焦点框我们才能知道当前选中的是哪⼀个,我们来看下效果图:那它是怎么实现的呢,我们⼀起来看下。原理布局上使⽤⼀个view,背景是.9图⽚做焦点框,选中⼀个控件的时候把这个view移动选中的控件的位置。怎么样,是不是很简单,⾏动起来。先看下布局codeing布局: 最底下的View就是我们要⽤到的焦点框代码import ty;import patActivity;import ;import ;import ;import ayout;public class MainActivity extends Activity implements sChangeListener{ private String TAG ="qkmin"; private int Layout1 = _fl; private int Layout2 = _fl_2; private View onFousView; @Override protected void onCreate(Bundle savedInstanceState) { te(savedInstanceState); setContentView(ty_main); initViews(); } private void initViews() { FrameLayout frameLayout=findViewById(Layout1); FrameLayout frameLayout2=findViewById(Layout2); onFousView = findViewById(_focus); //设置焦点变化监听 ocusChangeListener(this); ocusChangeListener(this); } @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus){ Log.i(TAG,"onFocusChange"+()); //设置焦点框的位置和动画 nimator(v,onFousView); } }}我们来看下Tools 这个类:import or;import orSet;import Animator;import nimator;import ;import oup;public class Tools { private static int mFocusWidth; private static int mFoucsHeight; public static void focusAnimator(View v, View onFousView) { focusAnimator(v, onFousView, -1, 0, 0); } public static void focusAnimator(View parentView, final View focusView, int scrollY, int offSetX, int offSetY) { int[] fromLocation = new int[2]; ationOnScreen(fromLocation); int fromWidth = th(); int fromHeight = ght(); float fromX = fromLocation[0]; float fromY = fromLocation[1]; int[] toLocation = new int[2]; ationOnScreen(toLocation); int toWidth = th() + offSetX; int toHeight = ght() + offSetY; float toX = toLocation[0] - offSetX / 2; float toY = toLocation[1] - offSetY / 2; if (scrollY == -1) { if (ibility() == ) ibility(E); } AnimatorSet animatorSet = new AnimatorSet(); ObjectAnimator translateXAnimator = t(focusView, "x", fromX, toX); tener(new orListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { if (ibility() == ) { ibility(E); } } @Override public void onAnimationCancel(Animator animation) { if (ibility() == ) ibility(E); } }); ObjectAnimator translateYAnimator = t(focusView, "y", fromY, toY); ValueAnimator scaleWidthAnimator = t(focusView, "width", fromWidth, toWidth); ateListener(new orUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float width = (Float) matedValue(); mFocusWidth = (int) width; Params layoutParams = outParams(); = mFocusWidth; = mFoucsHeight; outParams(layoutParams); } }); ValueAnimator scaleHeightAnimator = t(focusView, "height", fromHeight, toHeight); ateListener(new orUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float height = (Float) matedValue(); mFoucsHeight = (int) height; Params layoutParams = outParams(); = mFocusWidth; = mFoucsHeight; outParams(layoutParams); } }); gether(translateXAnimator, translateYAnimator, scaleWidthAnimator, scaleHeightAnimator); ation(150); (); }}主要⽅法是focusAnimator(),⾸先获取focusView的宽、⾼,以及x ,y 坐标,在得到获取焦点的view的宽、⾼,以及x ,y 坐标,最会设置动画。这样就Ok了。以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
发布者:admin,转转请注明出处:http://www.yc00.com/news/1689431199a247167.html
评论列表(0条)