今天找到一个比较不错的进度有关的素材,随即写了一个自定义控件HeartProgress,思路:先把图片绘制进去,然后根据进度值,截取图片自下而上的范围,进行裁剪,裁剪后的图片进行图片变色,然后重新绘制到View,公开方法setProgress (int progress)控制进度显示。(也可以换一种实现方式,设置xFermode..)下面是效果图:
自定义控件的基本使用都差不多,这里就就简单明一下,先获取心形背景图片的绝对宽高,进行onMeasure
mDrawable=getBackground(); mWidth=mDrawable.getIntrinsicWidth(); mHeight=mDrawable.getIntrinsicHeight();
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub setMeasuredDimension(mWidth, mHeight); }
public void caculater(){ mRectHeight=mHeight*mProgress/100;//这里必须先乘以mProgress再除以100 minX=0; minY=mHeight-mRectHeight; maxX=mWidth; }
Bitmap retBitmap = Bitmap.createBitmap(bgBitmap, minX, minY, maxX, mRectHeight, matrix, true);
canvas.drawBitmap(retBitmap, 0, mHeight-mRectHeight, mPaint); ColorMatrix mColorMatrix = new ColorMatrix(); mColorMatrix.set(getArrayFilter()); mPaint.setColorFilter(new ColorMatrixColorFilter(mColorMatrix)); canvas.drawBitmap(retBitmap, 0, mHeight-mRectHeight, mPaint);
public void setProgress(final int progress){ new Thread(){ public void run() { try { for (int i = 0; i <= progress; i++) { if(i>mProgress){ mProgress=i; postInvalidate(); Thread.sleep(100); }else if(i<mProgress){ mProgress--; postInvalidate(); Thread.sleep(100); } } Thread.interrupted(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start(); }
private float[] getArrayFilter() { float []arr=new float[20]; for (int i = 0; i < arr.length; i++) { if(i==0){ arr[i]=2.0f; }else if(i==6||i==12||i==18){ arr[i]=1.0f; }else{ arr[i]=0.0f; } } return arr; }
参考资料:http://www.cnblogs.com/menlsh/archive/2013/02/03/2890888.html
Demo地址:http://download.csdn.net/detail/analyzesystem/8919285