Android自定義Animation實現(xiàn)View搖擺效果
使用自定義Animation,實現(xiàn)View的左右搖擺效果,如圖所示:

代碼很簡單,直接上源碼
activity_maini.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:gravity="center" android:orientation="vertical"> <!--圖片--> <ImageView android:id="@+id/iv_dial" android:layout_width="200dp" android:layout_height="200dp" android:src="@drawable/img"/> <!--控制按鈕--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:gravity="center" android:orientation="horizontal"> <Button android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="開始"/> <Button android:id="@+id/btn_end" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="結(jié)束"/> </LinearLayout> </LinearLayout>
也可以用其它的View控件替代ImageView,都是可以實現(xiàn)搖擺效果的
主界面MainActivity
/**
* 主界面
* Created by zhuwentao on 2016-08-08.
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
/** 表盤圖片 */
private ImageView mDialIv;
/** 開始按鈕 */
private Button mStartBtn;
/** 結(jié)束按鈕 */
private Button mEndBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
initListener();
}
/**
* 初始化UI
*/
private void initUI() {
mDialIv = (ImageView) findViewById(R.id.iv_dial);
mStartBtn = (Button) findViewById(R.id.btn_start);
mEndBtn = (Button) findViewById(R.id.btn_end);
}
/**
* 初始化監(jiān)聽
*/
private void initListener() {
mStartBtn.setOnClickListener(this);
mEndBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_start:
showAnimation();
break;
case R.id.btn_end:
mDialIv.clearAnimation();
break;
}
}
/**
* 設(shè)置動畫
*/
private void showAnimation() {
// 獲取自定義動畫實例
CustomRotateAnim rotateAnim = CustomRotateAnim.getCustomRotateAnim();
// 一次動畫執(zhí)行1秒
rotateAnim.setDuration(1000);
// 設(shè)置為循環(huán)播放
rotateAnim.setRepeatCount(-1);
// 設(shè)置為勻速
rotateAnim.setInterpolator(new LinearInterpolator());
// 開始播放動畫
mDialIv.startAnimation(rotateAnim);
}
}
setRepeatCount()設(shè)置的是重復(fù)播放動畫的次數(shù),-1是為了讓它循環(huán)播放,setRepeatCount(0)代表的是執(zhí)行一次,setRepeatCount(1)代表重復(fù)1次,即動畫執(zhí)行2次。
setInterpolator()方法是設(shè)置插值器,用來指定動畫的效果,這里使用系統(tǒng)提供的LinearInterpolator()勻速變化效果。
自定義的CustomRotateAnim動畫需要繼承Animation,這里只要實現(xiàn)它的initialize()和applyTransformation()方法就好
/**
* 左右搖擺動畫
* Created by zhuwentao on 2016-08-08.
*/
public class CustomRotateAnim extends Animation {
/** 控件寬 */
private int mWidth;
/** 控件高 */
private int mHeight;
/** 實例 */
private static CustomRotateAnim rotateAnim;
/**
* 獲取動畫實例
* @return 實例
*/
public static CustomRotateAnim getCustomRotateAnim() {
if (null == rotateAnim) {
rotateAnim = new CustomRotateAnim();
}
return rotateAnim;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
this.mWidth = width;
this.mHeight = height;
super.initialize(width, height, parentWidth, parentHeight);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
// 左右搖擺
t.getMatrix().setRotate((float)(Math.sin(interpolatedTime*Math.PI*2)*50), mWidth/2, mHeight/2);
super.applyTransformation(interpolatedTime, t);
}
}
initialize(int width, int height, int parentWidth, int parentHeight)中,width和height代表指定播放動畫的View空間寬高,parentWidth和parentHeight代表該View控件所在的父控件寬高。
我們需要使用當前View的寬高來確定搖擺的旋轉(zhuǎn)點,所以在initialize中獲取View控件的寬高。
applyTransformation()方法是動畫具體的實現(xiàn)方法,在系統(tǒng)繪制動畫時會反復(fù)調(diào)用這個方法,每調(diào)用一次applyTransformation()方法,其中的interpolatedTime參數(shù)都會改變一次,值從0到1遞增,當interpolatedTime的值為1時則動畫結(jié)束。
Transformatio類是一個變換的矩陣,通過改變該矩陣就可以實現(xiàn)各種復(fù)雜的效果。
復(fù)寫這個方法,在里面就可以實現(xiàn)我們自定義的動畫效果了。
- Android Animation實戰(zhàn)之一個APP的ListView的動畫效果
- Android使用glide加載gif動畫設(shè)置播放次數(shù)
- Android Glide圖片加載(加載監(jiān)聽、加載動畫)
- Android實現(xiàn)跳動的小球加載動畫效果
- Android自定義加載loading view動畫組件
- Android自定義加載控件實現(xiàn)數(shù)據(jù)加載動畫
- Android加載Gif動畫實現(xiàn)代碼
- Android自定義view實現(xiàn)阻尼效果的加載動畫
- Android自定義View實現(xiàn)loading動畫加載效果
- Android使用View Animation實現(xiàn)動畫加載界面
相關(guān)文章
android 加載本地聯(lián)系人實現(xiàn)方法
在android開發(fā)過程中,有些功能需要訪問本地聯(lián)系人列表,本人搜集整理了一番,拿出來和大家分享一下,希望可以幫助你們2012-12-12
詳解Flutter中StatefulBuilder組件的使用
StatefulBuilder小部件可以在這些區(qū)域的狀態(tài)發(fā)生變化時僅重建某些小區(qū)域而無需付出太多努力。本文將來詳細講講它的使用,需要的可以參考一下2022-05-05
Android使用手勢監(jiān)聽器GestureDetector遇到的不響應(yīng)問題
這篇文章主要介紹了Android使用手勢監(jiān)聽器GestureDetector遇到的不響應(yīng)問題,具有很好的參考價值,對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
淺談Android獲取ImageView上的圖片,和一個有可能遇到的問題
下面小編就為大家?guī)硪黄獪\談Android獲取ImageView上的圖片,和一個有可能遇到的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04

