android 實(shí)現(xiàn)控件左右或上下抖動(dòng)教程
差不多一年前在自己的項(xiàng)目中用過這效果,雖然很簡單,但還是寫寫。
1、首先在你的res目錄下新建anim子目錄,并在anim目錄下新建兩個(gè)文件:
(1)shake.xml文件(位移/平移:translate),設(shè)置起始的位移范圍、效果時(shí)間、循環(huán)次數(shù)
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="10" android:duration="500" android:interpolator="@anim/share_cycle"> <!--. fromXDelta:x軸起點(diǎn)抖動(dòng)位置 toXDelta:x軸終點(diǎn)抖動(dòng)位置 duration:循環(huán)播放的時(shí)間 interpolator:循環(huán)不放設(shè)置(次數(shù)) --> </translate>
(2)cycle.xml文件,控制循環(huán)次數(shù)
<?xml version="1.0" encoding="utf-8"?> <cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="2"><!--. 循環(huán)次數(shù) --> </cycleInterpolator><!--. 循環(huán)播放 -->
最后給你的控件設(shè)置改動(dòng)畫屬性
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
ivShake.startAnimation(shake);
這里ivShake是ImageView,就是這么簡單。
2、個(gè)人碰到一個(gè)問題就是在Activity實(shí)現(xiàn)監(jiān)聽中添加動(dòng)畫效果第一次沒有反應(yīng),不知道為什么
補(bǔ)充知識(shí):Android 抖動(dòng)提示動(dòng)畫
左右抖動(dòng)
ObjectAnimator animator = ObjectAnimator.ofFloat(textView, "translationX", 0, 100, -100,0); animator.setDuration(200); animator.start();
重復(fù)左右抖動(dòng)
Animation translateAnimation = new TranslateAnimation(-20, 20, 0, 0); translateAnimation.setDuration(100);//每次時(shí)間 translateAnimation.setRepeatCount(10);//重復(fù)次數(shù) /**倒序重復(fù)REVERSE 正序重復(fù)RESTART**/ translateAnimation.setRepeatMode(Animation.REVERSE); nope.startAnimation(translateAnimation); public static void Shakeview( View view) { Animation translateAnimation = new TranslateAnimation(-10, 10, 0, 0); translateAnimation.setDuration(50);//每次時(shí)間 translateAnimation.setRepeatCount(10);//重復(fù)次數(shù) /**倒序重復(fù)REVERSE 正序重復(fù)RESTART**/ translateAnimation.setRepeatMode(Animation.REVERSE); view.startAnimation(translateAnimation); }
左右上下抖動(dòng)
ObjectAnimator animator = tada(clickMe); animator.setRepeatCount(ValueAnimator.INFINITE); animator.start(); public static ObjectAnimator tada(View view) { return tada(view, 2f); } public static ObjectAnimator tada(View view, float shakeFactor) { PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofKeyframe(View.SCALE_X, Keyframe.ofFloat(0f, 1f), Keyframe.ofFloat(.1f, .9f), Keyframe.ofFloat(.2f, .9f), Keyframe.ofFloat(.3f, 1.1f), Keyframe.ofFloat(.4f, 1.1f), Keyframe.ofFloat(.5f, 1.1f), Keyframe.ofFloat(.6f, 1.1f), Keyframe.ofFloat(.7f, 1.1f), Keyframe.ofFloat(.8f, 1.1f), Keyframe.ofFloat(.9f, 1.1f), Keyframe.ofFloat(1f, 1f) ); PropertyValuesHolder pvhScaleY = PropertyValuesHolder.ofKeyframe(View.SCALE_Y, Keyframe.ofFloat(0f, 1f), Keyframe.ofFloat(.1f, .9f), Keyframe.ofFloat(.2f, .9f), Keyframe.ofFloat(.3f, 1.1f), Keyframe.ofFloat(.4f, 1.1f), Keyframe.ofFloat(.5f, 1.1f), Keyframe.ofFloat(.6f, 1.1f), Keyframe.ofFloat(.7f, 1.1f), Keyframe.ofFloat(.8f, 1.1f), Keyframe.ofFloat(.9f, 1.1f), Keyframe.ofFloat(1f, 1f) ); PropertyValuesHolder pvhRotate = PropertyValuesHolder.ofKeyframe(View.ROTATION, Keyframe.ofFloat(0f, 0f), Keyframe.ofFloat(.1f, -3f * shakeFactor), Keyframe.ofFloat(.2f, -3f * shakeFactor), Keyframe.ofFloat(.3f, 3f * shakeFactor), Keyframe.ofFloat(.4f, -3f * shakeFactor), Keyframe.ofFloat(.5f, 3f * shakeFactor), Keyframe.ofFloat(.6f, -3f * shakeFactor), Keyframe.ofFloat(.7f, 3f * shakeFactor), Keyframe.ofFloat(.8f, -3f * shakeFactor), Keyframe.ofFloat(.9f, 3f * shakeFactor), Keyframe.ofFloat(1f, 0) ); return ObjectAnimator.ofPropertyValuesHolder(view, pvhScaleX, pvhScaleY, pvhRotate). setDuration(1000); }
以上這篇android 實(shí)現(xiàn)控件左右或上下抖動(dòng)教程就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android Camera2實(shí)現(xiàn)最簡單的預(yù)覽框顯示
這篇文章主要為大家詳細(xì)介紹了Android Camera2實(shí)現(xiàn)最簡單的預(yù)覽框顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Android 實(shí)現(xiàn)全屏和無標(biāo)題欄的顯示
本篇文章主要介紹了Android 全屏顯示和無標(biāo)題欄的方法,并附上代碼實(shí)例,和運(yùn)行效果圖,有需要的朋友可以參考下2016-07-07利用DrawerLayout和觸摸事件分發(fā)實(shí)現(xiàn)抽屜側(cè)滑效果
這篇文章主要為大家詳細(xì)介紹了利用DrawerLayout和觸摸事件分發(fā)實(shí)現(xiàn)抽屜側(cè)滑效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android?Studio實(shí)現(xiàn)簡單繪圖板
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)簡單繪圖板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Android framework ATMS啟動(dòng)流程
這篇文章主要為大家介紹了Android framework ATMS啟動(dòng)流程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Android編程實(shí)現(xiàn)橫豎屏切換時(shí)不銷毀當(dāng)前activity和鎖定屏幕的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)橫豎屏切換時(shí)不銷毀當(dāng)前activity和鎖定屏幕的方法,涉及Android屬性設(shè)置及activity操作的相關(guān)技巧,需要的朋友可以參考下2015-11-11Android progressbar實(shí)現(xiàn)帶底部指示器和文字的進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android progressbar實(shí)現(xiàn)帶底部指示器和文字的進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Android 自定View實(shí)現(xiàn)仿QQ運(yùn)動(dòng)步數(shù)圓弧及動(dòng)畫效果
這篇文章主要介紹了Android自定義view實(shí)現(xiàn)高仿QQ運(yùn)動(dòng)步數(shù)圓弧及動(dòng)畫效果的實(shí)例代碼,本文涉及到繪制圓弧需要具備的知識(shí)點(diǎn),需要的朋友可以參考下2016-10-10