博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android--自定义加载框
阅读量:6307 次
发布时间:2019-06-22

本文共 9042 字,大约阅读时间需要 30 分钟。

1,在网上看了下好看的加载框,看了一下,挺好看的,再看了下源码,就是纯paint画出来的,再加上属性动画就搞定了

再来看一下我们的源码

LvGhost.java

package com.qianmo.retrofitdemo;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.Path;import android.graphics.RectF;import android.util.AttributeSet;import android.view.View;import android.view.animation.LinearInterpolator;/** * Created by lumingmin on 16/6/29. */public class LVGhost extends View {    float mWidth = 0f;    float mHight = 0f;    Paint mPaint, mPaintHand, mPaintShadow, mPaintArms;    RectF rectFGhost = new RectF();    RectF rectFGhostShadow = new RectF();    float mPadding = 0f;    int mskirtH = 0;    Path path = new Path();    public LVGhost(Context context) {        this(context, null);    }    public LVGhost(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public LVGhost(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        initPaint();    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        mWidth = getMeasuredWidth();        mHight = getMeasuredHeight();        mPadding = 10;        mskirtH = (int) (mWidth / 40);    }    private void initPaint() {        mPaint = new Paint();        mPaint.setAntiAlias(true);        mPaint.setStyle(Paint.Style.FILL);        mPaint.setColor(Color.WHITE);        mPaintHand = new Paint();        mPaintHand.setAntiAlias(true);        mPaintHand.setStyle(Paint.Style.FILL);        mPaintHand.setColor(Color.argb(220, 0, 0, 0));        mPaintShadow = new Paint();        mPaintShadow.setAntiAlias(true);        mPaintShadow.setStyle(Paint.Style.FILL);        mPaintShadow.setColor(Color.argb(60, 0, 0, 0));        mPaintArms = new Paint();        mPaintArms.setAntiAlias(true);        mPaintArms.setStrokeWidth(8);        mPaintArms.setStyle(Paint.Style.FILL);        mPaintArms.setColor(Color.argb(150, 0, 0, 0));        startAnim();    }    private void drawShadow(Canvas canvas) {        canvas.drawArc(rectFGhostShadow, 0, 360, false, mPaintShadow);    }    private void drawHead(Canvas canvas) {        canvas.drawCircle(rectFGhost.left + rectFGhost.width() / 2                , rectFGhost.width() / 2 + rectFGhost.top                , rectFGhost.width() / 2 - 15                , mPaint        );    }    private void drawHand(Canvas canvas) {        canvas.drawCircle(rectFGhost.left + rectFGhost.width() / 2 - mskirtH * 3 / 2 + mskirtH * onAnimationRepeatFlag                , rectFGhost.width() / 2 + mskirtH + rectFGhost.top,                mskirtH * 0.9f, mPaintHand        );        canvas.drawCircle(rectFGhost.left + rectFGhost.width() / 2 + mskirtH * 3 / 2 + mskirtH * onAnimationRepeatFlag                , rectFGhost.width() / 2 + mskirtH + rectFGhost.top,                mskirtH * 0.9f, mPaintHand        );    }    float wspace = 10f;    float hspace = 10f;    private void drawBody(Canvas canvas) {        path.reset();        float x = (float) ((rectFGhost.width() / 2 - 15) * Math.cos(5 * Math.PI / 180f));        float y = (float) ((rectFGhost.width() / 2 - 15) * Math.sin(5 * Math.PI / 180f));        float x2 = (float) ((rectFGhost.width() / 2 - 15) * Math.cos(175 * Math.PI / 180f));        float y2 = (float) ((rectFGhost.width() / 2 - 15) * Math.sin(175 * Math.PI / 180f));        path.moveTo(rectFGhost.left + rectFGhost.width() / 2 - x, rectFGhost.width() / 2 - y + rectFGhost.top);        path.lineTo(rectFGhost.left + rectFGhost.width() / 2 - x2, rectFGhost.width() / 2 - y2 + rectFGhost.top);        path.quadTo(rectFGhost.right + wspace / 2, rectFGhost.bottom                , rectFGhost.right - wspace, rectFGhost.bottom - hspace);        float a = mskirtH;//(mskirtH/2);        float m = (rectFGhost.width() - 2 * wspace) / 7f;        for (int i = 0; i < 7; i++) {            if (i % 2 == 0) {                path.quadTo(rectFGhost.right - wspace - m * i - (m / 2), rectFGhost.bottom - hspace - a                        , rectFGhost.right - wspace - (m * (i + 1)), rectFGhost.bottom - hspace);            } else {                path.quadTo(rectFGhost.right - wspace - m * i - (m / 2), rectFGhost.bottom - hspace + a                        , rectFGhost.right - wspace - (m * (i + 1)), rectFGhost.bottom - hspace);            }        }        path.quadTo(rectFGhost.left - 5, rectFGhost.bottom                , rectFGhost.left + rectFGhost.width() / 2 - x, rectFGhost.width() / 2 - y + rectFGhost.top);        path.close();        canvas.drawPath(path, mPaint);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        canvas.save();        float distance = (mWidth - 2 * mPadding) / 3 * 2 * mAnimatedValue;        rectFGhost.left = mPadding + distance;        rectFGhost.right = (mWidth - 2 * mPadding) / 3 + distance;        float moveY = 0f;        float moveYMax = mHight / 4f / 2f;        float shadowHighMax = 5f;        float shadowHigh = 0f;        if (mAnimatedValue <= 0.25) {            moveY = (float) (moveYMax / 0.25 * mAnimatedValue);            rectFGhost.top = moveY;            rectFGhost.bottom = mHight / 4 * 3 + moveY;            shadowHigh = shadowHighMax / 0.25f * mAnimatedValue;        } else if (mAnimatedValue > 0.25 && mAnimatedValue <= 0.5f) {            moveY = (float) (moveYMax / 0.25 * (mAnimatedValue - 0.25f));            rectFGhost.top = moveYMax - moveY;            rectFGhost.bottom = mHight / 4 * 3 + moveYMax - moveY;            shadowHigh = shadowHighMax - shadowHighMax / 0.25f * (mAnimatedValue - 0.25f);        } else if (mAnimatedValue > 0.5 && mAnimatedValue <= 0.75f) {            moveY = (float) (moveYMax / 0.25 * (mAnimatedValue - 0.5f));            rectFGhost.top = moveY;            rectFGhost.bottom = mHight / 4 * 3 + moveY;            shadowHigh = shadowHighMax / 0.25f * (mAnimatedValue - 0.5f);        } else if (mAnimatedValue > 0.75 && mAnimatedValue <= 1f) {            moveY = (float) (moveYMax / 0.25 * (mAnimatedValue - 0.75f));            rectFGhost.top = moveYMax - moveY;            rectFGhost.bottom = mHight / 4 * 3 + moveYMax - moveY;            shadowHigh = shadowHighMax - shadowHighMax / 0.25f * (mAnimatedValue - 0.75f);        }        rectFGhostShadow.top = mHight - 25 + shadowHigh;        rectFGhostShadow.bottom = mHight - 5 - shadowHigh;        rectFGhostShadow.left = rectFGhost.left + 5 + shadowHigh * 3;        rectFGhostShadow.right = rectFGhost.right - 5 - shadowHigh * 3;        drawShadow(canvas);        drawHead(canvas);        drawBody(canvas);        drawHand(canvas);        canvas.restore();    }    public void startAnim() {        stopAnim();        startViewAnim(0f, 1f, 2500);    }    private ValueAnimator valueAnimator;    private float mAnimatedValue = 0.f;    public void stopAnim() {        if (valueAnimator != null) {            clearAnimation();            valueAnimator.setRepeatCount(0);            valueAnimator.cancel();            valueAnimator.end();            mAnimatedValue = 0f;            wspace = 10;            onAnimationRepeatFlag = 1;            postInvalidate();        }    }    int onAnimationRepeatFlag = 1;    private ValueAnimator startViewAnim(float startF, final float endF, long time) {        valueAnimator = ValueAnimator.ofFloat(startF, endF);        valueAnimator.setDuration(time);        valueAnimator.setInterpolator(new LinearInterpolator());        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);//无限循环        valueAnimator.setRepeatMode(ValueAnimator.REVERSE);        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {            @Override            public void onAnimationUpdate(ValueAnimator valueAnimator) {                mAnimatedValue = (float) valueAnimator.getAnimatedValue();                invalidate();            }        });        valueAnimator.addListener(new AnimatorListenerAdapter() {            @Override            public void onAnimationEnd(Animator animation) {                super.onAnimationEnd(animation);            }            @Override            public void onAnimationStart(Animator animation) {                super.onAnimationStart(animation);            }            @Override            public void onAnimationRepeat(Animator animation) {                super.onAnimationRepeat(animation);                onAnimationRepeatFlag = onAnimationRepeatFlag * -1;                if (onAnimationRepeatFlag == -1) {                    wspace = 22;                } else {                    wspace = -2;                }            }        });        if (!valueAnimator.isRunning()) {            wspace = -2;            valueAnimator.start();        }        return valueAnimator;    }}

在我们的布局文件中使用一下

这样就可以简单的使用了

  

转载于:https://www.cnblogs.com/wjtaigwh/p/6029512.html

你可能感兴趣的文章
用sqlplus远程连接oracle命令
查看>>
多年一直想完善的自由行政审批流程组件【2002年PHP,2008年.NET,2010年完善数据设计、代码实现】...
查看>>
自动生成四则运算题目
查看>>
【翻译】使用新的Sencha Cmd 4命令app watch
查看>>
【前台】【单页跳转】整个项目实现单页面跳转,抛弃iframe
查看>>
因为你是前端程序员!
查看>>
数据库设计中的14个技巧
查看>>
Android学习系列(5)--App布局初探之简单模型
查看>>
git回退到某个历史版本
查看>>
ecshop
查看>>
HTML5基础(二)
查看>>
在GCE上安装Apache、tomcat等
查看>>
在Mac 系统下进行文件的显示和隐藏
查看>>
ue4(c++) 按钮中的文字居中的问题
查看>>
技能点
查看>>
读书笔记《乌合之众》
查看>>
Hadoop日记Day1---Hadoop介绍
查看>>
iOS 学习资料汇总
查看>>
centos7 yum安装jdk
查看>>
Bluedroid与BluZ,蓝牙测试方法的变动(基于bludroid和BlueZ的对比)
查看>>