AndroidTV 获取焦点View放大效果实现方式

需求

电视开发最常见的就是view获焦后要有放大效果,让用户明显看到。这里总结两个实现方法,以后遇到其他的再补充。

方式一:ViewCompat.animate(view)

1、注册焦点变化监听

java 复制代码
mBtnFocus1.setOnFocusChangeListener(this);

2、有焦点变化的时候进行放缩

java 复制代码
@Override
public void onFocusChange(View view, boolean hasFocus) {
    switch (view.getId()) {
        case R.id.btn_focus1:
	        if (hasFocus) {
	        	 //获焦后放大1.2倍
	             ViewCompat.animate(view).scaleX(1.2f).scaleY(1.2f).translationZ(1.2f).start();
	        } else {
	        	//丢失焦点后缩回正常
	        	ViewCompat.animate(view).scaleX(1.0f).scaleY(1.0f).translationZ(1.0f).start();
	        }
    }
}

方式二:StateListAnimator

1、res文件夹下新建animator文件夹,然后新建focus_scale_anim.xml文件。

java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
	<item android:state_focused="true">
		<set>
			<objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleX"
                android:valueTo="1.2"
                android:valueType="floatType" />
           <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleY"
                android:valueTo="1.2"
                android:valueType="floatType" />
        </set>
    </item>
    <item android:state_focused="false">
		<set>
			<objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleX"
                android:valueTo="1"
                android:valueType="floatType" />
           <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleY"
                android:valueTo="1"
                android:valueType="floatType" />
        </set>
    </item>
</selector>

2、然后在xml布局文件中,把需要放缩的view加上该动画

java 复制代码
<Button
    android:id="@+id/btn_focus3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50px"
    android:stateListAnimator="@animator/focus_scale_anim"
    android:text="focus3" />

3、或者在代码中实现也可以

java 复制代码
StateListAnimator animator = AnimatorInflater.loadStateListAnimator(this, R.animator.focus_scale_anim);
mBtnFocus2.setStateListAnimator(animator);

上面分别用focus3和focus2分别用了xml和代码的方式,运行效果一致。

参考文章:

StateListAnimator的应用

相关推荐
Cao_Shixin攻城狮3 小时前
Flutter运行Android项目时显示java版本不兼容(Unsupported class file major version 65)的处理
android·java·flutter
呼啦啦呼啦啦啦啦啦啦6 小时前
利用pdfjs实现的pdf预览简单demo(包含翻页功能)
android·javascript·pdf
idjl8 小时前
Mysql测试题
android·adb
游戏开发爱好者810 小时前
iOS App 电池消耗管理与优化 提升用户体验的完整指南
android·ios·小程序·https·uni-app·iphone·webview
人生游戏牛马NPC1号11 小时前
学习 Flutter (四):玩安卓项目实战 - 中
android·学习·flutter
星辰也为你祝福h12 小时前
Android原生Dialog
android
梁同学与Android13 小时前
Android ---【CPU优化】需要优化的原因及优化的地方
android
Misha韩14 小时前
React Native 基础tabBar和自定义tabBar - bottom-tabs
android·react native
iHero14 小时前
【Nextcloud】在 Ubuntu 22.04.3 LTS 上的 Nextcloud Hub 10 (31.0.2) 后台任务cron 的优化
android·linux·ubuntu·nextcloud
yuanlaile18 小时前
Flutter Android打包学习指南
android·flutter·flutter打包·flutter android