android 开发-自定义多节点进度条显示

看效果图:

里面的线段颜色和节点图标都是可以自定义的。

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#ffffff"
    tools:context=".MainActivity" xmlns:app="http://schemas.android.com/apk/res/com.demo.demomutiprogress">

    <com.demo.demomutiprogress.MutiProgress
        android:id="@+id/mp_1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        app:nodesNum="4"
        app:currNodeState="1"
        app:currNodeNO="2"
        app:nodeRadius="10dp"
        app:processingLineColor="#7B68EE"
        app:unprogressingDrawable="@drawable/ic_round_ddd"
        app:progressingDrawable="@drawable/ic_completed"
        app:progresFailDrawable="@drawable/ic_error"
        app:progresSuccDrawable="@drawable/ic_checked"/>
    <com.demo.demomutiprogress.MutiProgress
        android:id="@+id/mp_2"
        android:layout_below="@+id/mp_1"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        app:nodesNum="10"
        app:currNodeState="1"
        app:currNodeNO="6"
        app:nodeRadius="6dp"
        app:processingLineColor="#7B68EE"
        app:progressingDrawable="@drawable/ic_completed"
        app:unprogressingDrawable="@drawable/ic_round_ddd"
        app:progresFailDrawable="@drawable/ic_error"
        app:progresSuccDrawable="@drawable/ic_checked"/>
    <com.demo.demomutiprogress.MutiProgress
        android:id="@+id/mp_3"
        android:layout_below="@+id/mp_2"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:nodesNum="15"
        app:currNodeState="0"
        app:currNodeNO="10"
        app:nodeRadius="4dp"
        app:processingLineColor="#FF00FF"
        app:progressingDrawable="@drawable/ic_completed"
        app:unprogressingDrawable="@drawable/ic_round_ddd"
        app:progresFailDrawable="@drawable/ic_error"
        app:progresSuccDrawable="@drawable/ic_checked"/>

</RelativeLayout>

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MutiProgress">
        <attr name="nodesNum" format="integer"/> <!-- 节点数量 -->
        <attr name="nodeRadius" format="dimension"/>
        <attr name="progressingDrawable" format="reference"></attr>
        <attr name="unprogressingDrawable" format="reference" />  <!-- 未完成的节点图标 -->
        <attr name="progresFailDrawable" format="reference" />
        <attr name="progresSuccDrawable" format="reference" /> 

        <attr name="processingLineColor" format="color"></attr>
        <attr name="currNodeNO" format="integer"></attr>   <!-- 当前所到达的节点编号  0开始计算-->
        <attr name="currNodeState" format="integer"></attr>   <!-- 当前所到达的节点状态,0:失败  1:成功 -->
    </declare-styleable>
</resources>

MutiProgress.java

package com.demo.demomutiprogress;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 多节点进度条自定义视图
 * @author huqiang
 *
 */
public class MutiProgress extends View{

    private int nodesNum ; //节点数量
    private Drawable progressingDrawable;  //进行中的图标
    private Drawable unprogressingDrawable;
    private Drawable progresFailDrawable;  //失败的节点
    private Drawable progresSuccDrawable;  //成功的节点
    private int nodeRadius;  //节点的半径
    private int processingLineColor;  //进度条的颜色

//    private int progressLineHeight;   //进度条的高度
    private int currNodeNO;  //当前进行到的节点编号。从0开始计算
    private int currNodeState; //当前进行到的节点编号所对应的状态 0:失败  1:成功
//    private int textSize;  //字体大小
    Context mContext;

    int mWidth,mHeight;
    private Paint mPaint;
    private Canvas mCanvas;
    private Bitmap mBitmap; //mCanvas绘制在这上面
    private ArrayList<Node> nodes;

    private int DEFAULT_LINE_COLOR = Color.BLUE;
    public MutiProgress(Context context) {
        this(context,null);

    }
    public MutiProgress(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }
    public MutiProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;

        TypedArray mTypedArray = context.obtainStyledAttributes(attrs,R.styleable.MutiProgress);
        nodesNum = mTypedArray.getInteger(R.styleable.MutiProgress_nodesNum, 1); //默认一个节点
        nodeRadius = mTypedArray.getDimensionPixelSize(R.styleable.MutiProgress_nodeRadius, 10); //节点半径
        progressingDrawable = mTypedArray.getDrawable(R.styleable.MutiProgress_progressingDrawable);
        unprogressingDrawable = mTypedArray.getDrawable(R.styleable.MutiProgress_unprogressingDrawable);
        progresFailDrawable = mTypedArray.getDrawable(R.styleable.MutiProgress_progresFailDrawable);
        progresSuccDrawable = mTypedArray.getDrawable(R.styleable.MutiProgress_progresSuccDrawable);
        processingLineColor = mTypedArray.getColor(R.styleable.MutiProgress_processingLineColor, DEFAULT_LINE_COLOR);
        currNodeState = mTypedArray.getInt(R.styleable.MutiProgress_currNodeState, 1);
        currNodeNO = mTypedArray.getInt(R.styleable.MutiProgress_currNodeNO, 1);

    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();

        mBitmap = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
        mPaint = new Paint();
        mPaint.setColor(processingLineColor);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeJoin(Paint.Join.ROUND); // 圆角
        mPaint.setStrokeCap(Paint.Cap.ROUND); // 圆角
        mCanvas = new Canvas(mBitmap);

        nodes = new ArrayList<MutiProgress.Node>();
        float nodeWidth = ((float)mWidth)/(nodesNum-1);
        for(int i=0;i<nodesNum;i++)
        {
            Node node = new Node();
            if(i==0)
                node.mPoint = new Point(((int)nodeWidth*i),mHeight/2-nodeRadius);
            else if(i==(nodesNum-1))
                node.mPoint = new Point(((int)nodeWidth*i)-nodeRadius*2,mHeight/2-nodeRadius);
            else
                node.mPoint = new Point(((int)nodeWidth*i)-nodeRadius,mHeight/2-nodeRadius);
            if(currNodeNO == i)
                node.type = 1;  //当前进度所到达的节点
            else
                node.type = 0; //已完成
            nodes.add(node);
        }
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        DrawProgerss();
        Log.v("ondraw", "mBitmap="+mBitmap);
        if(mBitmap!=null)
        {
            canvas.drawBitmap(mBitmap, new Rect(0,0,mBitmap.getWidth(),mBitmap.getHeight()), new Rect(0,0,mBitmap.getWidth(),mBitmap.getHeight()), mPaint);
        }
        for(int i=0;i<nodes.size();i++)
        {
            Node node = nodes.get(i);
            Log.v("ondraw", node.mPoint.x +";y="+node.mPoint.y);
            if(i<currNodeNO)  //已完成的进度节点
            {
                progressingDrawable.setBounds(node.mPoint.x,  node.mPoint.y , node.mPoint.x + nodeRadius*2,node.mPoint.y + nodeRadius*2);
                progressingDrawable.draw(canvas);
            }
            else if(i==currNodeNO)  //当前所到达的进度节点(终点)
            {
                if(currNodeState == 1) //判断是成功还是失败  0 :失败  1:成功
                {
                    progresSuccDrawable.setBounds(node.mPoint.x,  node.mPoint.y , node.mPoint.x + nodeRadius*2,node.mPoint.y + nodeRadius*2);
                    progresSuccDrawable.draw(canvas);
                }
                else
                {
                    progresFailDrawable.setBounds(node.mPoint.x,  node.mPoint.y , node.mPoint.x + nodeRadius*2,node.mPoint.y + nodeRadius*2);
                    progresFailDrawable.draw(canvas);
                }
            }
            else   //未完成的进度节点
            {
                unprogressingDrawable.setBounds(node.mPoint.x,  node.mPoint.y , node.mPoint.x + nodeRadius*2,node.mPoint.y + nodeRadius*2);
                unprogressingDrawable.draw(canvas);
            }
        }
    }
    private void DrawProgerss()
    {
        //先画背景
        Paint bgPaint = new Paint();
        bgPaint.setColor(Color.parseColor("#f0f0f0"));
        mCanvas.drawRect(0, 0, mWidth, mHeight, bgPaint);
        //先画线段,线段的高度为nodeRadius/2
        mPaint.setStrokeWidth(nodeRadius/2);
        //前半截线段
//        mCanvas.drawLine(nodeRadius, mHeight/2, mWidth-nodeRadius, mHeight/2, mPaint);  //线段2端去掉nodeRadius
        mCanvas.drawLine(nodeRadius, mHeight/2, nodes.get(currNodeNO).mPoint.x + nodeRadius, nodes.get(currNodeNO).mPoint.y + nodeRadius, mPaint);  //线段2端去掉nodeRadius
        //后半截线段
        mPaint.setColor(Color.parseColor("#dddddd"));
        mCanvas.drawLine(nodes.get(currNodeNO).mPoint.x +nodeRadius, nodes.get(currNodeNO).mPoint.y + nodeRadius, mWidth-nodeRadius, mHeight/2, mPaint);  //线段2端去掉nodeRadius
    }
    class Node
    {
        Point mPoint;
        int type; //0:已完成  1:当前到达的进度节点
    }
}

源码下载

时间: 2024-08-05 03:07:43

android 开发-自定义多节点进度条显示的相关文章

【Android开发】高级组件-进度条

当一个应用在后台执行时,前台界面不会有任何信息,这是用户根本不知道程序是否在执行以及执行进度等,因此需要使用进度条来提示程序执行的进度.在Android中,进度条(ProgressBar)用于向用户显示某个耗时操作完成的百分比. 在屏幕中添加进度天,可以在XML布局文件中通过标记添加,基本语法格式如下:    属性列表 > ProgressBar组件支持的XML属性如下所... http://songtaste.com/user/10310357/infohttp://songtaste.com

Android——音乐播放器完善——进度条显示当前播放进度,加可拖动进度条(未待解决完问题)

效果: 问题:可拖动进度条随进度条移动时,会致使音乐卡顿(待解决) xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:l

Android 自定义对话框,进度条,下拉刷新等

这个demo集合了自定义对话框,进度条,下拉刷新以及popup弹出框等.是学习了网上开源项目后,抽取集合了常用对话框,进度条,下拉刷新以及popup弹出框等.现在结构目录简单,更易于学习和扩展使用.注释都卸载代码.下面进行简单的介绍以及部分代码展示. 本文demo下载:点击 1.整体实现的效果图 2.项目结构图 这上面项目结构图也是一目了然,有什么功能展示.大家也看到了,这上面类有点多,如果全部贴出来,不大可能,有兴趣下载本文源码. 3.看看基础类BaseActivity 我就贴一下基础类,还有

Android简易实战教程--第十七话《自定义彩色环形进度条》

转载请注明出处:http://blog.csdn.net/qq_32059827/article/details/52203533   点击打开链接 在Android初级教程里面,介绍了shape用法的理论知识,再来完成这个小案例将会变得非常简单哦.(欢迎学习阅读):http://blog.csdn.net/qq_32059827/article/details/52203347 点击打开链接 这一篇就针对这个知识点,完成一个自定义的彩色进度条.系统自带的环形进度条是黑白相间的,如果你不是色盲,

Android自定义圆角矩形进度条2

效果图: 或 方法讲解: (1)invalidate()方法 invalidate()是用来刷新View的,必须是在UI线程中进行工作.比如在修改某个view的显示时, 调用invalidate()才能看到重新绘制的界面.invalidate()的调用是把之前的旧的view从主UI线程队列中pop掉.一般在自定义控件中会用到这个方法. (2)RectF方法的应用 RectF是用来绘画矩形的方法. RectF(left,top,right,bottom),四个参数的含义分别是父控件距离矩形左上右下

【Android】读取sdcard卡上的全部图片而且显示,读取的过程有进度条显示

尽管以下的app还没有做到快图浏览.ES文件浏览器的水平,遇到大sdcard还是会存在读取过久.内存溢出等问题,可是基本思想是这种. 例如以下图.在sdcard卡上有4张图片, 打开app,则会吧sd卡上的全部图片读取,并显示出来.读取的过程有进度条显示. 制作步骤例如以下: 1.首先,res\values\strings.xml对字符设置例如以下,没有什么特别的. <? xml version="1.0" encoding="utf-8"?> <

【Android】读取sdcard卡上的所有图片并且显示,读取的过程有进度条显示

虽然下面的app还没有做到快图浏览.ES文件浏览器的水平,遇到大sdcard还是会存在读取过久.内存溢出等问题,但是基本思想是这样的. 如下图,在sdcard卡上有4张图片, 打开app,则会吧sd卡上的所有图片读取,并显示出来,读取的过程有进度条显示. 制作过程如下: 1.首先,res\values\strings.xml对字符设置如下,没有什么特别的. <?xml version="1.0" encoding="utf-8"?> <resour

Android 学习心得(6)——ProgressBar(进度条)

没什么技术含量就是对系统进度条的简单应用 贴上代码 1 package cn.bwl.progressbar; 2 3 import android.support.v7.app.ActionBarActivity; 4 import android.view.View; 5 import android.widget.Button; 6 import android.widget.ProgressBar; 7 import android.widget.TextView; 8 import a

低版本系统兼容的ActionBar(三)自定义Item视图+进度条的实现+下拉导航+透明ActionBar

       一.自定义MenuItem的视图 custom_view.xml (就是一个单选按钮) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android