Imagen de Android cargando ImageView con barra de progreso

En muchos casos, se necesita un ImageView con efecto de carga al cargar una imagen, pero en realidad es muy simple de implementar

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();  
    }  
  
}  
De hecho, no hay nada a lo que prestar atención, es uno de los controles personalizados más simples. Puede cambiarlo si es necesario.


Supongo que te gusta

Origin blog.csdn.net/shu_quan/article/details/79975578
Recomendado
Clasificación