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,也可以设置放大缩小的动态图

相关推荐
lxysbly10 分钟前
安卓PCE模拟器下载指南:用手机玩 PC-Engine / TurboGrafx 经典
android·智能手机
Digitally2 小时前
哪款应用最适合将数据从安卓手机传输到 iPhone?
android·智能手机·iphone
Java小白,一起学习3 小时前
新版onenet云平台数据流对接,包括设备端MQTT和应用端API
android·物联网
走在路上的菜鸟3 小时前
Android学Flutter学习笔记 第一节 Android视角认知Flutter(View,intent,Async UI)
android·学习·flutter
一起搞IT吧4 小时前
相机Camera日志实例分析之十二:相机Camx【萌拍后置zoom拍照】单帧流程日志详解
android·c++·数码相机·智能手机
冬奇Lab4 小时前
一次 Android 车机黑屏问题的深度剖析:当显示驱动遇上中断风暴
android·性能优化·debug
兮动人4 小时前
Fatal error: Uncaught think\exception\ErrorException: SourceGuardian Loade
android·php
笔夏4 小时前
【安卓学习之myt】adb常用命令
android·学习·adb
lxysbly4 小时前
安卓gba模拟器下载
android
bst@微胖子5 小时前
CrewAI+FastAPI实现多Agent协作完成软件编码项目
android·fastapi