自定义控件(progresses)(360手机助手下载进度示例)

概述

Android要想获得酷炫的效果或者良好的用户体验,自定义控件是不可避免的。通常自定义控件的方式可以概况为以下三种或者它们的组合:

1,通过继承View或者View的子类实现。
2,通过继承ViewGroup或者ViewGroup的子类实现。
3,通过继承Drawable对象来实现。

progress控件是android最常用的控件之一,下面我们来实现一个类似360手机助手下载应用时的进度控件。实现后的效果图如下,我们看到这个进度条包括了多个进度(一个不断闪动的加速进度,一个缓慢的进度,一个快速的进度)。之所以设计成这么变态可能是想给用户一种快和动态的体验吧。


ezgif.com-a4296aa51f.gif

示例详解:

先来看下代码结构,其实本文主要涉及了两个类:ProgressView,LinearProgressDrawable,一个xml文件:attrs.xml.至于CircularProgressDrawable.java,WaveProgressDrawable.java是另外两种进度条,圆形进度条和水波进度条。代码结构来看实现了三种样式的进度,但本文主要讲解下直线型的进度。


Paste_Image.png
ProgressView此类作用是控件的初始化,进度的获取和设置接口提供。

1,applyStyle方法主要初始化控件信息,这些属性在attrs.xml中配置,由调用者在layout.xml中传入。
2,onVisibilityChanged是progressView的可见状态发生变化(setVisibility)后发生回调。在这个方法中我们可以动画的开始和结束。
3,onAttachedToWindow,onDetachedFromWindow 视图被添加到窗口上或者移除时调用。在这两个方法中可以做一些资源的创建和释放工作。
4,ProgressView完整代码如下:

package com.wayne.android.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import com.wayne.android.drawable.CircularProgressDrawable;
import com.wayne.android.drawable.LinearProgressDrawable;
import com.wayne.android.drawable.WaveProgressDrawable;

public class ProgressView extends View {

    public static final int MODE_DETERMINATE = 0;//有准确进度的
    public static final int MODE_INDETERMINATE = 1;//没有准确进度

    public static final String STYLE_CIRCULAR = "circular";//圆形进度条
    public static final String STYLE_LINEAR   = "linear";//直线型进度条
    public static final String STYLE_WAVE     = "wave";//波浪型进度条

    private Drawable progressDrawable;

    private String progressStyle;

    private int progressMode;

    public ProgressView(Context context) {
        super(context);
        init(context, null, 0);
    }

    public ProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    @Override
    protected void onVisibilityChanged(View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if (changedView != this) {
            return;
        }
        if (visibility == View.VISIBLE) {
            start();
        } else {
            stop();
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        start();
    }

    @Override
    protected void onDetachedFromWindow() {
        android.util.Log.i("wayne_test", "onDetachedFromWindow");
        super.onDetachedFromWindow();
        stop();
    }
    
    //初始化
    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        applyStyle(context, attrs, defStyleAttr, 0);
    }
    

    private void applyStyle(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        int progress = 0;
        int secondaryProgress = 0;
        //读取xml中传入的样式
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProgressView, defStyleAttr, defStyleRes);
        for (int i = 0, count = a.getIndexCount(); i< count; i++) {
            int attr = a.getIndex(i);
            if (attr == R.styleable.ProgressView_pv_mode) {
                progressMode = a.getInteger(attr, 0);
            } else if (attr == R.styleable.ProgressView_pv_progress) {
                progress = a.getInteger(attr, 0);
            } else if (attr == R.styleable.ProgressView_pv_secondary_progress) {
                secondaryProgress = a.getInteger(attr, 0);
            } else if (attr == R.styleable.ProgressView_pv_style) {
                progressStyle = a.getString(attr);
            }
        }
        a.recycle();
       //设置初始样式
        if (progressDrawable == null) {
            if (STYLE_CIRCULAR.equals(progressStyle)) {
                progressDrawable = new CircularProgressDrawable();
            } else if (STYLE_LINEAR.equals(progressStyle)) {
                progressDrawable = new LinearProgressDrawable();
            } else if (STYLE_WAVE.equals(progressStyle)) {
                progressDrawable = new WaveProgressDrawable();
            } else {
                throw new IllegalArgumentException("invalid  progressStyle, progressStyle:" + progressStyle);
            }

            //关联View和Drawable对象
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                this.setBackground(progressDrawable);
            } else {
                this.setBackgroundDrawable(progressDrawable);
            }
        }
       //设置初始模式
        if (STYLE_CIRCULAR.equals(progressStyle)) {
            ((CircularProgressDrawable)progressDrawable).setProgressMode(progressMode);
        } else if (STYLE_LINEAR.equals(progressStyle)) {
            ((LinearProgressDrawable)progressDrawable).setProgressMode(progressMode);
        } else if (STYLE_WAVE.equals(progressStyle)) {
            ((WaveProgressDrawable)progressDrawable).setProgressMode(progressMode);
        }
        //设置初始进度
        if(progress >= 0) {
            setProgress(progress);
        }

        if(secondaryProgress >= 0) {
            setSecondaryProgress(secondaryProgress);
        }
    }

    private void start() {
        ((Animatable)progressDrawable).start();
    }

    private void stop() {
        ((Animatable)progressDrawable).stop();
    }

    public int getProgress() {
        if (STYLE_CIRCULAR.equals(progressStyle)) {
            return ((CircularProgressDrawable)progressDrawable).getProgress();
        } else if (STYLE_LINEAR.equals(progressStyle)) {
            return ((LinearProgressDrawable)progressDrawable).getProgress();
        } else if (STYLE_WAVE.equals(progressStyle)) {
            return ((WaveProgressDrawable)progressDrawable).getProgress();
        } else {
            return 0;
        }
    }

    public void setProgress(int percent) {
        if (STYLE_CIRCULAR.equals(progressStyle)) {
             ((CircularProgressDrawable)progressDrawable).setProgress(percent);
        } else if (STYLE_LINEAR.equals(progressStyle)) {
             ((LinearProgressDrawable)progressDrawable).setProgress(percent);
        } else if (STYLE_WAVE.equals(progressStyle)) {
             ((WaveProgressDrawable)progressDrawable).setProgress(percent);
        }
    }

    public int getSecondaryProgress() {
        if (STYLE_CIRCULAR.equals(progressStyle)) {
            return ((CircularProgressDrawable)progressDrawable).getSecondaryProgress();
        } else if (STYLE_LINEAR.equals(progressStyle)) {
            return ((LinearProgressDrawable)progressDrawable).getSecondaryProgress();
        } else if (STYLE_WAVE.equals(progressStyle)) {
            return ((WaveProgressDrawable)progressDrawable).getSecondaryProgress();
        } else {
            return 0;
        }
    }

    public void setSecondaryProgress(int percent) {
        if (STYLE_CIRCULAR.equals(progressStyle)) {
            ((CircularProgressDrawable)progressDrawable).setSecondaryProgress(percent);
        } else if (STYLE_LINEAR.equals(progressStyle)) {
            ((LinearProgressDrawable)progressDrawable).setSecondaryProgress(percent);
        } else if (STYLE_WAVE.equals(progressStyle)) {
            ((WaveProgressDrawable)progressDrawable).setSecondaryProgress(percent);
        }
    }

}

LinearProgressDrawable 主要作用绘制进度条,实现进度条动画。此类是这次自定义进度条控件的核心类。通过重写drawable方法里面的draw方法,我们可以实现不同样式的进度效果。通过实现Animatable的方法我们可以对动画的开启和关闭进行控制。

LinearProgressDrawable 完整代码如下:

package com.wayne.android.drawable;

import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.view.animation.AccelerateInterpolator;

import com.wayne.android.widget.ProgressView;

public class LinearProgressDrawable extends Drawable implements Animatable {

    private int mProgressMode;

    private Path mPath;

    private Paint mPaint;

    private float mProgressPercent;

    private float mSecondaryProgressPercent;

    private float mStartLine;

    private float mLineWidth;

    private ValueAnimator valueAnimator;

    private Paint lpaint;

    private boolean isRunningFlag = false;

    public LinearProgressDrawable() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeJoin(Paint.Join.ROUND);

        mPath = new Path();
        lpaint = new Paint();
        lpaint.setAlpha(125);
    }
    
    //开启不断闪动的进度动画
    @Override
    public void start() {
        if (isRunning()) {
            return;
        }
        isRunningFlag = true;
        valueAnimator = ValueAnimator.ofFloat(0f, 1f);
        valueAnimator.setRepeatCount(-1);
        valueAnimator.setDuration(800);
        valueAnimator.setInterpolator(new AccelerateInterpolator());
        valueAnimator.setRepeatMode(ValueAnimator.RESTART);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                mStartLine = getBounds().width() *((float)valueAnimator.getAnimatedValue());
                invalidateSelf();
            }
        });
        valueAnimator.start();
    }
  //关闭动画
    @Override
    public void stop() {
        if (valueAnimator != null && isRunning()) {
            valueAnimator.cancel();
        }
        isRunningFlag = false;
    }


    @Override
    public boolean isRunning() {
        return isRunningFlag;
    }

    @Override
    public void draw(Canvas canvas) {
        switch (mProgressMode) {
            case ProgressView.MODE_DETERMINATE:
                drawDeterminate(canvas);
                break;
            case ProgressView.MODE_INDETERMINATE:
                drawIndeterminate(canvas);
                break;
        }
    }

    private void drawDeterminate(Canvas canvas) {
        Rect bounds = getBounds();
        int width = bounds.width();
        float y = bounds.height()/2;
        float size = 12f;

        float lineWidth = width * mProgressPercent;
        float secondLineWidth = width*mSecondaryProgressPercent;
        android.util.Log.i("wayne_test", "mProgressPercent:" + mProgressPercent + " mSecondaryProgressPercent:" + mSecondaryProgressPercent);
        mPaint.setStrokeWidth(size);
        mPaint.setStyle(Paint.Style.STROKE);

        if (mProgressPercent == 0) {
            start();
        }

        if (mProgressPercent == 1) {
            stop();
        }
        //绘制进度条底色
        if(mProgressPercent != 1f){
            mPaint.setColor(Color.parseColor("#E3F2FD"));
            canvas.drawLine(lineWidth, y, width, y, mPaint);
        }
        //绘制较快的进度条
        if (mSecondaryProgressPercent != 0f) {
            mPaint.setColor(Color.parseColor("#BBDEFB"));
            drawLinePath(canvas, 0, y, secondLineWidth, y, mPaint);
        }
        //绘制较慢的进度条
        if(mProgressPercent != 0f){
            mPaint.setColor(Color.parseColor("#2196F3"));
            drawLinePath(canvas, 0, y, lineWidth, y, mPaint);
        }
        
        //绘制最上面闪烁的进度
        if (mProgressPercent != 1f && mProgressPercent != 0f) {

            lpaint.setStrokeWidth(size);
            lpaint.setStyle(Paint.Style.STROKE);
            lpaint.setAlpha(160);
            float endline = Math.min(lineWidth, mStartLine + width/8);
            float statline =  Math.min(mStartLine, endline);
            LinearGradient gradient = new LinearGradient(statline, y, endline, y, Color.parseColor("#2196F3"), Color.WHITE, Shader.TileMode.REPEAT);
            lpaint.setShader(gradient);
            drawLinePath(canvas, statline, y, endline, y, lpaint);
        }
    }

    private void drawIndeterminate(Canvas canvas) {
        Rect bounds = getBounds();
        int width = bounds.width();
        float y = bounds.height()/2;
        float size = 12f;

        mPaint.setStrokeWidth(size);
        mPaint.setStyle(Paint.Style.STROKE);

        mPaint.setColor(Color.BLUE);
        canvas.drawLine(0, y, width, y, mPaint);

        mPaint.setColor(Color.RED);
        mLineWidth = width/6;
        float endline = Math.min(width, mStartLine + mLineWidth);
        drawLinePath(canvas, mStartLine, y, endline, y, mPaint);
    }

    private void drawLinePath(Canvas canvas, float x1, float y1, float x2, float y2, Paint paint){
        mPath.reset();
        mPath.moveTo(x1, y1);//线条的起点
        mPath.lineTo(x2, y2);//线条的终点
        canvas.drawPath(mPath, paint);
    }

    @Override
    public void setAlpha(int i) {
        mPaint.setAlpha(i);
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {
        mPaint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    public int getProgress() {
        return (int)(mProgressPercent*100);
    }

    public void setProgress(int progress) {
        float fpercent = Math.min(1f, Math.max(0f, progress/100f));
        if(mProgressPercent != fpercent) {
            mProgressPercent = fpercent;
            invalidateSelf();
        }
    }

    public int getSecondaryProgress() {
        return (int)(mSecondaryProgressPercent*100);
    }

    public void setSecondaryProgress(int progress) {
        float fpercent = Math.min(1f, Math.max(0f, progress/100f));
        if(mSecondaryProgressPercent != fpercent) {
            mSecondaryProgressPercent = fpercent;
            invalidateSelf();
        }
    }

    public void setProgressMode(int progressMode) {
        if(mProgressMode != progressMode) {
            mProgressMode = progressMode;
            invalidateSelf();
        }
    }
}
attrs.xml 文件中对属性进行定义,用户在布局文件中可以传入给控件。

attrs的完整代码如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ProgressView">
        <attr name="pv_mode" format="integer"></attr>
        <attr name="pv_progress" format="integer"></attr>
        <attr name="pv_secondary_progress" format="integer"></attr>
        <attr name="pv_style" format="string"></attr>
    </declare-styleable>
</resources>
ProgressActivity 测试进度条控件是否正常。
package com.wayne.android.testcancaspath;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.wayne.android.widget.ProgressView;

public class ProgressActivity extends AppCompatActivity implements Handler.Callback{

    private static final int MSG_PROGRESS = 0;
    private static final int MSG_START = 1;
    private static final int MSG_STOP = 2;
    private static final int MSG_SPROGRESS = 3;

    private ProgressView progressView;
    private Handler mHandler;
    private boolean isStop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progress_layout);
        progressView = (ProgressView)findViewById(R.id.progress_view_id);
        mHandler = new Handler(this);
        isStop = false;
    }

    @Override
    public boolean handleMessage(Message message) {
        switch (message.what) {
            case MSG_START:
                progressView.setVisibility(View.VISIBLE);
                break;
            case MSG_STOP:
                progressView.setVisibility(View.GONE);
                break;
            case MSG_PROGRESS:
                progressView.setProgress(message.arg1);
                break;
            case MSG_SPROGRESS:
                progressView.setSecondaryProgress(message.arg1);
                break;
        }
        return false;
    }

    public void onStart(View view) {
        isStop = false;
        new Thread(new Runnable() {
            @Override
            public void run() {

                Message message = mHandler.obtainMessage();
                message.what = MSG_START;
                message.sendToTarget();

                int progress = 0;
                while (progress <= 100 && !isStop) {

                    Message msg = mHandler.obtainMessage();
                    msg.what = MSG_PROGRESS;
                    msg.arg1 = progress;
                    msg.sendToTarget();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    progress++;
                }

                Message msg = mHandler.obtainMessage();
                msg.what = MSG_STOP;
                msg.sendToTarget();

            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                int sprogress = 0;
                while (sprogress <= 100 && !isStop) {

                    Message msg = mHandler.obtainMessage();
                    msg.what = MSG_SPROGRESS;
                    msg.arg1 = sprogress;
                    msg.sendToTarget();
                    try {
                        Thread.sleep(80);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    sprogress++;
                }
            }
        }).start();
    }

    public void onStop(View view) {
        isStop = true;
        Message message = mHandler.obtainMessage();
        message.what = MSG_STOP;
        message.sendToTarget();
    }
}

ProgressActivity 布局文件progress_layout的内容,我们注意app:开头的属性就是我们在attrs中定义的属性。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">
    <Button
        android:id="@+id/btn_start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onStart"
        android:text="start"/>
    <Button
        android:id="@+id/btn_stop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn_start"
        android:onClick="onStop"
        android:text="stop"/>
    <com.wayne.android.widget.ProgressView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/progress_view_id"
        android:layout_below="@id/btn_stop"
        app:pv_progress="0"
        app:pv_secondary_progress="0"
        app:pv_mode="0"
        app:pv_style="linear"
        android:visibility="gone"/>
</RelativeLayout>

ProgressActivity 在manifest中的定义。
     <activity android:name=".ProgressActivity">
            <intent-filter>
                <action android:name="android.intent.action.PROGRESS_VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
结语:

至此实现上面效果的自定义progress控件已经完成,我们可以简单总结为下面几步:
1,在attrs文件中定义属性值。(当然,这步骤可以省略,如果你不想写xml文件,也可以在继承的View类中提供方法给调用者,调用者通过方法设置)
2,继承View,获取初始化信息,绑定drawable。提供用户需要的接口。
3,继承drawable,重写draw方法,绘制控件。(重点)
其实就是开篇说的第三种和第一种结合的方式。

上篇:动画(Property Animation)(闪闪星光示例+知识点)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,864评论 6 494
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,175评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,401评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,170评论 1 286
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,276评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,364评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,401评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,179评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,604评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,902评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,070评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,751评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,380评论 3 319
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,077评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,312评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,924评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,957评论 2 351

推荐阅读更多精彩内容