网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

AndroidTV焦点框移动的实现方法

Tv开发,最重要的当然是焦点框的移动,有了焦点框我们才能知道当前选中的是哪一个,我们来看下效果图:

创新互联建站于2013年开始,先为南票等服务建站,南票等地企业,进行企业商务咨询服务。为南票企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

Android TV 焦点框移动的实现方法

那它是怎么实现的呢,我们一起来看下。

原理

布局上使用一个view,背景是.9图片做焦点框,选中一个控件的时候把这个view移动选中的控件的位置。怎么样,是不是很简单,行动起来。先看下布局

codeing

布局:

<?xml version="1.0" encoding="utf-8"?>

  

    

      
    

    

      
    
  


  

 

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


文章标题:AndroidTV焦点框移动的实现方法
本文地址:http://bjjierui.cn/article/iejods.html

其他资讯