用画布自定义 渐变 和 螺纹 进度条 没有图片

来源:互联网 发布:电脑免费变声软件 编辑:程序博客网 时间:2024/04/29 06:54


渐变的进度条网上好多。。但没有螺纹的进度条。。做了一个供大家参考

主要说下螺纹的思路。

水平有限 只想到这个思路。如果对你有启发 ,有好的意见请告诉我啊。互相成长
画螺纹基本实现了。

原理是 

先画一个黑色

 在画 上下左右都减2像素的灰色 出现黑边框了

 在画一层浅黄色 

再画一层又间隔的斜线。

斜线是用1像素的线挨个画出来的 就出现了平行四边形了。


螺纹代码

package com.spring.progressview;import android.R.color;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.LinearGradient;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.RectF;import android.graphics.Shader;import android.util.AttributeSet;import android.view.View;/*** * 自定义进度条 *  * 画螺纹基本实现了。原理是 先画一个黑色 在画 上下左右都减2像素的灰色 出现黑边框了 在画一层浅黄色 再画一层又间隔的斜线。斜线 * 是用1像素的线挨个画出来的 就出现了平行四边形了。 */public class SpringProgressView2 extends View {/** 进度条最大值 */private float maxCount;/** 进度条当前值 */private float currentCount;/** 画笔 */private Paint mPaint1, mPaint2, mPaint3, mPaint4;private int mWidth, mHeight;/** *  * 第一个斜线的起始位置 */private float first_diagonal_line;/** *  * 第一个斜线的底边最左端 */private float first_diagonal_line_left;/** *  * 每条斜线之间的距离 */private float dis_line;/** *  * 每条斜線的寬度 */private float w_line;Canvas canvas;/** * 矩形圆角 */private int round;/** * 进度条百分率 */private float section;/** * 设置几条斜线 */private int num;private float b;public SpringProgressView2(Context context, AttributeSet attrs,int defStyleAttr) {super(context, attrs, defStyleAttr);initView(context);}public SpringProgressView2(Context context, AttributeSet attrs) {super(context, attrs);initView(context);}public SpringProgressView2(Context context) {super(context);initView(context);}private void initView(Context context) {}// 初始化各种private void init() {mPaint1 = new Paint();mPaint2 = new Paint();mPaint3 = new Paint();mPaint4 = new Paint();mPaint1.setAntiAlias(true);mPaint2.setAntiAlias(true);mPaint3.setAntiAlias(true);mPaint4.setAntiAlias(true);mPaint1.setColor(0xffe6e3e1);// mPaint2.setColor(0xffe0dcda);mPaint4.setColor(0xfff3c45e);mPaint3.setColor(0xffd89c19);mPaint3.setStrokeWidth(1);// 斜线画笔的粗度1像素// 可以更改螺纹宽度等等round = mHeight / 5;first_diagonal_line = mWidth / 40;first_diagonal_line_left = first_diagonal_line - mWidth / 80;dis_line = mWidth / 24;w_line = mWidth / 48;section = currentCount / maxCount;// section =(float) 0.02;// 设置画出来几个斜线num = (int) ((section * (mWidth - 2) - first_diagonal_line) / (w_line + dis_line));System.out.println("num:" + num);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);this.canvas = canvas;init();System.out.println("max=" + maxCount + "  current=" + currentCount);// 背景黑边儿RectF rectBg = new RectF(0, 0, mWidth, mHeight);canvas.drawRoundRect(rectBg, round, round, mPaint2);// 灰色背景RectF rectBlackBg = new RectF(2, 2, mWidth - 2, mHeight - 2);canvas.drawRoundRect(rectBlackBg, round, round, mPaint1);// 浅色 进度条RectF rectProgressBg_up = new RectF(2, 2, (mWidth - 2) * section,mHeight - 2);// canvas.drawRoundRect(rectProgressBg_up, round, round, mPaint4);/* * 当进度条小于第一条斜线最右短,不显示斜线 当大于第一条进度后开始画线 主要是线的宽度 和 两条线之间的宽度的计算。 */if ((mWidth - 2) * section <= first_diagonal_line + w_line + mWidth/ 80) {System.out.println("---------------");canvas.drawRoundRect(rectProgressBg_up, round, round, mPaint4);}else {canvas.drawRoundRect(rectProgressBg_up, round, round, mPaint4);//double a = ((mWidth - 2) * section - first_diagonal_line)//% (w_line + dis_line);for (float k = first_diagonal_line; k <= section * (mWidth - 2)- mWidth/30 ; k = k + dis_line + w_line) {diagonal_line(k);//b = k;}//if (a < w_line + 4) {////} else {////diagonal_line(b);////}}}// 画一条斜线private void diagonal_line(float i) {System.out.println("----------画了一条线-------------------");for (float j = i; j <= i + w_line; j++) {canvas.drawLine(j, 2, j - mWidth / 80, mHeight - 3, mPaint3);}}private int dipToPx(int dip) {float scale = getContext().getResources().getDisplayMetrics().density;return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));}/*** * 设置最大的进度值 *  * @param maxCount */public void setMaxCount(float maxCount) {this.maxCount = maxCount;}/*** * 设置当前的进度值 *  * @param currentCount */public void setCurrentCount(float currentCount) {this.currentCount = currentCount > maxCount ? maxCount : currentCount;invalidate();}public float getMaxCount() {return maxCount;}public float getCurrentCount() {return currentCount;}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);if (widthSpecMode == MeasureSpec.EXACTLY|| widthSpecMode == MeasureSpec.AT_MOST) {mWidth = widthSpecSize;} else {mWidth = 0;}if (heightSpecMode == MeasureSpec.AT_MOST|| heightSpecMode == MeasureSpec.UNSPECIFIED) {mHeight = dipToPx(15);} else {mHeight = heightSpecSize;}setMeasuredDimension(mWidth, mHeight);System.out.println("mWidth:" + mWidth + "mHeight:" + mHeight);}}


渐变代码

package com.spring.progressview;import android.R.color;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.LinearGradient;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.RectF;import android.graphics.Shader;import android.util.AttributeSet;import android.view.View;/*** * 自定义进度条 *  * @author spring sky Email:vipa1888@163.com 创建时间:2014-1-6下午3:28:51 */public class SpringProgressView extends View {/** 分段颜色 */private static final int[] SECTION_COLORS = { Color.rgb(191, 251, 151),Color.rgb(201, 246, 181), Color.rgb(148, 220, 110) };private static final int[] SECTION_COLORS1 = { Color.rgb(138, 212, 73),Color.rgb(140, 217, 75), Color.rgb(148, 220, 110) };/** 进度条最大值 */private float maxCount;/** 进度条当前值 */private float currentCount;/** 画笔 */private Paint mPaint, mPaint1, mPaint2, mPaint3;private int mWidth, mHeight;public SpringProgressView(Context context, AttributeSet attrs,int defStyleAttr) {super(context, attrs, defStyleAttr);initView(context);}public SpringProgressView(Context context, AttributeSet attrs) {super(context, attrs);initView(context);}public SpringProgressView(Context context) {super(context);initView(context);}private void initView(Context context) {}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);mPaint = new Paint();// 进度条mPaint1 = new Paint();mPaint2 = new Paint();mPaint.setAntiAlias(true);// 进度条mPaint1.setAntiAlias(true);mPaint2.setAntiAlias(true);int round = mHeight / 5;System.out.println("max=" + maxCount + "  current=" + currentCount);// 背景色mPaint2.setColor(0xFFc2c2c2);RectF rectBg = new RectF(0, 0, mWidth, mHeight);canvas.drawRoundRect(rectBg, round, round, mPaint2);// 背景色LinearGradient background = new LinearGradient(0, 0, 0, mHeight,new int[] { 0xFFe6e6e6, 0xFFfbfbfb }, null,Shader.TileMode.MIRROR);mPaint1.setShader(background);RectF rectBlackBg = new RectF(2, 2, mWidth - 2, mHeight - 2);canvas.drawRoundRect(rectBlackBg, round, round, mPaint1);// 进度条float section = currentCount / maxCount;RectF rectProgressBg_up = new RectF(2, 2, (mWidth - 2) * section,mHeight - 2);if (section <= 1.0f / 3.0f) {if (section != 0.0f) {mPaint.setColor(SECTION_COLORS[0]);} else {mPaint.setColor(Color.TRANSPARENT);}} else {int count = (section <= 1.0f / 3.0f * 2) ? 2 : 3;int[] colors = new int[count];System.arraycopy(SECTION_COLORS, 0, colors, 0, count);// 吧SECTION_COLORS的内容复制到colors中LinearGradient shader = new LinearGradient(3, 3, (mWidth - 3)* section, mHeight - 3, colors, null,Shader.TileMode.MIRROR);mPaint.setShader(shader);}canvas.drawRoundRect(rectProgressBg_up, round, round, mPaint);}private int dipToPx(int dip) {float scale = getContext().getResources().getDisplayMetrics().density;return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));}/*** * 设置最大的进度值 *  * @param maxCount */public void setMaxCount(float maxCount) {this.maxCount = maxCount;}/*** * 设置当前的进度值 *  * @param currentCount */public void setCurrentCount(float currentCount) {this.currentCount = currentCount > maxCount ? maxCount : currentCount;invalidate();}public float getMaxCount() {return maxCount;}public float getCurrentCount() {return currentCount;}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);if (widthSpecMode == MeasureSpec.EXACTLY|| widthSpecMode == MeasureSpec.AT_MOST) {mWidth = widthSpecSize;} else {mWidth = 0;}if (heightSpecMode == MeasureSpec.AT_MOST|| heightSpecMode == MeasureSpec.UNSPECIFIED) {mHeight = dipToPx(15);} else {mHeight = heightSpecSize;}setMeasuredDimension(mWidth, mHeight);}}

主函数

package com.spring.progressview;import java.util.Random;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.TextView;/*** * 设置界面 *  * @author smz 创建时间:2014-1-7上午10:51:21 */public class MainActivity extends Activity implements OnClickListener {private TextView textView;private SpringProgressView progressView;private SpringProgressView2 progressView1;private Random random = new Random(System.currentTimeMillis());private int i = 0;private int currentCount = 0;// 进度当前值@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main_layout);textView = (TextView) findViewById(R.id.textview);progressView = (SpringProgressView) findViewById(R.id.spring_progress_view);progressView.setMaxCount(1000.0f);progressView1 = (SpringProgressView2) findViewById(R.id.spring_progress_view2);progressView1.setMaxCount(1000.0f);findViewById(R.id.click).setOnClickListener(this);findViewById(R.id.click_stop).setOnClickListener(this);progressView1.setCurrentCount(130);}Handler handle = new Handler() {public void handleMessage(android.os.Message msg) {int i = (Integer) msg.obj;switch (msg.what) {case 1:progressView.setCurrentCount(i);progressView1.setCurrentCount(i);textView.setText("最大值:" + progressView.getMaxCount()+ "   当前值:" + progressView.getCurrentCount());textView.setText("最大值:" + progressView1.getMaxCount()+ "   当前值:" + progressView1.getCurrentCount());break;default:break;}};};Runnable runnable = new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubwhile (i <= 1000 && flag) {i++;currentCount = i;try {Thread.sleep(10);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}Message msg = new Message();msg.what = 1;msg.obj = i;handle.sendMessage(msg);}}};private Thread runnable1;private Boolean flag;@SuppressWarnings("deprecation")@Overridepublic void onClick(View v) {// progressView.setCurrentCount(random.nextInt(1000));switch (v.getId()) {case R.id.click:i = currentCount;flag = true;runnable1 = new Thread(runnable);runnable1.start();break;case R.id.click_stop:flag = false;// if (runnable1 != null && runnable1.isAlive()) {// // Log.e("readCacheThread", "thread interrupt_1");// runnable1.interrupt();// // Log.e("status", ""+readCacheThread.isInterrupted());// }break;default:break;}}}


http://download.csdn.net/detail/wanghao200906/8157903

代码


0 0
原创粉丝点击