glide 加载圆角图片

效果如上, 这是ListView的一个item 左边是一个圆角矩形的背景. 使用glide加载图片

自定义一个FilletImageView, 代码如下:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ImageView;

/**
 * Created by Administrator on 2016/8/19.
 */
public class FilletImageView extends ImageView
{
    private Rect    rect = new Rect();
    private RectF   rectF = new RectF();
    private float   radius;
    private Bitmap  bitmap;
    private Xfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);

    public FilletImageView(Context context)
    {
        this(context, null);
    }

    public FilletImageView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
        try
        {
            if(android.os.Build.VERSION.SDK_INT >= 11)
            {
                setLayerType(LAYER_TYPE_SOFTWARE, null);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void setImageBitmap(Bitmap bm)
    {
        this.bitmap = bm;
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        if (bitmap == null)
            return;

        rect.set(0,0,getWidth(),getHeight());
        rectF.set(rect);

        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
        canvas.drawRoundRect(rectF, radius, radius, paint);
        paint.setXfermode(xfermode);
        canvas.drawBitmap(bitmap, null, rect, paint);
    }
}
View Code

然后在加载的时候列表项太多, 第一次没有显示出图像, 解决方法:在glide将图像下载成功后再加载. 代码如下:

好了, 一次可以全部显示了

参考:
http://blog.csdn.net/zz7zz7zz/article/details/11495517
http://www.cnblogs.com/zhaoyanjun/p/5404162.html

posted @ 2016-08-19 17:59  莫压枯枯地  阅读(819)  评论(0编辑  收藏  举报