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

相关推荐
xiangzhihong82 小时前
使用Universal Links与Android App Links实现网页无缝跳转至应用
android·ios
车载应用猿2 小时前
基于Android14的CarService 启动流程分析
android
没有了遇见3 小时前
Android 渐变色实现总结
android
雨白6 小时前
Jetpack系列(四):精通WorkManager,让后台任务不再失控
android·android jetpack
mmoyula8 小时前
【RK3568 驱动开发:实现一个最基础的网络设备】
android·linux·驱动开发
sam.li8 小时前
WebView安全实现(一)
android·安全·webview
移动开发者1号9 小时前
Kotlin协程超时控制:深入理解withTimeout与withTimeoutOrNull
android·kotlin
程序员JerrySUN9 小时前
RK3588 Android SDK 实战全解析 —— 架构、原理与开发关键点
android·架构
移动开发者1号10 小时前
Java Phaser:分阶段任务控制的终极武器
android·kotlin
哲科软件18 小时前
跨平台开发的抉择:Flutter vs 原生安卓(Kotlin)的优劣对比与选型建议
android·flutter·kotlin