欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)旋轉(zhuǎn)動畫

 更新時間:2022年01月06日 11:12:54   作者:吃橘子的季節(jié)呢  
這篇文章主要為大家詳細介紹了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初學(xué)者的Gradle知識普及

    給Android初學(xué)者的Gradle知識普及

    剛學(xué) Android 不久,對 Gradle 不懂,看了很多資料依然一知半解,很多人都這樣覺得,表示同感,下面小編來給大家講講 Gradle相關(guān)知識,需要的朋友跟隨小編一起來學(xué)習(xí)一下
    2018-09-09
  • Android上使用grpc的方法教程

    Android上使用grpc的方法教程

    這篇文章主要給大家介紹了在Android上使用grpc的方法教程,文中通過示例代碼給大家詳細介紹了在android上使用grpc的方法以及可能遇到的種種問題的解決方法,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2017-07-07
  • Android支付寶支付的示例代碼

    Android支付寶支付的示例代碼

    本篇文章主要介紹了Android支付寶支付的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Android組合控件自定義標題欄

    Android組合控件自定義標題欄

    這篇文章主要為大家詳細介紹了Android組合控件自定義標題欄,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Android實現(xiàn)自定義滑動式抽屜菜單效果

    Android實現(xiàn)自定義滑動式抽屜菜單效果

    這篇文章主要為大家詳細介紹了Android實現(xiàn)自定義滑動式抽屜效果菜單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android 仿微信圖像拍攝和選擇界面功能(代碼分享)

    Android 仿微信圖像拍攝和選擇界面功能(代碼分享)

    這篇文章主要介紹了 Android 仿微信圖像拍攝和選擇界面功能,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下
    2017-01-01
  • Android中activity的啟動模式

    Android中activity的啟動模式

    本文主要介紹了Android中activity的啟動模式。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • Android ApiDemo示例工程的創(chuàng)建

    Android ApiDemo示例工程的創(chuàng)建

    本文主要介紹Android ApiDemo示例工程的創(chuàng)建,這里SDK中的示例工程做了大致介紹,并說明如何創(chuàng)建ApiDemo 示例工程,有需要看自帶代碼的朋友可以參考下
    2016-09-09
  • Android獲取ROOT權(quán)限的實例代碼

    Android獲取ROOT權(quán)限的實例代碼

    這篇文章主要介紹了Android如何獲取ROOT權(quán)限,寫了一個小方法,大家可以在應(yīng)用中檢測ROOT權(quán)限
    2013-11-11
  • Android開發(fā)筆記之:消息循環(huán)與Looper的詳解

    Android開發(fā)筆記之:消息循環(huán)與Looper的詳解

    本篇文章是對Android中消息循環(huán)與Looper的應(yīng)用進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05

最新評論