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

Android實(shí)現(xiàn)旋轉(zhuǎn)動(dòng)畫(huà)的兩種方式案例詳解

 更新時(shí)間:2021年08月09日 17:36:03   作者:我想月薪過(guò)萬(wàn)  
這篇文章主要介紹了Android實(shí)現(xiàn)旋轉(zhuǎn)動(dòng)畫(huà)的兩種方式,需要的朋友可以參考下

練習(xí)案例

視差動(dòng)畫(huà) - 雅虎新聞?wù)虞d

效果展示

前期準(zhǔn)備

第一步:準(zhǔn)備好顏色數(shù)組 res => values => colors.xml

 <color name="orange">#FF9600</color>
    <color name="aqua">#02D1AC</color>
    <color name="yellow">#FFD200</color>
    <color name="bule">#00C6FF</color>
    <color name="green">#00E099</color>
    <color name="pink">#FF3891</color>
 
    <array name="splash_circle_colors">
        <item>@color/orange</item>
        <item>@color/aqua</item>
        <item>@color/yellow</item>
        <item>@color/bule</item>
        <item>@color/green</item>
        <item>@color/pink</item>
    </array>

自定義 View java代碼編寫(xiě)

方法一

關(guān)鍵思想: 屬性動(dòng)畫(huà) + 計(jì)算圓心

package com.wust.mydialog;
 
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;
 
import androidx.annotation.Nullable;
 
 
/**
 * ClassName: com.wust.mydialog.MyRotateView <br/>
 * Description: <br/>
 * date: 2021/8/7 12:13<br/>
 *
 * @author yiqi<br />
 * @QQ 1820762465
 * @微信 yiqiideallife
 * @技術(shù)交流QQ群 928023749
 */
public class MyRotateView extends View {
 
    //設(shè)置旋轉(zhuǎn)間隔時(shí)間
    private int SPLASH_CIRCLE_ROTATE_TIME = 3000;
    //設(shè)置中心圓半徑
    private float CENTER_CIRCLE_RADIUS;
    private float SMALL_CIRCLE_RADIUS;
    private float mCurrentSingle = 0f;
    private int[] mColorArray;
    private Paint mCirclePaint;
    private ValueAnimator va;
 
    public MyRotateView(Context context) {
        super(context);
    }
 
    public MyRotateView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
 
    public MyRotateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
 
        //初始化參數(shù)
        initParams(width,height);
 
        setMeasuredDimension(width,height);
    }
 
    private void initParams(int w, int h) {
        //設(shè)置中心圓半徑
        CENTER_CIRCLE_RADIUS = 1/4.0f * w;
        //設(shè)置小圓的半徑
        SMALL_CIRCLE_RADIUS = 1/25.0f * w;
        //獲取小球顏色
        mColorArray = getResources().getIntArray(R.array.splash_circle_colors);
        //初始化畫(huà)筆
        mCirclePaint = new Paint();
        mCirclePaint.setDither(true);
        mCirclePaint.setAntiAlias(true);
 
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //繪制圓
        drawSplashCircle(canvas);
    }
 
    private void drawSplashCircle(Canvas canvas) {
        //設(shè)置屬性動(dòng)畫(huà),讓小圓轉(zhuǎn)起來(lái)
        //這里得注意,是個(gè)坑,你如果不判斷那球就不會(huì)動(dòng) 因?yàn)闀?huì)陷入死循環(huán) 值動(dòng)畫(huà)將值設(shè)置為0 -> invalidate()重繪 -> 執(zhí)行draw 又將值設(shè)為0
        if (va == null){
            va = ObjectAnimator.ofFloat(0f, 2 * (float) Math.PI);
            va.setDuration(SPLASH_CIRCLE_ROTATE_TIME);
            va.setRepeatCount(ValueAnimator.INFINITE);
            va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    mCurrentSingle = (float) animation.getAnimatedValue();
//                    System.out.println("mCurrentSingle ->" + mCurrentSingle);
                    invalidate();
                }
            });
            va.setInterpolator(new LinearInterpolator());
            va.start();
        }
 
        //計(jì)算每個(gè)小球的間隔
        double spaceAngle = Math.PI*2/mColorArray.length;
 
        for (int i = 0; i < mColorArray.length; i++) {
            //為 每個(gè)球 畫(huà)筆 設(shè)置顏色
            mCirclePaint.setColor(mColorArray[i]);
 
            //利用 勾股定理 計(jì)算 小圓 中心點(diǎn)
            float cx = getWidth()/2 + (float) (CENTER_CIRCLE_RADIUS*Math.cos(spaceAngle*i+mCurrentSingle));
            float cy = getHeight()/2 + (float) (CENTER_CIRCLE_RADIUS*Math.sin(spaceAngle*i+mCurrentSingle));
 
            canvas.drawCircle(cx,cy,SMALL_CIRCLE_RADIUS,mCirclePaint);
        }
    }
}

方法二

關(guān)鍵思想:旋轉(zhuǎn)畫(huà)布法

package com.wust.mydialog;
 
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;
 
import androidx.annotation.Nullable;
 
 
/**
 * ClassName: com.wust.mydialog.MyRotateView <br/>
 * Description: <br/>
 * date: 2021/8/7 12:13<br/>
 *
 * @author yiqi<br />
 * @QQ 1820762465
 * @微信 yiqiideallife
 * @技術(shù)交流QQ群 928023749
 */
public class MyRotateView extends View {
 
    //設(shè)置旋轉(zhuǎn)間隔時(shí)間
    private int SPLASH_CIRCLE_ROTATE_TIME = 3000;
    //設(shè)置中心圓半徑
    private float CENTER_CIRCLE_RADIUS;
    private float SMALL_CIRCLE_RADIUS;
    private float mCurrentSingle = 0f;
    private int[] mColorArray;
    private Paint mCirclePaint;
    private ValueAnimator va;
 
    public MyRotateView(Context context) {
        super(context);
    }
 
    public MyRotateView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
 
    public MyRotateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
 
        //初始化參數(shù)
        initParams(width,height);
 
        setMeasuredDimension(width,height);
    }
 
    private void initParams(int w, int h) {
        //設(shè)置中心圓半徑
        CENTER_CIRCLE_RADIUS = 1/4.0f * w;
        //設(shè)置小圓的半徑
        SMALL_CIRCLE_RADIUS = 1/25.0f * w;
        //獲取小球顏色
        mColorArray = getResources().getIntArray(R.array.splash_circle_colors);
        //初始化畫(huà)筆
        mCirclePaint = new Paint();
        mCirclePaint.setDither(true);
        mCirclePaint.setAntiAlias(true);
 
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //繪制圓
        drawSplashCircle(canvas);
    }
 
    private void drawSplashCircle(Canvas canvas) {
        //設(shè)置屬性動(dòng)畫(huà),讓小圓轉(zhuǎn)起來(lái)
        //這里得注意,是個(gè)坑,你如果不判斷那球就不會(huì)動(dòng) 因?yàn)闀?huì)陷入死循環(huán) 值動(dòng)畫(huà)將值設(shè)置為0 -> invalidate()重繪 -> 執(zhí)行draw 又將值設(shè)為0
        if (va == null){
//            va = ObjectAnimator.ofFloat(0f, 2 * (float) Math.PI);
            va = ObjectAnimator.ofFloat(0f, 360.0f);
            va.setDuration(SPLASH_CIRCLE_ROTATE_TIME);
            va.setRepeatCount(ValueAnimator.INFINITE);
            va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    mCurrentSingle = (float) animation.getAnimatedValue();
//                    System.out.println("mCurrentSingle ->" + mCurrentSingle);
                    invalidate();
                }
            });
            va.setInterpolator(new LinearInterpolator());
            va.start();
        }
 
        //計(jì)算每個(gè)小球的間隔
//        double spaceAngle = Math.PI*2/mColorArray.length;
        double spaceAngle = 360.0d/mColorArray.length;
        System.out.println("spaceAngle -> " + spaceAngle);
 
        //利用旋轉(zhuǎn)畫(huà)布法
        canvas.save();
        canvas.rotate(mCurrentSingle,getWidth()/2,getHeight()/2);
        for (int i = 0; i < mColorArray.length; i++) {
            canvas.rotate((float) spaceAngle,getWidth()/2,getHeight()/2);
            //為 每個(gè)球 畫(huà)筆 設(shè)置顏色
            mCirclePaint.setColor(mColorArray[i]);
 
            //利用 勾股定理 計(jì)算 小圓 中心點(diǎn)
            //float cx = getWidth()/2 + (float) (CENTER_CIRCLE_RADIUS*Math.cos(spaceAngle*i+mCurrentSingle));
            //float cy = getHeight()/2 + (float) (CENTER_CIRCLE_RADIUS*Math.sin(spaceAngle*i+mCurrentSingle));
 
            //利用旋轉(zhuǎn)畫(huà)布法
            float cx = getWidth()/2 + CENTER_CIRCLE_RADIUS;
            float cy = getHeight()/2;
 
            canvas.drawCircle(cx,cy,SMALL_CIRCLE_RADIUS,mCirclePaint);
        }
        canvas.restore();
    }
}

易錯(cuò)點(diǎn)總結(jié):

1、canvas.rotate(mCurrentSingle,getWidth()/2,getHeight()/2);中 第一個(gè)參數(shù)傳的是角度(360度的那種),而 Math.cos();中 參數(shù)傳的是一個(gè)弧度(2π的那種)

2、canvas.rotate() 函數(shù)執(zhí)行之后對(duì)后續(xù)畫(huà)布上的操作都是有影響的,所以,得配合 canvas.save();和 canvas.restore();使用。因此,里面的canvas.rotate((float) spaceAngle,getWidth()/2,getHeight()/2);中spaceAngle不能乘 i 。

3、畫(huà)布的旋轉(zhuǎn)除了 canvas.rotate() 函數(shù) 可以實(shí)現(xiàn)外,還可以利用矩陣。代碼如下:

//創(chuàng)建矩陣
private Matrix mSpaceMatrix;
//初始化旋轉(zhuǎn)矩陣
mSpaceMatrix = new Matrix();
//初始化旋轉(zhuǎn)矩陣
mSpaceMatrix.reset();
mSpaceMatrix.postRotate((float) spaceAngle,getWidth()/2,getHeight()/2);
//畫(huà)布旋轉(zhuǎn)角度
canvas.concat(mSpaceMatrix);

完整代碼

package com.wust.mydialog;
 
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;
 
import androidx.annotation.Nullable;
 
 
/**
 * ClassName: com.wust.mydialog.MyRotateView <br/>
 * Description: <br/>
 * date: 2021/8/7 12:13<br/>
 *
 * @author yiqi<br />
 * @QQ 1820762465
 * @微信 yiqiideallife
 * @技術(shù)交流QQ群 928023749
 */
public class MyRotateView extends View {
 
    //設(shè)置旋轉(zhuǎn)間隔時(shí)間
    private int SPLASH_CIRCLE_ROTATE_TIME = 3000;
    //設(shè)置中心圓半徑
    private float CENTER_CIRCLE_RADIUS;
    private float SMALL_CIRCLE_RADIUS;
    private float mCurrentSingle = 0f;
    private int[] mColorArray;
    private Paint mCirclePaint;
    private ValueAnimator va;
    private Matrix mSpaceMatrix;
 
    public MyRotateView(Context context) {
        super(context);
    }
 
    public MyRotateView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
 
    public MyRotateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
 
        //初始化參數(shù)
        initParams(width,height);
 
        setMeasuredDimension(width,height);
    }
 
    private void initParams(int w, int h) {
        //設(shè)置中心圓半徑
        CENTER_CIRCLE_RADIUS = 1/4.0f * w;
        //設(shè)置小圓的半徑
        SMALL_CIRCLE_RADIUS = 1/25.0f * w;
        //獲取小球顏色
        mColorArray = getResources().getIntArray(R.array.splash_circle_colors);
        //初始化畫(huà)筆
        mCirclePaint = new Paint();
        mCirclePaint.setDither(true);
        mCirclePaint.setAntiAlias(true);
        //初始化旋轉(zhuǎn)矩陣
        mSpaceMatrix = new Matrix();
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //繪制圓
        drawSplashCircle(canvas);
    }
 
    private void drawSplashCircle(Canvas canvas) {
        //設(shè)置屬性動(dòng)畫(huà),讓小圓轉(zhuǎn)起來(lái)
        //這里得注意,是個(gè)坑,你如果不判斷那球就不會(huì)動(dòng) 因?yàn)闀?huì)陷入死循環(huán) 值動(dòng)畫(huà)將值設(shè)置為0 -> invalidate()重繪 -> 執(zhí)行draw 又將值設(shè)為0
        if (va == null){
//            va = ObjectAnimator.ofFloat(0f, 2 * (float) Math.PI);
            va = ObjectAnimator.ofFloat(0f, 360.0f);
            va.setDuration(SPLASH_CIRCLE_ROTATE_TIME);
            va.setRepeatCount(ValueAnimator.INFINITE);
            va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    mCurrentSingle = (float) animation.getAnimatedValue();
//                    System.out.println("mCurrentSingle ->" + mCurrentSingle);
                    invalidate();
                }
            });
            va.setInterpolator(new LinearInterpolator());
            va.start();
        }
 
        //計(jì)算每個(gè)小球的間隔
//        double spaceAngle = Math.PI*2/mColorArray.length;
        double spaceAngle = 360.0d/mColorArray.length;
        //初始化旋轉(zhuǎn)矩陣
        mSpaceMatrix.reset();
        mSpaceMatrix.postRotate((float) spaceAngle,getWidth()/2,getHeight()/2);
 
 
        //利用旋轉(zhuǎn)畫(huà)布法
        canvas.save();
        canvas.rotate(mCurrentSingle,getWidth()/2,getHeight()/2);
        for (int i = 0; i < mColorArray.length; i++) {
//            canvas.rotate((float) spaceAngle,getWidth()/2,getHeight()/2);
//        System.out.println("spaceAngle -> " + spaceAngle);
            canvas.concat(mSpaceMatrix);
            //為 每個(gè)球 畫(huà)筆 設(shè)置顏色
            mCirclePaint.setColor(mColorArray[i]);
 
            //利用 勾股定理 計(jì)算 小圓 中心點(diǎn)
            //float cx = getWidth()/2 + (float) (CENTER_CIRCLE_RADIUS*Math.cos(spaceAngle*i+mCurrentSingle));
            //float cy = getHeight()/2 + (float) (CENTER_CIRCLE_RADIUS*Math.sin(spaceAngle*i+mCurrentSingle));
 
            //利用旋轉(zhuǎn)畫(huà)布法
            float cx = getWidth()/2 + CENTER_CIRCLE_RADIUS;
            float cy = getHeight()/2;
 
            canvas.drawCircle(cx,cy,SMALL_CIRCLE_RADIUS,mCirclePaint);
        }
        canvas.restore();
    }
}

注意事項(xiàng):

1、canvas.concat(mSpaceMatrix);對(duì)畫(huà)布的操作也會(huì)對(duì)后面進(jìn)行影響

2、Android中Matrix的set、pre、post的區(qū)別

說(shuō)set、pre、post的區(qū)別之前,先說(shuō)說(shuō)Matrix。

Matrix包含一個(gè)3 X 3的矩陣,專門(mén)用于圖像變換匹配。

Matrix提供了四種操作:

  • translate(平移)
  • rotate(旋轉(zhuǎn))
  • scale(縮放)
  • skew(傾斜)

也就是說(shuō)這4種操作都是對(duì)這個(gè)3 X 3的矩陣設(shè)值來(lái)達(dá)到變換的效果。

Matrix沒(méi)有結(jié)構(gòu)體,它必須被初始化,通過(guò)reset或set方法。

OK,Matrix介紹完了,我們來(lái)看看set、pre、post的區(qū)別。

pre是在隊(duì)列最前面插入,post是在隊(duì)列最后面追加,而set先清空隊(duì)列在添加(這也是上文提到的“Matrix沒(méi)有結(jié)構(gòu)體,它必須被初始化,通過(guò)reset或set方法”的原因)。

下面通過(guò)一些例子具體說(shuō)明:

  1. matrix.preScale(2f,1f);   
  2. matrix.preTranslate(5f, 0f);  
  3. matrix.postScale(0.2f, 1f);   
  4. matrix.postTranslate(0.5f, 0f); 

執(zhí)行順序:translate(5, 0) -> scale(2f, 1f) -> scale(0.2f, 1f) -> translate(0.5f, 0f)

  1. matrix.postTranslate(2f, 0f);  
  2. matrix.preScale(0.2f, 1f);    
  3. matrix.setScale(1f, 1f);  
  4. matrix.postScale(5f, 1f);  
  5. matrix.preTranslate(0.5f, 0f);  

執(zhí)行順序:translate(0.5f, 0f) -> scale(1f, 1f) -> scale(5f, 1)

到此這篇關(guān)于Android實(shí)現(xiàn)旋轉(zhuǎn)動(dòng)畫(huà)的兩種方式的文章就介紹到這了,更多相關(guān)android旋轉(zhuǎn)動(dòng)畫(huà)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論