亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

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

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

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

新乡县| 余姚市| 漳州市| 亚东县| 桃园县| 法库县| 抚远县| 凤城市| 新源县| 嘉兴市| 普定县| 武城县| 轮台县| 社旗县| 嘉峪关市| 榆树市| 浪卡子县| 林甸县| 宁阳县| 闻喜县| 江油市| 柞水县| 宝坻区| 鞍山市| 宜丰县| 高安市| 罗田县| 铜山县| 军事| 莱芜市| 天峨县| 常熟市| 习水县| 和林格尔县| 古交市| 景德镇市| 大洼县| 封丘县| 双辽市| 阆中市| 商水县|