Android TV 焦點(diǎn)框移動(dòng)

 Tv開發(fā),最重要的當(dāng)然是焦點(diǎn)框的移動(dòng)氛悬,有了焦點(diǎn)框我們才能知道當(dāng)前選中的是哪一個(gè),我們來看下效果圖:
焦點(diǎn)框移動(dòng)

那它是怎么實(shí)現(xiàn)的呢凄诞,我們一起來看下圆雁。

原理

布局上使用一個(gè)view,背景是.9圖片做焦點(diǎn)框帆谍,選中一個(gè)控件的時(shí)候把這個(gè)view移動(dòng)選中的控件的位置。怎么樣轴咱,是不是很簡(jiǎn)單汛蝙,行動(dòng)起來。先看下布局

codeing

布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@color/colorAccent"
   tools:context=".MainActivity">
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical">

       <FrameLayout
           android:id="@+id/id_fl"
           android:layout_width="880dp"
           android:layout_height="76dp"
           android:layout_marginLeft="208dp"
           android:layout_marginTop="9dp"
           android:focusable="true">

           <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_gravity="center_vertical"
               android:layout_marginLeft="40dp"
               android:text="第一行"
               android:textSize="28sp" />
       </FrameLayout>

       <FrameLayout
           android:id="@+id/id_fl_2"
           android:layout_width="880dp"
           android:layout_height="76dp"
           android:layout_marginLeft="208dp"
           android:layout_marginTop="9dp"
           android:focusable="true">

           <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_gravity="center_vertical"
               android:layout_marginLeft="40dp"
               android:text="第二行"
               android:textSize="28sp" />
       </FrameLayout>
   </LinearLayout>


   <View
       android:id="@+id/id_focus"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:background="@drawable/settings_selector"
       android:visibility="gone" />

</FrameLayout> 

最底下的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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市肋乍,隨后出現(xiàn)的幾起案子鹅颊,更是在濱河造成了極大的恐慌,老刑警劉巖墓造,帶你破解...
    沈念sama閱讀 219,270評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件堪伍,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡觅闽,警方通過查閱死者的電腦和手機(jī)帝雇,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蛉拙,“玉大人摊求,你說我怎么就攤上這事×趵耄” “怎么了室叉?”我有些...
    開封第一講書人閱讀 165,630評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)硫惕。 經(jīng)常有香客問我茧痕,道長(zhǎng),這世上最難降的妖魔是什么恼除? 我笑而不...
    開封第一講書人閱讀 58,906評(píng)論 1 295
  • 正文 為了忘掉前任踪旷,我火速辦了婚禮,結(jié)果婚禮上豁辉,老公的妹妹穿的比我還像新娘令野。我一直安慰自己,他們只是感情好徽级,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評(píng)論 6 392
  • 文/花漫 我一把揭開白布气破。 她就那樣靜靜地躺著,像睡著了一般餐抢。 火紅的嫁衣襯著肌膚如雪现使。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,718評(píng)論 1 305
  • 那天旷痕,我揣著相機(jī)與錄音碳锈,去河邊找鬼。 笑死欺抗,一個(gè)胖子當(dāng)著我的面吹牛售碳,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,442評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼贸人,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼间景!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起灸姊,我...
    開封第一講書人閱讀 39,345評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤拱燃,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后力惯,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體碗誉,經(jīng)...
    沈念sama閱讀 45,802評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評(píng)論 3 337
  • 正文 我和宋清朗相戀三年父晶,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了哮缺。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,117評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡甲喝,死狀恐怖尝苇,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情埠胖,我是刑警寧澤糠溜,帶...
    沈念sama閱讀 35,810評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站直撤,受9級(jí)特大地震影響非竿,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜谋竖,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評(píng)論 3 331
  • 文/蒙蒙 一红柱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蓖乘,春花似錦锤悄、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至众眨,卻和暖如春握牧,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背娩梨。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留览徒,地道東北人狈定。 一個(gè)月前我還...
    沈念sama閱讀 48,377評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親纽什。 傳聞我的和親對(duì)象是個(gè)殘疾皇子措嵌,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容