安卓自定义流程进度图控件实例代码
作者:Zhuzzzzzzx 发布时间:2023-10-30 00:07:23
标签:Android,进度图
先上效果图:
如图,可实现设置:总流程数、已完进度程数、已完成颜色,各个标题
github地址戳这里
使用方法
1.导入compile 'com.github.pavlospt:circleview:1.3'依赖包(因为用到了CircleView)
2.直接把下面两个文件一个java一个xml,复制粘贴进项目(代码放在了文章最后,暂时还没弄成开源库,有时间直接做成依赖包倒进去)
在xml中写入ProcessImg控件
在java文件中实例化ProcessImg对象
根据需要调用几个方法
1.对象.setColor( int color )
设置已完成的进度的颜色,传入颜色的整型值
2.对象.setProcess( int total , int process )
设置总流程数和已完成进度数,第一个参数为总流程数(1~6,因为超过6个堆在一排很难看),第二个为已完成数,均为整型变量
3.对象.setTitle( int position , String text )
设置各流程的标题,第一个参数为标题对应的流程数(1~total),第二个参数为String格式标题文本
源码
源码很简单,注释也比较清楚,应该能看懂
process_img.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/processImg_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="20dp">
<LinearLayout
android:id="@+id/process1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:visibility="gone">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<View
android:id="@+id/view1l"
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_weight="2"
android:background="#00000000"/>
<com.github.pavlospt.CircleView
android:id="@+id/circle1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
app:cv_backgroundColorValue="#00000000"
app:cv_fillColor="#00000000"
app:cv_strokeColorValue="#00000000"
app:cv_subtitleText=""
app:cv_titleColor="#fff"
app:cv_titleSize="20sp"
app:cv_titleText="1"/>
<View
android:id="@+id/view1r"
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_weight="2"
android:background="#00000000"/>
</LinearLayout>
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="text1"
android:textColor="#00000000"/>
</LinearLayout>
<LinearLayout
android:id="@+id/process2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:visibility="gone">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<View
android:id="@+id/view2l"
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_weight="2"
android:background="#00000000"/>
<com.github.pavlospt.CircleView
android:id="@+id/circle2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
app:cv_backgroundColorValue="#00000000"
app:cv_fillColor="#00000000"
app:cv_strokeColorValue="#00000000"
app:cv_subtitleText=""
app:cv_titleColor="#fff"
app:cv_titleSize="20sp"
app:cv_titleText="2"/>
<View
android:id="@+id/view2r"
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_weight="2"
android:background="#00000000"/>
</LinearLayout>
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="text2"
android:textColor="#00000000"/>
</LinearLayout>
<LinearLayout
android:id="@+id/process3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:visibility="gone">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<View
android:id="@+id/view3l"
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_weight="2"
android:background="#00000000"/>
<com.github.pavlospt.CircleView
android:id="@+id/circle3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
app:cv_backgroundColorValue="#00000000"
app:cv_fillColor="#00000000"
app:cv_strokeColorValue="#00000000"
app:cv_subtitleText=""
app:cv_titleColor="#fff"
app:cv_titleSize="20sp"
app:cv_titleText="3"/>
<View
android:id="@+id/view3r"
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_weight="2"
android:background="#00000000"/>
</LinearLayout>
<TextView
android:id="@+id/text3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="text3"
android:textColor="#00000000"/>
</LinearLayout>
<LinearLayout
android:id="@+id/process4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:visibility="gone">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<View
android:id="@+id/view4l"
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_weight="2"
android:background="#00000000"/>
<com.github.pavlospt.CircleView
android:id="@+id/circle4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
app:cv_backgroundColorValue="#00000000"
app:cv_fillColor="#00000000"
app:cv_strokeColorValue="#00000000"
app:cv_subtitleText=""
app:cv_titleColor="#fff"
app:cv_titleSize="20sp"
app:cv_titleText="4"/>
<View
android:id="@+id/view4r"
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_weight="2"
android:background="#00000000"/>
</LinearLayout>
<TextView
android:id="@+id/text4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="text4"
android:textColor="#00000000"/>
</LinearLayout>
<LinearLayout
android:id="@+id/process5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:visibility="gone">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<View
android:id="@+id/view5l"
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_weight="2"
android:background="#00000000"/>
<com.github.pavlospt.CircleView
android:id="@+id/circle5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
app:cv_backgroundColorValue="#00000000"
app:cv_fillColor="#00000000"
app:cv_strokeColorValue="#00000000"
app:cv_subtitleText=""
app:cv_titleColor="#fff"
app:cv_titleSize="20sp"
app:cv_titleText="5"/>
<View
android:id="@+id/view5r"
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_weight="2"
android:background="#00000000"/>
</LinearLayout>
<TextView
android:id="@+id/text5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="text5"
android:textColor="#00000000"/>
</LinearLayout>
<LinearLayout
android:id="@+id/process6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:visibility="gone">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<View
android:id="@+id/view6l"
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_weight="2"
android:background="#00000000"/>
<com.github.pavlospt.CircleView
android:id="@+id/circle6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
app:cv_backgroundColorValue="#00000000"
app:cv_fillColor="#00000000"
app:cv_strokeColorValue="#00000000"
app:cv_subtitleText=""
app:cv_titleColor="#fff"
app:cv_titleSize="20sp"
app:cv_titleText="6"/>
<View
android:id="@+id/view6r"
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_weight="2"
android:background="#00000000"/>
</LinearLayout>
<TextView
android:id="@+id/text6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="text6"
android:textColor="#00000000"/>
</LinearLayout>
</LinearLayout>
ProcessImg.java
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.github.pavlospt.CircleView;
import com.zxzhu.processtest.R;
import java.util.ArrayList;
import java.util.List;
/**
* Created by zxzhu on 2017/6/18.
*/
public class ProcessImg extends LinearLayout {
private String TAG = "ProcessImg";
private CircleView circleView1, circleView2, circleView3, circleView4, circleView5, circleView6;
private TextView textView1, textView2, textView3, textView4, textView5, textView6;
//view代表流程连接线
private View view1l, view1r, view2l, view2r, view3l, view3r, view4l, view4r, view5l, view5r, view6l, view6r;
private LinearLayout process1, process2, process3, process4, process5, process6;
private List<LinearLayout> layouts = new ArrayList<>();//用于控制流程个数的列表
private List<View> views = new ArrayList<>();
private List<TextView> texts = new ArrayList<>();
private List<CircleView> circleViews = new ArrayList<>();
private Context context;
private int color = Color.parseColor("#E0E0E0");
private int total = 0, process = 0;
public ProcessImg(Context context) {
super(context);
this.context = context;
initViews();
}
public ProcessImg(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
this.context = context;
initViews();
}
private void initViews() {
LayoutInflater.from(context).inflate(R.layout.process_img, this);
circleView1 = (CircleView) findViewById(R.id.circle1);
circleView2 = (CircleView) findViewById(R.id.circle2);
circleView3 = (CircleView) findViewById(R.id.circle3);
circleView4 = (CircleView) findViewById(R.id.circle4);
circleView5 = (CircleView) findViewById(R.id.circle5);
circleView6 = (CircleView) findViewById(R.id.circle6);
process1 = (LinearLayout) findViewById(R.id.process1);
process2 = (LinearLayout) findViewById(R.id.process2);
process3 = (LinearLayout) findViewById(R.id.process3);
process4 = (LinearLayout) findViewById(R.id.process4);
process5 = (LinearLayout) findViewById(R.id.process5);
process6 = (LinearLayout) findViewById(R.id.process6);
textView1 = (TextView) findViewById(R.id.text1);
textView2 = (TextView) findViewById(R.id.text2);
textView3 = (TextView) findViewById(R.id.text3);
textView4 = (TextView) findViewById(R.id.text4);
textView5 = (TextView) findViewById(R.id.text5);
textView6 = (TextView) findViewById(R.id.text6);
view1l = findViewById(R.id.view1l);
view1r = findViewById(R.id.view1r);
view2l = findViewById(R.id.view2l);
view2r = findViewById(R.id.view2r);
view3l = findViewById(R.id.view3l);
view3r = findViewById(R.id.view3r);
view4l = findViewById(R.id.view4l);
view4r = findViewById(R.id.view4r);
view5l = findViewById(R.id.view5l);
view5r = findViewById(R.id.view5r);
view6l = findViewById(R.id.view6l);
view6r = findViewById(R.id.view6r);
circleViews.add(circleView1);
circleViews.add(circleView2);
circleViews.add(circleView3);
circleViews.add(circleView4);
circleViews.add(circleView5);
circleViews.add(circleView6);
layouts.add(process1);
layouts.add(process2);
layouts.add(process3);
layouts.add(process4);
layouts.add(process5);
layouts.add(process6);
texts.add(textView1);
texts.add(textView2);
texts.add(textView3);
texts.add(textView4);
texts.add(textView5);
texts.add(textView6);
views.add(view1l);
views.add(view1r);
views.add(view2l);
views.add(view2r);
views.add(view3l);
views.add(view3r);
views.add(view4l);
views.add(view4r);
views.add(view5l);
views.add(view5r);
views.add(view6l);
views.add(view6r);
}
/**
* 设置已完成流程颜色
*
* @param color
*/
public void setColor(int color) {
this.color = color;
setProcess(total, process);
}
/**
* 设置进度
*
* @param total 总共流程个数(不超过6)
* @param process 当前进度
*/
public void setProcess(int total, int process) {
this.total = total;
this.process = process;
//使流程总数的view显示出来
for (int i = 0; i < total; i++) {
layouts.get(i).setVisibility(VISIBLE);
}
// 设置已完成进度的颜色
for (int i = 0; i < circleViews.size(); i++) {
if (i < process) {
circleViews.get(i).setFillColor(color);
circleViews.get(i).setBackgroundColor(color);
circleViews.get(i).setStrokeColor(color);
texts.get(i).setTextColor(color);
} else {
//未完成设置为灰色
circleViews.get(i).setFillColor(Color.parseColor("#CCCCCC"));
circleViews.get(i).setBackgroundColor(Color.parseColor("#CCCCCC"));
texts.get(i).setTextColor(Color.parseColor("#CCCCCC"));
circleViews.get(i).setStrokeColor(color);
circleViews.get(i).setStrokeColor(Color.parseColor("#CCCCCC"));
}
}
//设置流程连接线的颜色
for (int i = 0; i < views.size(); i++) {
if (i < process * 2) {
views.get(i).setBackgroundColor(color);
} else {
views.get(i).setBackgroundColor(Color.parseColor("#CCCCCC"));
}
}
views.get(0).setBackgroundColor(Color.parseColor("#00000000"));
if (total != 0) {
views.get(2 * total - 1).setBackgroundColor(Color.parseColor("#00000000"));
}
}
/**
* 设置各进度标题
*
* @param position
* @param text
*/
public void setTitle(int position, String text) {
texts.get(position - 1).setText(text);
}
}
来源:http://www.jianshu.com/p/1a4867299a86?utm_source=tuicool&utm_medium=referral


猜你喜欢
- springboot天生支持使用hibernate validation对参数的优雅校验,如果不使用它,只能对参数挨个进行如下方式的手工校验
- Dialog的基本方法//创建DialogAlertDialog.Builder builder = new AlertDialog.Bui
- 在对接第三方支付的时候,第三方会要求参数按照ASCII码从小到大排序。如下:public static void requestPay()
- 一、前言关于EasyExcel,它对poi做了进一步的封装,使得整个编写流程更加的面向对象。好处嘛,我认为流程上更加清晰即易懂、可读性更好,
- XML假如有这样一个XML格式的数据:<?xml version="1.0" encoding="utf
- 还是有一些小问题....懒得改了,但大体思路还是清晰的,首先定义一个运算符栈,一个数栈。关于优先级,两个括号(,)优先级最低,其次是+、-,
- 前言Webp是Google推出的一种新型图片格式,相比于 传统的PNG/JPG图片有着更小体积的优势,在Web中有着广泛的应用。由于Webp
- 约瑟夫环约瑟夫环(约瑟夫问题)是一个数学的应用问题:已知n个人(以编号1,2,3…n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数
- 所谓c#的委托就是说把函数当参数来传递。这个在js完全就用不着搞什么委托东西,直接转就是了。而对于C#来说则不是这样!一个函数,如果它的参数
- 1.Overview经常研究.NET源码库的小伙伴会经常看到一个关键字volatile,那它在开发当中的作用是什么呢?我们一起来看看官方文档
- 使用方法:先把mvcpager.dll引用加入mvc项目中。前台代码前台:@{Layout = null;}@using Webdiyer.
- 数据访问之Druid数据源的使用说明:该数据源Druid,使用自定义方式实现,后面文章使用start启动器实现,学习思路为主。为什么要使用数
- 快速排序快速排序是一种比较高效的排序算法,采用“分而治之”的思想,通过多次比较和交换来实现排序,在一
- 假设下面是你的视频网站链接列表,如果别人想爬取你的数据十分轻松,看规则就知道数据库是序列自增的http://www.xxxx.com/vid
- 一:背景1.讲故事前段时间分析了一个dump,一顿操作之后,我希望用外力来阻止程序内部对某一个com组件的调用,对,就是想借助外力实现,如果
- 不啰嗦,上菜 QueryWrapper queryWrapper = new QueryWrapper(); queryWrapper.se
- 先看下利用wheelview实现滚动随机选择号码效果:直接上代码 首页就是dialog显示不在描述 主要看dialog代码package c
- 一、序言(一)背景内容软件应用技术架构中DAO层最常见的选型组件为MyBatis,熟悉MyBatis的朋友都清楚,曾几何时MyBatis是多
- 带返回值的方法练习需求: 设计一个方法可以获取两个数的较大值,数据来自于参数思路:1. 定义一个方法,用于获取两个数中的较大数public
- java 制作验证码并进行验证实例详解在注册、登录的页面上经常会出现验证码,为了防止频繁的注册或登录行为。下面是我用java制作的一个验证码