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

代碼很簡(jiǎn)單,直接上源碼
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="開(kāi)始"/> <Button android:id="@+id/btn_end" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="結(jié)束"/> </LinearLayout></LinearLayout>
也可以用其它的View控件替代ImageView,都是可以實(shí)現(xiàn)搖擺效果的
主界面MainActivity
/** * 主界面 * Created by zhuwentao on 2016-08-08. */public class MainActivity extends AppCompatActivity implements View.OnClickListener{ /** 表盤(pán)圖片 */ private ImageView mDialIv; /** 開(kāi)始按鈕 */ 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)聽(tīng) */ 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è)置動(dòng)畫(huà) */ private void showAnimation() { // 獲取自定義動(dòng)畫(huà)實(shí)例 CustomRotateAnim rotateAnim = CustomRotateAnim.getCustomRotateAnim(); // 一次動(dòng)畫(huà)執(zhí)行1秒 rotateAnim.setDuration(1000); // 設(shè)置為循環(huán)播放 rotateAnim.setRepeatCount(-1); // 設(shè)置為勻速 rotateAnim.setInterpolator(new LinearInterpolator()); // 開(kāi)始播放動(dòng)畫(huà) mDialIv.startAnimation(rotateAnim); }}setRepeatCount()設(shè)置的是重復(fù)播放動(dòng)畫(huà)的次數(shù),-1是為了讓它循環(huán)播放,setRepeatCount(0)代表的是執(zhí)行一次,setRepeatCount(1)代表重復(fù)1次,即動(dòng)畫(huà)執(zhí)行2次。
setInterpolator()方法是設(shè)置插值器,用來(lái)指定動(dòng)畫(huà)的效果,這里使用系統(tǒng)提供的LinearInterpolator()勻速變化效果。
自定義的CustomRotateAnim動(dòng)畫(huà)需要繼承Animation,這里只要實(shí)現(xiàn)它的initialize()和applyTransformation()方法就好
/** * 左右搖擺動(dòng)畫(huà) * Created by zhuwentao on 2016-08-08. */public class CustomRotateAnim extends Animation { /** 控件寬 */ private int mWidth; /** 控件高 */ private int mHeight; /** 實(shí)例 */ private static CustomRotateAnim rotateAnim; /** * 獲取動(dòng)畫(huà)實(shí)例 * @return 實(shí)例 */ 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代表指定播放動(dòng)畫(huà)的View空間寬高,parentWidth和parentHeight代表該View控件所在的父控件寬高。
我們需要使用當(dāng)前View的寬高來(lái)確定搖擺的旋轉(zhuǎn)點(diǎn),所以在initialize中獲取View控件的寬高。
applyTransformation()方法是動(dòng)畫(huà)具體的實(shí)現(xiàn)方法,在系統(tǒng)繪制動(dòng)畫(huà)時(shí)會(huì)反復(fù)調(diào)用這個(gè)方法,每調(diào)用一次applyTransformation()方法,其中的interpolatedTime參數(shù)都會(huì)改變一次,值從0到1遞增,當(dāng)interpolatedTime的值為1時(shí)則動(dòng)畫(huà)結(jié)束。
Transformatio類是一個(gè)變換的矩陣,通過(guò)改變?cè)摼仃嚲涂梢詫?shí)現(xiàn)各種復(fù)雜的效果。
復(fù)寫(xiě)這個(gè)方法,在里面就可以實(shí)現(xiàn)我們自定義的動(dòng)畫(huà)效果了。
新聞熱點(diǎn)
疑難解答
圖片精選