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

相关推荐
风清云淡_A几秒前
【ANDROID】使用webview实现加载第三方的网页效果
android
吴声子夜歌8 分钟前
RxJava——操作符详解(四)
android·echarts·rxjava
我是阿亮啊38 分钟前
Android Handler 消息机制之 Looper 深度解析
android·loop·handler·looper
Mr YiRan40 分钟前
Android 16KB 腾讯Mars XLog适配
android
2501_9159214344 分钟前
不用 Xcode 上架 iOS,拆分流程多工具协作完成 iOS 应用的发布准备与提交流程
android·macos·ios·小程序·uni-app·iphone·xcode
子木鑫1 小时前
[SUCTF2019 & GXYCTF2019] 文件上传绕过实战:图片马 + .user.ini / .htaccess 构造 PHP 后门
android·开发语言·安全·php
一起养小猫1 小时前
Flutter for OpenHarmony 实战:打造功能完整的记账助手应用
android·前端·flutter·游戏·harmonyos
_乐无1 小时前
Unity 发布 Android 安卓端所有文件可读写
android·unity·游戏引擎
User_芊芊君子1 小时前
【LeetCode原地复写零】:双指针+逆向填充,O(n)时间O(1)空间最优解!
android·linux·leetcode
2501_944448003 小时前
Flutter for OpenHarmony衣橱管家App实战:支持我们功能实现
android·javascript·flutter