Android 图片加载带进度条的ImageView

很多时候图片上传的时候需要一个有加载效果的ImageView,其实实现起来也非常简单

public class LoadingImgView extends ImageView{  
  
    private float per;  
  
    private boolean isfinished = false;  
  
    private String colorStr;  
  
    private Paint paintLayer;  
    private Paint textPaint;  
  
    private Rect textbound;  
  
    private float layer_w;  
    private float layer_h;  
  
    public LoadingImgView(Context context) {  
        super(context);  
        init();  
    }  
  
    public LoadingImgView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        init();  
    }  
  
    public LoadingImgView(Context context, AttributeSet attrs, int defStyleAttr) {  
        super(context, attrs, defStyleAttr);  
        init();  
    }  
  
    //初始化画笔  
    private void init(){  
        paintLayer = new Paint();  
        paintLayer.setColor(Color.LTGRAY);  
        paintLayer.setAlpha(100);  
        textPaint = new Paint();  
        textPaint.setColor(Color.DKGRAY);  
        textPaint.setTextSize(25);  
        textbound = new Rect();  
    }  
  
    @Override  
    protected void onDraw(Canvas canvas) {  
        super.onDraw(canvas);  
        if (isfinished)  
            return;  
        String perStr = (int) (per*100) + "%";  
        //获取文字区域的矩形大小,以便确定文字正中间的位置  
        textPaint.getTextBounds(perStr,0, perStr.length(),textbound);  
        layer_w = getWidth();  
        layer_h = getHeight()*per;  
        float y = getHeight() - layer_h;  
        //画遮蔽层  
        canvas.drawRect(0,y,layer_w,getHeight(),paintLayer);  
        //画文字  
        canvas.drawText(perStr, getWidth() / 2 - textbound.width() / 2, getHeight() / 2 + textbound.height() / 2, textPaint);  
    }  
  
    public void setPer(float per){  
        this.per = per;  
        //在主线程刷新  
        postInvalidate();  
    }  
  
    public void finish(){  
        isfinished = true;  
        postInvalidate();  
    }  
  
}  
其实也没什么要注意的地方,算是最简单的自定义控件之一了。有需要的可以改一改。


猜你喜欢

转载自blog.csdn.net/shu_quan/article/details/79975578