中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android TV 焦點框移動的實現方法

發布時間:2020-10-15 17:16:04 來源:腳本之家 閱讀:169 作者:i小康 欄目:移動開發

Tv開發,最重要的當然是焦點框的移動,有了焦點框我們才能知道當前選中的是哪一個,我們來看下效果圖:

Android TV 焦點框移動的實現方法

那它是怎么實現的呢,我們一起來看下。

原理

布局上使用一個view,背景是.9圖片做焦點框,選中一個控件的時候把這個view移動選中的控件的位置。怎么樣,是不是很簡單,行動起來。先看下布局

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就是我們要用到的焦點框

代碼

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);
    //設置焦點變化監聽
    frameLayout.setOnFocusChangeListener(this);
    frameLayout2.setOnFocusChangeListener(this);
  }

  @Override
  public void onFocusChange(View v, boolean hasFocus) {
    if (hasFocus){
      Log.i(TAG,"onFocusChange"+v.getId());
      //設置焦點框的位置和動畫
      Tools.focusAnimator(v,onFousView);
    }
  }
}

我們來看下Tools 這個類:

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 坐標,在得到獲取焦點的view的寬、高,以及x ,y 坐標,最會設置動畫。這樣就Ok了。

下面是項目地址:https://gitee.com/love_k/FocusTest.git

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

皋兰县| 阿瓦提县| 呼伦贝尔市| 巴南区| 稷山县| 饶河县| 中阳县| 家居| 巴彦淖尔市| 昌平区| 盐边县| 浠水县| 常州市| 乳源| 银川市| 信阳市| 鄂托克前旗| 习水县| 辽宁省| 雷州市| 和田市| 巩义市| 洪雅县| 佛坪县| 凉城县| 兴和县| 平顶山市| 梧州市| 贵阳市| 博乐市| 中卫市| 清原| 西乌珠穆沁旗| 太湖县| 浮梁县| 平潭县| 讷河市| 呼图壁县| 岚皋县| 岳阳市| 株洲市|