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

相关推荐
程序员码歌9 小时前
短思考第263天,每天复盘10分钟,胜过盲目努力一整年
android·前端·后端
安卓兼职framework应用工程师9 小时前
Android 10.0 按键智能机按键连续响两次的异常处理
android·audio·audioservice·按键音·按键声音
studyForMokey9 小时前
【Android 项目】个人学习demo随笔
android
吃喝不愁霸王餐APP开发者9 小时前
利用责任链模式解耦多平台(美团/饿了么)霸王餐接口的适配逻辑
android·责任链模式
百***78759 小时前
Step-Audio-2 轻量化接入全流程详解
android·java·gpt·php·llama
yangpipi-12 小时前
《C++并发编程实战》第5章 C++内存模型和原子操作
android·java·c++
云水木石14 小时前
Android 的下一个战场:Windows 应用与游戏?
android·windows·游戏
雨声不在14 小时前
Android文字渐变的实现
android·textview
GoldenPlayer14 小时前
KTS语法
android
GoldenPlayer14 小时前
后台服务Service销毁逻辑+单例造成的内存泄露
android