您好,登錄后才能下訂單哦!
怎么在Android中實現一個仿微軟系統加載動畫效果?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
初始化五個圓球分別設置中心點,方便畫圓
利用ValueAnimator的值變化來獲取旋轉角度
onDraw來分別畫每個圓
package com.sjl.keeplive.track; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PointF; public class Circle { private PointF center; private float radius; public Circle() { center = new PointF(); } /** * 設置圓球半徑 */ public void setRadius(float radius) { this.radius = radius; } /** * 設置中心點 * * @param x * @param y */ public void setCenter(float x, float y) { center.set(x, y); } public void draw(Canvas canvas, Paint paint) { canvas.drawCircle(center.x,center.y,radius,paint); } }
package com.sjl.keeplive.track; import android.animation.ValueAnimator; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PointF; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View; public class MinSoftLoadingView extends View { private int circleCount = 5; private Circle[] circles; private Paint paint; private int width; private int height; private PointF center; private float circleRadius; private float[] rotates; private float roateRadius; public MinSoftLoadingView(Context context) { this(context, null); } public MinSoftLoadingView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public MinSoftLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.WHITE); //每個點旋轉的角度 rotates = new float[circleCount]; } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); width = getWidth(); height = getHeight(); center = new PointF(width / 2.0f, height / 2.0f); //旋轉掃描半徑 roateRadius = Math.min(width, height); //圓球最大的那個半徑 circleRadius = roateRadius / 10.0f; initCircle(); } /** * 初始化5個圓球 */ private void initCircle() { circles = new Circle[circleCount]; for (int i = 0; i < circleCount; i++) { circles[i] = new Circle(); circles[i].setCenter(center.x, center.y - roateRadius / 2 + circleRadius); circles[i].setRadius(circleRadius - circleRadius * i / 5); } //開始執行動畫 startAnimation(); } private void startAnimation() { for (int i = 0; i < circleCount; i++) { final int index = i; ValueAnimator animator = ValueAnimator.ofFloat(0, 360); animator.setRepeatCount(ValueAnimator.INFINITE);//重復 animator.setDuration(2000); animator.setStartDelay(index * 100); //每一個隨后的延遲時間 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { rotates[index] = (float) animation.getAnimatedValue(); invalidate(); } }); animator.start(); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); for (int i = 0; i < circleCount; i++) { canvas.save(); canvas.rotate(rotates[i], center.x, center.y); circles[i].draw(canvas, paint); canvas.restore(); } } }
<com.sjl.keeplive.track.MinSoftLoadingView android:layout_width="60dp" android:layout_height="60dp" android:layout_gravity="center"/>
關于怎么在Android中實現一個仿微軟系統加載動畫效果問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。