您好,登錄后才能下訂單哦!
這篇文章主要介紹“Android實現(xiàn)旋轉(zhuǎn)動畫的方式代碼分享”,在日常操作中,相信很多人在Android實現(xiàn)旋轉(zhuǎn)動畫的方式代碼分享問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Android實現(xiàn)旋轉(zhuǎn)動畫的方式代碼分享”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
練習(xí)案例
效果展示
前期準(zhǔn)備
自定義 View java代碼編寫
方法一
方法二
易錯點總結(jié):
視差動畫 - 雅虎新聞?wù)虞d
第一步:準(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>
關(guān)鍵思想: 屬性動畫 + 計算圓心
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)間隔時間 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); //初始化畫筆 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è)置屬性動畫,讓小圓轉(zhuǎn)起來 //這里得注意,是個坑,你如果不判斷那球就不會動 因為會陷入死循環(huán) 值動畫將值設(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(); } //計算每個小球的間隔 double spaceAngle = Math.PI*2/mColorArray.length; for (int i = 0; i < mColorArray.length; i++) { //為 每個球 畫筆 設(shè)置顏色 mCirclePaint.setColor(mColorArray[i]); //利用 勾股定理 計算 小圓 中心點 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)畫布法
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)間隔時間 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); //初始化畫筆 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è)置屬性動畫,讓小圓轉(zhuǎn)起來 //這里得注意,是個坑,你如果不判斷那球就不會動 因為會陷入死循環(huán) 值動畫將值設(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(); } //計算每個小球的間隔 // double spaceAngle = Math.PI*2/mColorArray.length; double spaceAngle = 360.0d/mColorArray.length; System.out.println("spaceAngle -> " + spaceAngle); //利用旋轉(zhuǎn)畫布法 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); //為 每個球 畫筆 設(shè)置顏色 mCirclePaint.setColor(mColorArray[i]); //利用 勾股定理 計算 小圓 中心點 //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)畫布法 float cx = getWidth()/2 + CENTER_CIRCLE_RADIUS; float cy = getHeight()/2; canvas.drawCircle(cx,cy,SMALL_CIRCLE_RADIUS,mCirclePaint); } canvas.restore(); } }
1、canvas.rotate(mCurrentSingle,getWidth()/2,getHeight()/2);中 第一個參數(shù)傳的是角度(360度的那種),而 Math.cos();中 參數(shù)傳的是一個弧度(2π的那種)
2、canvas.rotate() 函數(shù)執(zhí)行之后對后續(xù)畫布上的操作都是有影響的,所以,得配合 canvas.save();和 canvas.restore();使用。因此,里面的canvas.rotate((float) spaceAngle,getWidth()/2,getHeight()/2);中spaceAngle不能乘 i 。
3、畫布的旋轉(zhuǎn)除了 canvas.rotate() 函數(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); //畫布旋轉(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)間隔時間 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); //初始化畫筆 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è)置屬性動畫,讓小圓轉(zhuǎn)起來 //這里得注意,是個坑,你如果不判斷那球就不會動 因為會陷入死循環(huán) 值動畫將值設(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(); } //計算每個小球的間隔 // 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)畫布法 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); //為 每個球 畫筆 設(shè)置顏色 mCirclePaint.setColor(mColorArray[i]); //利用 勾股定理 計算 小圓 中心點 //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)畫布法 float cx = getWidth()/2 + CENTER_CIRCLE_RADIUS; float cy = getHeight()/2; canvas.drawCircle(cx,cy,SMALL_CIRCLE_RADIUS,mCirclePaint); } canvas.restore(); } }
注意事項:
1、canvas.concat(mSpaceMatrix);對畫布的操作也會對后面進(jìn)行影響
2、Android中Matrix的set、pre、post的區(qū)別
說set、pre、post的區(qū)別之前,先說說Matrix。
Matrix包含一個3 X 3的矩陣,專門用于圖像變換匹配。
Matrix提供了四種操作:
translate(平移)
rotate(旋轉(zhuǎn))
scale(縮放)
skew(傾斜)
也就是說這4種操作都是對這個3 X 3的矩陣設(shè)值來達(dá)到變換的效果。
Matrix沒有結(jié)構(gòu)體,它必須被初始化,通過reset或set方法。
OK,Matrix介紹完了,我們來看看set、pre、post的區(qū)別。
pre是在隊列最前面插入,post是在隊列最后面追加,而set先清空隊列在添加(這也是上文提到的“Matrix沒有結(jié)構(gòu)體,它必須被初始化,通過reset或set方法”的原因)。
下面通過一些例子具體說明:
matrix.preScale(2f,1f);
matrix.preTranslate(5f, 0f);
matrix.postScale(0.2f, 1f);
matrix.postTranslate(0.5f, 0f);
執(zhí)行順序:translate(5, 0) -> scale(2f, 1f) -> scale(0.2f, 1f) -> translate(0.5f, 0f)
matrix.postTranslate(2f, 0f);
matrix.preScale(0.2f, 1f);
matrix.setScale(1f, 1f);
matrix.postScale(5f, 1f);
matrix.preTranslate(0.5f, 0f);
執(zhí)行順序:translate(0.5f, 0f) -> scale(1f, 1f) -> scale(5f, 1)
到此,關(guān)于“Android實現(xiàn)旋轉(zhuǎn)動畫的方式代碼分享”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。