Android實現(xiàn)旋轉(zhuǎn)動畫
本文實例為大家分享了Android實現(xiàn)旋轉(zhuǎn)動畫的具體代碼,供大家參考,具體內(nèi)容如下
旋轉(zhuǎn)動畫(可加速、減速)
1、準備工作
首先需要有一個用于旋轉(zhuǎn)的圖片
需要考慮如何開始、結(jié)束、加速、減速
2、加速減速原理
本次的動畫采用RotateAnimation,初始化需要的參數(shù)如下
public RotateAnimation(float fromDegrees,float toDegrees,int pivotXType,float pivotXValue,int pivotYType, float pivotYValue) { ? ? mFromDegrees = fromDegrees;//開始角度 ? ? mToDegrees = toDegrees;//結(jié)束角度 ? ? mPivotXValue = pivotXValue;//確定x軸坐標的類型 ? ? mPivotXType = pivotXType;//x軸的值,0.5f表明是以自身這個控件的一半長度為x軸 ? ? mPivotYValue = pivotYValue;//確定y軸坐標的類型 ? ? mPivotYType = pivotYType;//y軸的值,0.5f表明是以自身這個控件的一半長度為y軸 ? ? initializePivotPoint(); }
所謂旋轉(zhuǎn)動畫,在本質(zhì)上就是在如上的對象初始化之后,規(guī)定在一定的周期內(nèi)旋轉(zhuǎn)
所謂加速,本質(zhì)上就是在設(shè)定好的周期內(nèi)變換旋轉(zhuǎn)角度
或者修改周期,在預(yù)設(shè)周期內(nèi)旋轉(zhuǎn)一定角度
總之,角度和周期一定會變化一個,就可以決定動畫的快慢。
如: 從 2秒內(nèi)旋轉(zhuǎn)360度 到 1秒內(nèi)旋轉(zhuǎn)360度 就是一種加速,從 2秒內(nèi)旋轉(zhuǎn)360度 到 2秒內(nèi)旋轉(zhuǎn)720度 也是一種加速。
? 反之就是減速。
3、初始化
RotateAnimation rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ? ? ? ? rotate.setInterpolator(new LinearInterpolator()); ? ? ? ? rotate.setDuration(2000);//設(shè)置動畫持續(xù)周期 ? ? ? ? rotate.setRepeatCount(-1);//設(shè)置重復(fù)次數(shù) // ? ? ? ?rotate.setFillAfter(true);//動畫執(zhí)行完后是否停留在執(zhí)行完的狀態(tài) ? ? ? ? rotate.setStartOffset(10);//執(zhí)行前的等待時間
4、開始
start.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? fan.startAnimation(rotate); ? ? ? ? ? ? } ? ? ? ? });
5、加速
首先需要創(chuàng)建全局變量
private int duration=2000;
加速樣例
accelerate.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? if (duration>10){ ? ? ? ? ? ? ? ? ? ? duration/=2;?? ?//周期除2角度不變加速(需要考慮極端,所以加一個判斷) ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? rotate.setDuration(duration);?? ?//設(shè)置周期 ? ? ? ? ? ? ? ? fan.startAnimation(rotate);?? ??? ?//開始旋轉(zhuǎn) ? ? ? ? ? ? } ? ? ? ? });
6、減速
decelerate.setOnClickListener(new View.OnClickListener() { ? ? @Override ? ? public void onClick(View v) { ? ? ? ? if (duration<10000){ ? ? ? ? ? ? duration*=2;?? ?//周期乘2角度不變減速(需要考慮極端,所以加一個判斷) ? ? ? ? } ? ? ? ? rotate.setDuration(duration);?? ?//設(shè)置周期 ? ? ? ? fan.startAnimation(rotate);?? ??? ?//開始旋轉(zhuǎn) ? ? } });
7、停止
stop.setOnClickListener(new View.OnClickListener() { ? ? @Override ? ? public void onClick(View v) { ? ? ? ? fan.clearAnimation();?? ?//停止 ? ? } });
8、項目源碼
Layout部分
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:background="#DEECFA" ? ? tools:context=".MainActivity"> ? ? <RelativeLayout ? ? ? ? android:layout_centerHorizontal="true" ? ? ? ? android:layout_marginTop="100dp" ? ? ? ? android:id="@+id/relativeLayout" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:background="@mipmap/border" ? ? ? ? tools:layout_editor_absoluteX="566dp" ? ? ? ? tools:layout_editor_absoluteY="132dp"> ? ? ? ? <ImageView ? ? ? ? ? ? android:id="@+id/fan" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? app:srcCompat="@mipmap/fan" ? ? ? ? ? ? tools:layout_editor_absoluteX="552dp" ? ? ? ? ? ? tools:layout_editor_absoluteY="122dp" /> ? ? </RelativeLayout> ? ? <LinearLayout ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_alignParentBottom="true" ? ? ? ? android:layout_centerHorizontal="true" ? ? ? ? android:layout_marginBottom="150dp"> ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/start" ? ? ? ? ? ? android:layout_width="60dp" ? ? ? ? ? ? android:layout_height="60dp" ? ? ? ? ? ? android:background="@mipmap/border" ? ? ? ? ? ? android:text="開始" ? ? ? ? ? ? tools:layout_editor_absoluteX="525dp" ? ? ? ? ? ? tools:layout_editor_absoluteY="596dp" /> ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/accelerate" ? ? ? ? ? ? android:layout_marginLeft="100dp" ? ? ? ? ? ? android:layout_width="60dp" ? ? ? ? ? ? android:layout_height="60dp" ? ? ? ? ? ? android:background="@mipmap/border" ? ? ? ? ? ? android:text="加速" ? ? ? ? ? ? tools:layout_editor_absoluteX="650dp" ? ? ? ? ? ? tools:layout_editor_absoluteY="596dp" /> ? ? ? ? <Button ? ? ? ? ? ? android:layout_marginLeft="100dp" ? ? ? ? ? ? android:id="@+id/decelerate" ? ? ? ? ? ? android:layout_width="60dp" ? ? ? ? ? ? android:layout_height="60dp" ? ? ? ? ? ? android:background="@mipmap/border" ? ? ? ? ? ? android:text="減速" ? ? ? ? ? ? tools:layout_editor_absoluteX="795dp" ? ? ? ? ? ? tools:layout_editor_absoluteY="596dp" /> ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/stop" ? ? ? ? ? ? android:layout_marginLeft="100dp" ? ? ? ? ? ? android:layout_width="60dp" ? ? ? ? ? ? android:layout_height="60dp" ? ? ? ? ? ? android:background="@mipmap/border" ? ? ? ? ? ? android:text="結(jié)束" ? ? ? ? ? ? tools:layout_editor_absoluteX="950dp" ? ? ? ? ? ? tools:layout_editor_absoluteY="596dp" /> ? ? </LinearLayout> ? ? <ImageView ? ? ? ? android:layout_centerVertical="true" ? ? ? ? android:layout_marginLeft="90dp" ? ? ? ? android:id="@+id/imageView" ? ? ? ? android:layout_width="261dp" ? ? ? ? android:layout_height="527dp" ? ? ? ? app:srcCompat="@mipmap/title" ? ? ? ? tools:layout_editor_absoluteX="141dp" ? ? ? ? tools:layout_editor_absoluteY="132dp" /> </RelativeLayout>
MainActivity部分
package com.suk.rotate; import androidx.appcompat.app.AppCompatActivity; import android.app.AlertDialog; import android.graphics.drawable.Drawable; import android.graphics.drawable.RotateDrawable; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.view.animation.LinearInterpolator; import android.view.animation.RotateAnimation; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.Toast; import com.suk.rotate.R; public class MainActivity extends AppCompatActivity { ? ? private ImageView fan; ? ? private Button start; ? ? private Button stop; ? ? private Button accelerate; ? ? private RotateAnimation rotate; ? ? private Button decelerate; ? ? private int duration=2000; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? fan = findViewById(R.id.fan); ? ? ? ? start = findViewById(R.id.start); ? ? ? ? stop = findViewById(R.id.stop); ? ? ? ? accelerate = findViewById(R.id.accelerate); ? ? ? ? decelerate = findViewById(R.id.decelerate); ? ? } ? ? @Override ? ? protected void onStart() { ? ? ? ? super.onStart(); ? ? ? ? rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ? ? ? ? rotate.setInterpolator(new LinearInterpolator()); // ? ? ? ?rotate.setInterpolator(lin); ? ? ? ? rotate.setDuration(2000);//設(shè)置動畫持續(xù)周期 ? ? ? ? rotate.setRepeatCount(-1);//設(shè)置重復(fù)次數(shù) // ? ? ? ?rotate.setFillAfter(true);//動畫執(zhí)行完后是否停留在執(zhí)行完的狀態(tài) ? ? ? ? rotate.setStartOffset(10);//執(zhí)行前的等待時間 ? ? ? ? start.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? fan.startAnimation(rotate); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? accelerate.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? if (duration>10){ ? ? ? ? ? ? ? ? ? ? duration/=2;} ? ? ? ? ? ? ? ? rotate.setDuration(duration); ? ? ? ? ? ? ? ? fan.startAnimation(rotate); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? decelerate.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? if (duration<10000){ ? ? ? ? ? ? ? ? ? ? duration*=2;} ? ? ? ? ? ? ? ? rotate.setDuration(duration); ? ? ? ? ? ? ? ? fan.startAnimation(rotate); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? /* ? ? ? ? fan.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); ? ? ? ? ? ? ? ? dialog.setTitle("點我沒用"); ? ? ? ? ? ? ? ? dialog.setIcon(R.drawable.fan); ? ? ? ? ? ? ? ? dialog.setPositiveButton("OK",null); ? ? ? ? ? ? ? ? dialog.setMessage("這是普通對話框"); ? ? ? ? ? ? ? ? View view=View.inflate(MainActivity.this,R.layout. activity_main1, null); ? ? ? ? ? ? ? ? dialog.setView(view); ? ? ? ? ? ? ? ? dialog.create(); ? ? ? ? ? ? ? ? dialog.show(); ? ? ? ? ? ? } ? ? ? ? }); ?? ??? ?*/ ? ? ? ? stop.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? fan.clearAnimation(); ? ? ? ? ? ? } ? ? ? ? }); ? ? } }
需要有三個圖片:
? fan.png 風(fēng)扇扇葉
? border.png 風(fēng)扇邊框
? title.png 貼圖
(隨便找一個能看就行)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android ApiDemo示例工程的創(chuàng)建
本文主要介紹Android ApiDemo示例工程的創(chuàng)建,這里SDK中的示例工程做了大致介紹,并說明如何創(chuàng)建ApiDemo 示例工程,有需要看自帶代碼的朋友可以參考下2016-09-09Android開發(fā)筆記之:消息循環(huán)與Looper的詳解
本篇文章是對Android中消息循環(huán)與Looper的應(yīng)用進行了詳細的分析介紹,需要的朋友參考下2013-05-05