溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

android怎么實現控件左右或上下抖動

發布時間:2021-05-07 14:47:13 來源:億速云 閱讀:326 作者:小新 欄目:移動開發

這篇文章主要介紹android怎么實現控件左右或上下抖動,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

Android是什么

Android是一種基于Linux內核的自由及開放源代碼的操作系統,主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯盟領導及開發。

1、首先在你的res目錄下新建anim子目錄,并在anim目錄下新建兩個文件:

(1)shake.xml文件(位移/平移:translate),設置起始的位移范圍、效果時間、循環次數

<?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軸起點抖動位置
    toXDelta:x軸終點抖動位置
    duration:循環播放的時間
    interpolator:循環不放設置(次數)
  -->
</translate>

(2)cycle.xml文件,控制循環次數

<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
  android:cycles="2"><!--. 循環次數 -->

</cycleInterpolator><!--. 循環播放 -->

最后給你的控件設置改動畫屬性

Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
ivShake.startAnimation(shake);

這里ivShake是ImageView,就是這么簡單。

2、個人碰到一個問題就是在Activity實現監聽中添加動畫效果第一次沒有反應,不知道為什么

補充知識:Android 抖動提示動畫

左右抖動

ObjectAnimator animator = ObjectAnimator.ofFloat(textView, "translationX", 0, 100, -100,0);
animator.setDuration(200);
animator.start();

重復左右抖動

Animation translateAnimation = new TranslateAnimation(-20, 20, 0, 0);
translateAnimation.setDuration(100);//每次時間
translateAnimation.setRepeatCount(10);//重復次數
/**倒序重復REVERSE 正序重復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);//每次時間
    translateAnimation.setRepeatCount(10);//重復次數
/**倒序重復REVERSE 正序重復RESTART**/
    translateAnimation.setRepeatMode(Animation.REVERSE);
    view.startAnimation(translateAnimation);
  }

左右上下抖動

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怎么實現控件左右或上下抖動”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女