Android繪制旋轉(zhuǎn)動(dòng)畫(huà)方法詳解
1、準(zhǔn)備工作
首先需要有一個(gè)用于旋轉(zhuǎn)的圖片
需要考慮如何開(kāi)始、結(jié)束、加速、減速
2、加速減速原理
本次的動(dòng)畫(huà)采用RotateAnimation,初始化需要的參數(shù)如下
public RotateAnimation(float fromDegrees,float toDegrees,int pivotXType,float pivotXValue,int pivotYType, float pivotYValue) {
mFromDegrees = fromDegrees;//開(kāi)始角度
mToDegrees = toDegrees;//結(jié)束角度
mPivotXValue = pivotXValue;//確定x軸坐標(biāo)的類(lèi)型
mPivotXType = pivotXType;//x軸的值,0.5f表明是以自身這個(gè)控件的一半長(zhǎng)度為x軸
mPivotYValue = pivotYValue;//確定y軸坐標(biāo)的類(lèi)型
mPivotYType = pivotYType;//y軸的值,0.5f表明是以自身這個(gè)控件的一半長(zhǎng)度為y軸
initializePivotPoint();
}
所謂旋轉(zhuǎn)動(dòng)畫(huà),在本質(zhì)上就是在如上的對(duì)象初始化之后,規(guī)定在一定的周期內(nèi)旋轉(zhuǎn)
所謂加速,本質(zhì)上就是在設(shè)定好的周期內(nèi)變換旋轉(zhuǎn)角度
或者修改周期,在預(yù)設(shè)周期內(nèi)旋轉(zhuǎn)一定角度
總之,角度和周期一定會(huì)變化一個(gè),就可以決定動(dòng)畫(huà)的快慢。
如: 從 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)畫(huà)持續(xù)周期
rotate.setRepeatCount(-1);//設(shè)置重復(fù)次數(shù)
// rotate.setFillAfter(true);//動(dòng)畫(huà)執(zhí)行完后是否停留在執(zhí)行完的狀態(tài)
rotate.setStartOffset(10);//執(zhí)行前的等待時(shí)間
4、開(kāi)始
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); //開(kāi)始旋轉(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); //開(kāi)始旋轉(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="開(kāi)始"
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)畫(huà)持續(xù)周期
rotate.setRepeatCount(-1);//設(shè)置重復(fù)次數(shù)
// rotate.setFillAfter(true);//動(dòng)畫(huà)執(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)我沒(méi)用");
dialog.setIcon(R.drawable.fan);
dialog.setPositiveButton("OK",null);
dialog.setMessage("這是普通對(duì)話(huà)框");
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)畫(huà)方法詳解的詳細(xì)內(nèi)容,更多關(guān)于Android旋轉(zhuǎn)動(dòng)畫(huà)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android入門(mén)之計(jì)時(shí)器Chronometer的使用教程
Chronometer是一個(gè)簡(jiǎn)單的定時(shí)器,你可以給它一個(gè)開(kāi)始時(shí)間,并以此定時(shí)。本文將利用個(gè)簡(jiǎn)單的示例為大家講解一下它的使用,感興趣的小伙伴可以嘗試一下2022-11-11
android常見(jiàn)手動(dòng)和自動(dòng)輪播圖效果
這篇文章主要為大家詳細(xì)介紹了android常見(jiàn)手動(dòng)和自動(dòng)輪播圖效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Android自定義View實(shí)現(xiàn)點(diǎn)贊控件
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)點(diǎn)贊控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Android EditText設(shè)置邊框的操作方法
這篇文章主要介紹了Android EditText設(shè)置邊框,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-12-12
Android使用Handler實(shí)現(xiàn)View彈性滑動(dòng)
這篇文章主要介紹了Android使用Handler實(shí)現(xiàn)View彈性滑動(dòng),介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
Android 開(kāi)發(fā)之dataBinding與ListView及事件
這篇文章主要介紹了Android 開(kāi)發(fā)之dataBinding與ListView及事件的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10
點(diǎn)擊圖標(biāo)進(jìn)入指定瀏覽器將首頁(yè)設(shè)置全透明解決一閃而過(guò)問(wèn)題
進(jìn)入瀏覽器之前有一個(gè)頁(yè)面閃了一下,那是因?yàn)閺哪莻€(gè)空白的首頁(yè)跳過(guò)去的。解決的辦法是把他變成透明的就好了2014-08-08
Android異步加載數(shù)據(jù)和圖片的保存思路詳解
這篇文章主要介紹了Android異步加載數(shù)據(jù)和圖片的保存思路詳解的相關(guān)資料,需要的朋友可以參考下2016-04-04

