Android繪制旋轉(zhuǎn)動(dòng)畫方法詳解
1、準(zhǔn)備工作
首先需要有一個(gè)用于旋轉(zhuǎn)的圖片
需要考慮如何開始、結(jié)束、加速、減速
2、加速減速原理
本次的動(dòng)畫采用RotateAnimation,初始化需要的參數(shù)如下
public RotateAnimation(float fromDegrees,float toDegrees,int pivotXType,float pivotXValue,int pivotYType, float pivotYValue) {
mFromDegrees = fromDegrees;//開始角度
mToDegrees = toDegrees;//結(jié)束角度
mPivotXValue = pivotXValue;//確定x軸坐標(biāo)的類型
mPivotXType = pivotXType;//x軸的值,0.5f表明是以自身這個(gè)控件的一半長(zhǎng)度為x軸
mPivotYValue = pivotYValue;//確定y軸坐標(biāo)的類型
mPivotYType = pivotYType;//y軸的值,0.5f表明是以自身這個(gè)控件的一半長(zhǎng)度為y軸
initializePivotPoint();
}
所謂旋轉(zhuǎn)動(dòng)畫,在本質(zhì)上就是在如上的對(duì)象初始化之后,規(guī)定在一定的周期內(nèi)旋轉(zhuǎn)
所謂加速,本質(zhì)上就是在設(shè)定好的周期內(nèi)變換旋轉(zhuǎn)角度
或者修改周期,在預(yù)設(shè)周期內(nèi)旋轉(zhuǎn)一定角度
總之,角度和周期一定會(huì)變化一個(gè),就可以決定動(dòng)畫的快慢。
如: 從 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è)置動(dòng)畫持續(xù)周期
rotate.setRepeatCount(-1);//設(shè)置重復(fù)次數(shù)
// rotate.setFillAfter(true);//動(dòng)畫執(zhí)行完后是否停留在執(zhí)行完的狀態(tài)
rotate.setStartOffset(10);//執(zhí)行前的等待時(shí)間
4、開始
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fan.startAnimation(rotate);
}
});
5、加速
首先需要?jiǎng)?chuàng)建全局變量
private int duration=2000;
加速樣例
accelerate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (duration>10){
duration/=2; //周期除2角度不變加速(需要考慮極端,所以加一個(gè)判斷)
}
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角度不變減速(需要考慮極端,所以加一個(gè)判斷)
}
rotate.setDuration(duration); //設(shè)置周期
fan.startAnimation(rotate); //開始旋轉(zhuǎn)
}
});
7、停止
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fan.clearAnimation(); //停止
}
});
8、項(xiàng)目源碼
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è)置動(dòng)畫持續(xù)周期
rotate.setRepeatCount(-1);//設(shè)置重復(fù)次數(shù)
// rotate.setFillAfter(true);//動(dòng)畫執(zhí)行完后是否停留在執(zhí)行完的狀態(tài)
rotate.setStartOffset(10);//執(zhí)行前的等待時(shí)間
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("點(diǎn)我沒用");
dialog.setIcon(R.drawable.fan);
dialog.setPositiveButton("OK",null);
dialog.setMessage("這是普通對(duì)話框");
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();
}
});
}
}
需要有三個(gè)圖片:
fan.png 風(fēng)扇扇葉
border.png 風(fēng)扇邊框
title.png 貼圖
(隨便找一個(gè)能看就行)
以上就是Android繪制旋轉(zhuǎn)動(dòng)畫方法詳解的詳細(xì)內(nèi)容,更多關(guān)于Android旋轉(zhuǎn)動(dòng)畫的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android中實(shí)現(xiàn)多行、水平滾動(dòng)的分頁的Gridview實(shí)例源碼
如果單行水平滾動(dòng),可以用Horizontalscrollview實(shí)現(xiàn)。如果是多行水平滾動(dòng),則結(jié)合Gridview(一般是垂直滾動(dòng)的)和Horizontalscrollview實(shí)現(xiàn)2013-06-06
Android Studio導(dǎo)入so文件到項(xiàng)目中的實(shí)例詳解
這篇文章主要介紹了Android Studio導(dǎo)入so文件到項(xiàng)目中的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09
Android利用Canvas標(biāo)點(diǎn)畫線并加入位移動(dòng)畫(1)
這篇文章主要為大家詳細(xì)介紹了Android利用Canvas標(biāo)點(diǎn)畫線并加入位移動(dòng)畫的第一篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
Android基于廣播事件機(jī)制實(shí)現(xiàn)簡(jiǎn)單定時(shí)提醒功能代碼
這篇文章主要介紹了Android基于廣播事件機(jī)制實(shí)現(xiàn)簡(jiǎn)單定時(shí)提醒功能代碼,較為詳細(xì)的分析了Android廣播事件機(jī)制及提醒功能的相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android中AutoCompleteTextView與TextWatcher結(jié)合小實(shí)例
這篇文章主要為大家詳細(xì)介紹了Android中AutoCompleteTextView與TextWatcher結(jié)合的小實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
Android獲取手機(jī)配置信息具體實(shí)現(xiàn)代碼
下面為大家介紹下使用android獲取手機(jī)配置信息的具體過程,感興趣的朋友可以參考下哈,希望對(duì)你有所幫助2013-06-06
Android實(shí)現(xiàn)志愿者系統(tǒng)詳細(xì)步驟與代碼
這篇文章主要介紹了Android實(shí)現(xiàn)志愿者系統(tǒng),本系統(tǒng)采用MVC架構(gòu)設(shè)計(jì),SQLite數(shù)據(jù)表有用戶表、成員表和活動(dòng)表,有十多個(gè)Activity頁面。打開應(yīng)用,進(jìn)入歡迎界面,3s后跳轉(zhuǎn)登錄界面,用戶先注冊(cè)賬號(hào),登錄成功后進(jìn)入主界面2023-02-02

