Tv開發(fā),最重要的當(dāng)然是焦點(diǎn)框的移動(dòng),有了焦點(diǎn)框我們才能知道當(dāng)前選中的是哪一個(gè),我們來看下效果圖:
創(chuàng)新互聯(lián)建站于2013年開始,先為南票等服務(wù)建站,南票等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為南票企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
那它是怎么實(shí)現(xiàn)的呢,我們一起來看下。
原理
布局上使用一個(gè)view,背景是.9圖片做焦點(diǎn)框,選中一個(gè)控件的時(shí)候把這個(gè)view移動(dòng)選中的控件的位置。怎么樣,是不是很簡單,行動(dòng)起來。先看下布局
codeing
布局:
<?xml version="1.0" encoding="utf-8"?>
最底下的View就是我們要用到的焦點(diǎn)框
代碼
import android.app.Activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.FrameLayout; public class MainActivity extends Activity implements View.OnFocusChangeListener{ private String TAG ="qkmin"; private int Layout1 = R.id.id_fl; private int Layout2 = R.id.id_fl_2; private View onFousView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } private void initViews() { FrameLayout frameLayout=findViewById(Layout1); FrameLayout frameLayout2=findViewById(Layout2); onFousView = findViewById(R.id.id_focus); //設(shè)置焦點(diǎn)變化監(jiān)聽 frameLayout.setOnFocusChangeListener(this); frameLayout2.setOnFocusChangeListener(this); } @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus){ Log.i(TAG,"onFocusChange"+v.getId()); //設(shè)置焦點(diǎn)框的位置和動(dòng)畫 Tools.focusAnimator(v,onFousView); } } }
我們來看下Tools 這個(gè)類:
import android.animation.Animator; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.view.View; import android.view.ViewGroup; 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]; focusView.getLocationOnScreen(fromLocation); int fromWidth = focusView.getWidth(); int fromHeight = focusView.getHeight(); float fromX = fromLocation[0]; float fromY = fromLocation[1]; int[] toLocation = new int[2]; parentView.getLocationOnScreen(toLocation); int toWidth = parentView.getWidth() + offSetX; int toHeight = parentView.getHeight() + offSetY; float toX = toLocation[0] - offSetX / 2; float toY = toLocation[1] - offSetY / 2; if (scrollY == -1) { if (focusView.getVisibility() == View.GONE) focusView.setVisibility(View.VISIBLE); } AnimatorSet animatorSet = new AnimatorSet(); ObjectAnimator translateXAnimator = ObjectAnimator.ofFloat(focusView, "x", fromX, toX); translateXAnimator.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { if (focusView.getVisibility() == View.GONE) { focusView.setVisibility(View.VISIBLE); } } @Override public void onAnimationCancel(Animator animation) { if (focusView.getVisibility() == View.GONE) focusView.setVisibility(View.VISIBLE); } }); ObjectAnimator translateYAnimator = ObjectAnimator.ofFloat(focusView, "y", fromY, toY); ValueAnimator scaleWidthAnimator = ObjectAnimator.ofFloat(focusView, "width", fromWidth, toWidth); scaleWidthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float width = (Float) animation.getAnimatedValue(); mFocusWidth = (int) width; ViewGroup.LayoutParams layoutParams = focusView.getLayoutParams(); layoutParams.width = mFocusWidth; layoutParams.height = mFoucsHeight; focusView.setLayoutParams(layoutParams); } }); ValueAnimator scaleHeightAnimator = ObjectAnimator.ofFloat(focusView, "height", fromHeight, toHeight); scaleHeightAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float height = (Float) animation.getAnimatedValue(); mFoucsHeight = (int) height; ViewGroup.LayoutParams layoutParams = focusView.getLayoutParams(); layoutParams.width = mFocusWidth; layoutParams.height = mFoucsHeight; focusView.setLayoutParams(layoutParams); } }); animatorSet.playTogether(translateXAnimator, translateYAnimator, scaleWidthAnimator, scaleHeightAnimator); animatorSet.setDuration(150); animatorSet.start(); } }
主要方法是focusAnimator(),首先獲取focusView的寬、高,以及x ,y 坐標(biāo),在得到獲取焦點(diǎn)的view的寬、高,以及x ,y 坐標(biāo),最會(huì)設(shè)置動(dòng)畫。這樣就Ok了。
下面是項(xiàng)目地址:https://gitee.com/love_k/FocusTest.git
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。