Android繪制旋轉(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 貼圖
(隨便找一個能看就行)
以上就是Android繪制旋轉(zhuǎn)動畫方法詳解的詳細內(nèi)容,更多關(guān)于Android旋轉(zhuǎn)動畫的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android中實現(xiàn)多行、水平滾動的分頁的Gridview實例源碼
如果單行水平滾動,可以用Horizontalscrollview實現(xiàn)。如果是多行水平滾動,則結(jié)合Gridview(一般是垂直滾動的)和Horizontalscrollview實現(xiàn)2013-06-06Android Studio導(dǎo)入so文件到項目中的實例詳解
這篇文章主要介紹了Android Studio導(dǎo)入so文件到項目中的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09Android基于廣播事件機制實現(xiàn)簡單定時提醒功能代碼
這篇文章主要介紹了Android基于廣播事件機制實現(xiàn)簡單定時提醒功能代碼,較為詳細的分析了Android廣播事件機制及提醒功能的相關(guān)實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10Android中AutoCompleteTextView與TextWatcher結(jié)合小實例
這篇文章主要為大家詳細介紹了Android中AutoCompleteTextView與TextWatcher結(jié)合的小實例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05Android實現(xiàn)志愿者系統(tǒng)詳細步驟與代碼
這篇文章主要介紹了Android實現(xiàn)志愿者系統(tǒng),本系統(tǒng)采用MVC架構(gòu)設(shè)計,SQLite數(shù)據(jù)表有用戶表、成員表和活動表,有十多個Activity頁面。打開應(yīng)用,進入歡迎界面,3s后跳轉(zhuǎn)登錄界面,用戶先注冊賬號,登錄成功后進入主界面2023-02-02