android TV app适配遥控器思路,recycleview选中放大

背景:

1、当遥控器遥控盒子,app内是有一套机制,响应遥控器的操作;

2、要实现遥控器选中的效果,必须要设置setOnFocusChangeListener方法,另外一个就是设置view的setOnClickListener方法;设置完之后,就可以直接有遥控器选中的状态;

需要做的就是:

1、activity中,普通view的处理:

直接监听该view的"setOnFocusChangeListener"方法,如下:

复制代码
 imgTitle.setOnFocusChangeListener(this);



实现方法处理:

    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if (hasFocus) {
            //获焦后放大1.2倍
            ViewCompat.animate(view).scaleX(1.1f).scaleY(1.1f).translationZ(1.1f).start();
        } else {
            //丢失焦点后缩回正常
            ViewCompat.animate(view).scaleX(1.0f).scaleY(1.0f).translationZ(1.0f).start();
        }
    }

这样就可以响应遥控器的操作了,并且还有失去、获取焦点,view放大缩小的动画;

view的背景,都添加一个draw的xml,如下:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true">
        <shape>
            <corners android:radius="3dp" />
            <stroke android:width="3dp" android:color="@color/white" />
            <solid android:color="@color/transparent" />
            <padding android:top="3dp"
                android:bottom="3dp"
                android:left="3dp"
                android:right="3dp"/>
        </shape>
    </item>
    <item android:state_focused="false">
        <shape>
            <corners android:radius="3dp" />
            <stroke android:width="1dp" android:color="@color/transparent" />
            <solid android:color="@color/transparent" />
        </shape>
    </item>
</selector>

2、activity中,是recycleview时,应该如下处理:

先设置监听,

复制代码
        recyclerViewTop.setOnFocusChangeListener(this);

再将adapter中的item布局中,只设置一个view的focusable是true,其他的都设置成false,这样,遥控器就可以进行选择了,同时遥控器中的ok键,就是click事件;

布局如下:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="200dp">

    <ImageView
        android:id="@+id/item_img_left_line"
        android:layout_width="15dp"
        android:focusable="false"
        android:layout_height="1dp"/>
    <ImageView
        android:id="@+id/item_img_top_line"
        android:layout_width="1dp"
        android:layout_height="15dp"
        android:focusable="false"
        />

    <RelativeLayout
        android:id="@+id/item_layout_main"
        android:layout_width="wrap_content"
        android:layout_height="105dp"
        android:layout_toRightOf="@id/item_img_left_line"
        android:layout_below="@id/item_img_top_line"
        android:focusable="false"
        android:background="@color/green">
        <ImageView
            android:id="@+id/item_img_background"
            android:layout_width="match_parent"
            android:layout_height="105dp"
            android:background="@drawable/btn_blue_round_line_selector"
            android:focusable="true"
            />

        <TextView
            android:id="@+id/item_tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="@string/app_name"
            android:textSize="16sp"
            android:textColor="@color/white"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="10dp"

            android:focusable="false"
            />
    </RelativeLayout>

</RelativeLayout>

在activity中,需要对遥控器,做特殊监听的,以下:

复制代码
@Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        int keyCode = event.getKeyCode();
        int action = event.getAction();
        return handleKeyEvent(action, keyCode)||super.dispatchKeyEvent(event);
    }
    private boolean handleKeyEvent(int action, int keyCode) {
        if (action != KeyEvent.ACTION_DOWN)
            return false;
        switch (keyCode) {
            case KeyEvent.KEYCODE_ENTER:

            case KeyEvent.KEYCODE_DPAD_CENTER:
              	 //确定键enter
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
            	//向下键
                break;
            case KeyEvent.KEYCODE_DPAD_UP:
            	//向上键
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:
            	//向左键
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
            	//向右键
                break;
            default:
                break;
        }
        return false;
    }

Recycleview中的itemview,也可以设置放大缩小的动态图

相关推荐
liang_jy4 小时前
Android View Tag
android
liang_jy4 小时前
Android 架构中的统一分发与策略路由
android·架构
scan7246 小时前
长期记忆存储在数据库里
android
xingpanvip6 小时前
星盘接口开发文档:星相日历接口指南
android·开发语言·前端·css·php·lua
儿歌八万首9 小时前
Jetpack Compose 实战:实现一个动态平滑折线图
android·折线图·compose
李艺为13 小时前
Fake Device Test作假屏幕分辨率分析
android·java
zh_xuan13 小时前
github远程library仓库升级
android·github
峥嵘life13 小时前
Android蓝牙停用绝对音量原理
android
czlczl2002092515 小时前
IN和BETWEEN在索引效能的区别
android·adb
Volunteer Technology15 小时前
ES高级搜索功能
android·大数据·elasticsearch