废话不多说,先上效果图:
这个进度就是模拟心形填充的过程
实现原理是:首先将空心形图片绘制出来,然后根据进度大小再将实心的心形图片一部分一部分的绘制出来。也就是两张完全相同大小的空心图和圆心图重叠绘制,利用canvas的clipRect绘制实心部分,实现进度条效果。
首先在res->values先新建一个文件atrrs.xml,然后定义两个属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="HeartProgressBar">
<attr name="max" format="integer"></attr>
<attr name="progress" format="integer"></attr>
</declare-styleable>
</resources>
max是最大进度值,progress是当前进度。
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import com.helen.funnyview.R;
/**
* Created by Helen on 2015/5/14.
*/
public class HeartProgressBar extends View{
private Bitmap mHeartBitmap;//空心图片
private Bitmap mHeartedBitmap;//实心图
private Paint mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
private int progress=0;//当前进度
priv