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

embedded/2024/10/11 11:20:55/

背景:

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

需要做的就是:

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

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

 imgTitle.setOnFocusChangeListener(this);实现方法处理:@Overridepublic 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"><ImageViewandroid:id="@+id/item_img_left_line"android:layout_width="15dp"android:focusable="false"android:layout_height="1dp"/><ImageViewandroid:id="@+id/item_img_top_line"android:layout_width="1dp"android:layout_height="15dp"android:focusable="false"/><RelativeLayoutandroid: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"><ImageViewandroid: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"/><TextViewandroid: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中,需要对遥控器,做特殊监听的,以下:

@Overridepublic 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://确定键enterbreak;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;}


http://www.ppmy.cn/embedded/34813.html

相关文章

文件加密软件排行榜前四名(2024年4大好用的加密软件推荐)

说到文件加密&#xff0c;想必大家都很熟悉&#xff0c;文件加密已经普遍应用&#xff0c;文件加密是一种重要的安全措施&#xff0c;可以确保数据的机密性、完整性和可用性&#xff0c;降低因数据泄露或丢失带来的风险 。 下面小编给大家分享几款常用的加密软件&#xff0c;…

Magic Studio Eraser API使用教程

AI橡皮擦 - 使用网址 Magic Studio的AI橡皮擦功能非常好用&#xff0c;能去除图片中的杂物。但是网页版只支持低分辨率下载&#xff0c;想要原图就得开会员&#xff0c;价格不菲。 不过官网其实提供了API接入方式&#xff0c;并且有100次的免费试用机会 API接入网站 在这里可…

OpenVoice——强大的语音克隆与生成技术

OpenVoice 是一款由 MyShell TTS 开发的令人惊叹的技术。它只需一小段参考发言者的音频片段&#xff0c;就能精确复制其声音&#xff0c;并能够生成多种语言的语音。 其主要功能包括准确的音色克隆&#xff0c;能够精确地克隆参考音色&#xff0c;并在多种语言和口音中生成语音…

【notes2】并发,IO,内存

文章目录 1.线程/协程/异步&#xff1a;并发对应硬件资源是cpu&#xff0c;线程是操作系统如何利用cpu资源的一种抽象2.并发&#xff1a;cpu&#xff0c;线程2.1 可见性&#xff1a;volatile2.2 原子性&#xff08;读写原子&#xff09;&#xff1a;AtomicInteger/synchronized…

doris be报错:sysctl -w vm.max_map_count=2000000

报错信息 [ERROR] 2024-05-06 16:42:18 TaskLogLogger-DORIS-DorisBE:[197] - [INFO] 2024-05-06 16:42:18 TaskLogLogger-DORIS-DorisBE:[175] - execute shell command : [bash, be/bin/start_be.sh, --daemon] [INFO] 2024-05-06 16:42:18 TaskLogLogger-DORIS-DorisBE:[1…

小土堆pytorch学习

土堆视频链接 1. 两大函数 1.1 dir函数 dir函数返回一个属性列表,其中列表的元素都是字符串格式。 对于模块对象:返回模块的所有属性(变量名和方法) 对于类对象:返回这个类的属性,以及其所有父类(包括父类的父类)的属性 对于其它对象(实例对象):返回这个实例…

2024年5月6日优雅草蜻蜓API大数据服务中心v2.0.3更新

v2.0.3更新 2024年5月6日优雅草蜻蜓API大数据服务中心v2.0.3更新-修复改版后搜索框漏掉的bug-增加搜索框 提示&#xff1a;优雅草大数据中心已经 上线137天 稳定运行 1181555 次 累积调用 目前大数据中心用户呈现增长趋势&#xff0c;目标2024年11月底突破1亿次调用&#xf…

Linux 操作系统线程

目录 一、线程 1.1线程的基本概念 1.2 线程相关的API函数 1.2.1 线程的创建 1.2.2 线程退出 1.2.3 线程等待函数 1.2.4 获取线程ID 1.2.5 线程取消 1.2.6 线程的清理函数 一、线程 1.1线程的基本概念 线程是属于进程&#xff1b;一个进程可以有多个线程&#xff…