欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义带水滴的进度条样式(带渐变色效果)

程序员文章站 2024-02-28 17:06:10
一、直接看效果 二、直接上代码 1.自定义控件部分 package com.susan.project.myapplication; import an...

一、直接看效果

Android自定义带水滴的进度条样式(带渐变色效果)

二、直接上代码

1.自定义控件部分

package com.susan.project.myapplication;
import android.app.activity;
import android.content.context;
import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.lineargradient;
import android.graphics.paint;
import android.graphics.rectf;
import android.graphics.shader;
import android.util.attributeset;
import android.util.displaymetrics;
import android.util.log;
import android.view.view;
/**
* @author dahai
* @classname: ${type_name}
* @description: ${todo}
* @date ${date} ${time}
* @email 202491024@qq.com
* @since $android
渐变
进度条

*/
public class progressseek extends view {
/**
* 进度条的宽度
*/
private int view_width;
/**
* 画布的宽度
*/
private int view_base_width;
/**
* 控件的宽度
*/
private int view_edge_width;
/**
* 进度
*/
private int progress;
private canvas cachecanvas;
/**
* 背景颜色的画笔
*/
private paint backgroundpaint;
/**
* 进度条的画笔
*/
private paint progresspaint;
/**
* 进度末端的图
*/
private bitmap bitmap;
private int bitmapwidth;
private int bitmapheight;
private context context;
//渐变色开始
private static final int default_start_color = color.parsecolor("#34dab5");
//渐变色结束
private static final int default_end_color = color.parsecolor("#27a5fe");
/**
* 缓存图片
*/
private bitmap cachebitmap;
public progressseek(context context) {
super(context);
initview(context);
}
public progressseek(context context, attributeset attrs) {
super(context, attrs);
initview(context);
}
public progressseek(context context, attributeset attrs, int defstyleattr) {
super(context, attrs, defstyleattr);
initview(context);
}
private void initview(context context) {
this.context = context;
bitmap = bitmapfactory.decoderesource(context.getresources(), r.mipmap.thumb);
bitmapwidth = bitmap.getwidth();
bitmapheight = bitmap.getheight();
backgroundpaint = new paint();
backgroundpaint.setstrokewidth(bitmapwidth);
backgroundpaint.setcolor(color.parsecolor("#cccccc"));
backgroundpaint.setdither(true);
backgroundpaint.setantialias(true);
progresspaint = new paint();
progresspaint.setstrokewidth(bitmapwidth);
progresspaint.setdither(true);
progresspaint.setantialias(true);
displaymetrics d = new displaymetrics();
((activity) context).getwindowmanager().getdefaultdisplay().getmetrics(d);
view_base_width = d.widthpixels;
}
public void init(int progress) {
this.progress = progress;
if (view_width == 0) {//第一上来
/* displaymetrics d = new displaymetrics();
((activity) context).getwindowmanager().getdefaultdisplay().getmetrics(d);
view_width = d.widthpixels*progress/100;*/
view_width = view_base_width * progress / 100;
} else {
view_width = view_edge_width * progress / 100;
}
if (cachebitmap != null) {
if (!cachebitmap.isrecycled()) {
cachebitmap.recycle();
cachebitmap = null;
}
cachecanvas = null;
}
cachebitmap = bitmap.createbitmap(view_base_width, bitmapheight * 2, bitmap.config.argb_8888);
if (cachecanvas == null) {
cachecanvas = new canvas();
cachecanvas.setbitmap(cachebitmap);
}
/**
* 画背景
*/
rectf r = new rectf();
r.left = 0;
r.top = bitmapheight;
r.right = view_base_width;
r.bottom = bitmapwidth + 10;
cachecanvas.drawroundrect(r, 5f, 5f, backgroundpaint);
if (progress > 0) {
lineargradient lg = new lineargradient(0, 0, view_width, bitmapwidth, default_start_color, default_end_color, shader.tilemode.clamp);
progresspaint.setshader(lg);
rectf r1 = new rectf();
r.left = 0;
r.top = bitmapheight;
r.right = view_width;
r.bottom = bitmapwidth + 10;
cachecanvas.drawroundrect(r, 5f, 5f, progresspaint);
cachecanvas.drawbitmap(bitmap, view_width - bitmapwidth+8, bitmapheight / 2 + 6, new paint());
}
invalidate();
}
@override
protected void ondraw(canvas canvas) {
super.ondraw(canvas);
paint bmppaint = new paint();
//将cachebitmap绘制到该view组件
if (cachebitmap != null) {
canvas.drawbitmap(cachebitmap, 0, 0, bmppaint);
}
view_edge_width = this.getwidth();
log.e("打出来看看控件的宽度:", view_edge_width + "");
init(progress);
}
}

3. 布局文件部分

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.susan.project.myapplication.progressseek
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="80dp">
</com.susan.project.myapplication.progressseek>
<com.susan.project.myapplication.progressseek
android:id="@+id/progress1"
android:layout_width="match_parent"
android:layout_height="80dp">
</com.susan.project.myapplication.progressseek>
<com.susan.project.myapplication.progressseek
android:id="@+id/progress2"
android:layout_width="match_parent"
android:layout_height="80dp">
</com.susan.project.myapplication.progressseek>
<com.susan.project.myapplication.progressseek
android:id="@+id/progress3"
android:layout_width="match_parent"
android:layout_height="80dp">
</com.susan.project.myapplication.progressseek>
</linearlayout>

4.activity部分

package com.susan.project.myapplication;
import android.app.activity;
import android.os.bundle;
public class mainactivity extends activity {
private progressseek progress;
private progressseek progress1;
private progressseek progress2;
private progressseek progress3;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
progress = (progressseek) findviewbyid(r.id.progress);
progress.init(0);
progress1 = (progressseek) findviewbyid(r.id.progress1);
progress1.init(2);
progress2 = (progressseek) findviewbyid(r.id.progress2);
progress2.init(50);
progress3 = (progressseek) findviewbyid(r.id.progress3);
progress3.init(100);
}
}

以上所述是小编给大家介绍的android自定义带水滴的进度条样式(带渐变色效果),希望对大家有所帮助