Android加载动画系列——CircularLoadingAnim

来源:互联网 发布:无忧传奇挂机软件 编辑:程序博客网 时间:2024/06/09 15:05

Android加载动画系列——CircularLoadingAnim

       小编在逛掘金社区的时候,一不小心看到了一篇讲加载动画的文章,于是点进去看了看,被这些炫酷的加载动画深深地吸引了,于是小编觉得有必要自己动手记录一下这些炫酷的动画,顺便丰富一下自己的学习笔记。

       让我们先来看看效果图:


       在此我就不做过多的分析,直接上源码。

1、CircularLoadingAnim.java源码如下:

public class CircularLoadingAnim extends View {    private Paint mPaint;    private float mWidth = 0f;    private float mAnimatedValue = 0f;    private int mStartAngle = 0;    private float mMaxRadius = 4;    RotateAnimation mProgerssRotateAnim;    public CircularLoadingAnim(Context context) {        this(context, null);    }    public CircularLoadingAnim(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public CircularLoadingAnim(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        initPaint();    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        if (getMeasuredWidth() > getHeight())            mWidth = getMeasuredHeight();        else            mWidth = getMeasuredWidth();        mMaxRadius = mWidth / 30f;    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        for (int i = 0; i < 9; i++) {            float x2 = (float) ((mWidth / 2.f - mMaxRadius) * Math.cos(mStartAngle + 45f * i * Math.PI / 180f));            float y2 = (float) ((mWidth / 2.f - mMaxRadius) * Math.sin(mStartAngle + 45f * i * Math.PI / 180f));            canvas.drawCircle(mWidth / 2.f - x2, mWidth / 2.f - y2, mMaxRadius, mPaint);        }        canvas.drawCircle(mWidth / 2.f, mWidth / 2.f, mWidth / 2.f - mMaxRadius * 6, mPaint);    }    private void initPaint() {        mPaint = new Paint();        mPaint.setAntiAlias(true);        mPaint.setStyle(Paint.Style.FILL);        mPaint.setColor(Color.WHITE);                mProgerssRotateAnim = new RotateAnimation(0f, 360f, android.view.animation.Animation.RELATIVE_TO_SELF,                0.5f, android.view.animation.Animation.RELATIVE_TO_SELF, 0.5f);        mProgerssRotateAnim.setRepeatCount(-1);        mProgerssRotateAnim.setInterpolator(new LinearInterpolator());//不停顿        mProgerssRotateAnim.setFillAfter(true);//停在最后    }    public void startAnim() {        stopAnim();        mProgerssRotateAnim.setDuration(3500);        startAnimation(mProgerssRotateAnim);    }    public void stopAnim() {        clearAnimation();    }}

 

2、接下来就是如何使用的问题,首先我们需要在layout中引用自定义的动画控件,如下所示:

<com.cyril.loadinganim.CircularLoadingAnim    android:id="@+id/circular"    android:layout_width="50dp"    android:layout_height="50dp" />

 

3、然后在相关的Activity中实现动画的播放和停止,使用事例如下:

circularLoadingAnim = (CircularLoadingAnim) findViewById(R.id.circular);circularLoadingAnim.startAnim();
 

4、  最后小编双手奉上源码的下载地址:http://download.csdn.net/detail/zhimingshangyan/9582830

 

1 0