您好,登錄后才能下訂單哦!
這篇文章給大家介紹Android中怎么自定義加載圈動畫效果,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
具體代碼如下:
package blog.csdn.net.mchenys.myanimationloading; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; 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.util.AttributeSet; import android.view.View; import android.view.animation.LinearInterpolator; import android.view.animation.OvershootInterpolator; /** * Created by mChenys on 2016/5/21. */ public class AnimationLoading extends View { private float mBigCircleRaduis = 90;//大圓的半徑 private float mSubCircleRadius = 20;//小圓的半徑 private PointF mBigCenterPoint;//大圓的圓心坐標 private Paint mBgPaint;//繪制背景的畫筆 private Paint mFgPaint;//繪制前景色的畫筆 private AnimatorTemplet mTemplet;//動畫模板 float mBigCircleRotateAngle;//大圓旋轉的角度 float mDiagonalDist;//屏幕對角線一半的距離 float mBgStrokeCircleRadius;//用于作為繪制背景空心圓的半徑 //6個小圓的顏色 private int[] colors = new int[]{Color.RED, Color.DKGRAY, Color.YELLOW, Color.BLUE, Color.LTGRAY, Color.GREEN}; public AnimationLoading(Context context) { this(context, null); } public AnimationLoading(Context context, AttributeSet attrs) { super(context, attrs); init(); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); //確定大圓的圓心坐標 mBigCenterPoint.x = w / 2f; mBigCenterPoint.y = h / 2f; //屏幕對角線的一半 mDiagonalDist = (float) (Math.sqrt(w * w + h * h) / 2); } private void init() { mBigCenterPoint = new PointF(); mFgPaint = new Paint(); mFgPaint.setAntiAlias(true); mBgPaint = new Paint(mFgPaint); mBgPaint.setColor(Color.WHITE); mBgPaint.setStyle(Paint.Style.STROKE); } @Override protected void onDraw(Canvas canvas) { if (null == mTemplet) { //開啟旋轉動畫 mTemplet = new RotateState(); } //傳遞Canvas對象 mTemplet.drawState(canvas); } /** * 繪制圓 * * @param canvas */ private void drawCircle(Canvas canvas) { //獲取每個小圓間隔的角度 float rotateAngle = (float) (2 * Math.PI / colors.length); for (int i = 0; i < colors.length; i++) { //每個小圓的實際角度 double angle = rotateAngle * i + mBigCircleRotateAngle; //這里加上大圓旋轉的角度是為了帶動小圓一起旋轉 //計算每個小圓的圓心坐標 float cx = (float) (mBigCircleRaduis * Math.cos(angle)) + mBigCenterPoint.x; float cy = (float) (mBigCircleRaduis * Math.sin(angle)) + mBigCenterPoint.y; //繪制6個小圓 mFgPaint.setColor(colors[i]); canvas.drawCircle(cx, cy, mSubCircleRadius, mFgPaint); } } /** * 繪制背景 * * @param canvas */ private void drawBackground(Canvas canvas) { if (mBgStrokeCircleRadius > 0f) { //不斷擴散的空心圓,空心圓的半徑為屏幕對角線的一半,空心圓的線寬則從線寬一半到0 float strokeWidth = mDiagonalDist - mBgStrokeCircleRadius;//線寬從對角線的1/2 ~ 0 mBgPaint.setStrokeWidth(strokeWidth); float radius = mBgStrokeCircleRadius + strokeWidth / 2;//半徑從對角線的1/4 ~ 1/2 canvas.drawCircle(mBigCenterPoint.x, mBigCenterPoint.y,radius , mBgPaint); } else { //繪制白色背景 canvas.drawColor(Color.WHITE); } } private abstract class AnimatorTemplet { abstract void drawState(Canvas canvas); } /** * 繪制旋轉動畫 */ private class RotateState extends AnimatorTemplet { ValueAnimator mValueAnimator; public RotateState() { //旋轉的過程,就是不斷的獲取大圓的角度,從0-2π mValueAnimator = ValueAnimator.ofFloat(0, (float) Math.PI * 2); mValueAnimator.setInterpolator(new LinearInterpolator());//勻速插值器 mValueAnimator.setDuration(1200); mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { //獲取大圓旋轉的角度 mBigCircleRotateAngle = (float) animation.getAnimatedValue(); //重繪 invalidate(); } }); mValueAnimator.setRepeatCount(ValueAnimator.INFINITE);//無限循環 mValueAnimator.start(); } /** * 停止旋轉動畫,在數據加載完畢后供外部調用 */ public void stopRotate() { mValueAnimator.cancel(); } @Override void drawState(Canvas canvas) { drawBackground(canvas); drawCircle(canvas); } } /** * 繪制聚合動畫 */ private class MergingState extends AnimatorTemplet { public MergingState() { //聚合的過程,就是不斷的改變大圓的半徑,從mBigCircleRaduis~0 ValueAnimator valueAnimator = ValueAnimator.ofFloat(mBigCircleRaduis, 0); valueAnimator.setInterpolator(new OvershootInterpolator(10f));//彈性插值器 valueAnimator.setDuration(600); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { //獲取大圓變化的半徑 mBigCircleRaduis = (float) animation.getAnimatedValue(); //重繪 invalidate(); } }); valueAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { //聚合執行完后進入下一個擴散動畫 mTemplet = new SpreadState(); } }); valueAnimator.start(); } @Override void drawState(Canvas canvas) { drawBackground(canvas); drawCircle(canvas); } } /** * 繪制擴散動畫 */ private class SpreadState extends AnimatorTemplet { public SpreadState() { //擴散的過程,就是不斷的改變背景畫繪制空心圓的半徑,從0~mDiagonalDist ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, mDiagonalDist); valueAnimator.setDuration(600); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { //獲取大圓變化的半徑 mBgStrokeCircleRadius = (float) animation.getAnimatedValue(); //重繪 invalidate(); } }); valueAnimator.start(); } @Override void drawState(Canvas canvas) { drawBackground(canvas); } } /** * 停止加載動畫 */ public void stopLoading() { if (null != mTemplet && mTemplet instanceof RotateState) { ((RotateState) mTemplet).stopRotate(); //開啟下一個聚合動畫 post(new Runnable() { @Override public void run() { mTemplet = new MergingState(); } }); } } }
測試的Activity
package blog.csdn.net.mchenys.myanimationloading; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.widget.FrameLayout; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FrameLayout content = new FrameLayout(this); content.setOnClickListener(null); ImageView bg = new ImageView(this); bg.setImageResource(R.drawable.fg); bg.setScaleType(ImageView.ScaleType.FIT_XY); content.addView(bg); final AnimationLoading loading = new AnimationLoading(this); content.addView(loading); setContentView(content); new Handler().postDelayed(new Runnable() { @Override public void run() { //3s后停止加載動畫 loading.stopLoading(); } },3000); } }
關于Android中怎么自定義加載圈動畫效果就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。