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的应用

相关推荐
深盾安全11 分钟前
Kotlin Data Classes 快速上手
android
一枚小小程序员哈1 小时前
安卓\android程序开发之基于 Android 的校园报修系统的设计与实现
android
用户2018792831672 小时前
🌟 一场失败的加密舞会:SSL握手失败的奇幻冒险
android
tangweiguo030519873 小时前
面向对象编程三剑客:Dart、Java 和 Kotlin 的核心区别
android·flutter·kotlin
幼稚园的山代王4 小时前
Kotlin数据类型
android·开发语言·kotlin
xixixin_4 小时前
【H5】禁止IOS、安卓端长按的一些默认操作
android·css·ios·h5
zhangphil4 小时前
Android实现Glide/Coil样式图/视频加载框架,Kotlin
android·kotlin
用户2018792831674 小时前
“记忆邮局” (LiveData)
android
叽哥4 小时前
flutter学习第 17 节:项目实战:综合应用开发(下)
android·flutter·ios
小林up5 小时前
HiSmartPerf使用WIFI方式连接Android机显示当前设备0.0.0.0无法ping通!设备和电脑连接同一网络,将设备保持亮屏重新尝试
android·网络·电脑