大于二高端網(wǎng)站建設(shè)新手seo入門教程
需求
電視開發(fā)最常見的就是view獲焦后要有放大效果,讓用戶明顯看到。這里總結(jié)兩個實現(xiàn)方法,以后遇到其他的再補充。
方式一:ViewCompat.animate(view)
1、注冊焦點變化監(jiān)聽
mBtnFocus1.setOnFocusChangeListener(this);
2、有焦點變化的時候進行放縮
@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文件。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_focused="true"><set><objectAnimatorandroid:duration="@android:integer/config_shortAnimTime"android:propertyName="scaleX"android:valueTo="1.2"android:valueType="floatType" /><objectAnimatorandroid:duration="@android:integer/config_shortAnimTime"android:propertyName="scaleY"android:valueTo="1.2"android:valueType="floatType" /></set></item><item android:state_focused="false"><set><objectAnimatorandroid:duration="@android:integer/config_shortAnimTime"android:propertyName="scaleX"android:valueTo="1"android:valueType="floatType" /><objectAnimatorandroid:duration="@android:integer/config_shortAnimTime"android:propertyName="scaleY"android:valueTo="1"android:valueType="floatType" /></set></item>
</selector>
2、然后在xml布局文件中,把需要放縮的view加上該動畫
<Buttonandroid: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、或者在代碼中實現(xiàn)也可以
StateListAnimator animator = AnimatorInflater.loadStateListAnimator(this, R.animator.focus_scale_anim);
mBtnFocus2.setStateListAnimator(animator);
上面分別用focus3和focus2分別用了xml和代碼的方式,運行效果一致。
參考文章:
StateListAnimator的應(yīng)用