自定义Drawable实现圆角图片和圆形图片

转载请注明原博客地址:http://write.blog.csdn.net/postedit/51226071


例子源码下载地址: https://github.com/gdutxiaoxu/CustomDrawab le

 1 自定义圆角图片

 a 继承drawable,重写里面的几个方法
 b 通过构造方法传入Bitmap对象,初始化一个BitmapShader,将其传递给我们的画笔   
public RoundImageDrawable(Bitmap bitmap) {
mBitmap = bitmap;
    BitmapShader bitmapShader = new BitmapShader(bitmap, TileMode.CLAMP,
            TileMode.CLAMP);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setShader(bitmapShader);
}
c 在setBounds()初始化一个 ReactF
@Override
public void setBounds(int left, int top, int right, int bottom) {
super.setBounds(left, top, right, bottom);
rectF = new RectF(left, top, right, bottom);
}
 d 在draw()方法里面调用
  canvas.draw()传入我们的矩形对象和画笔
canvas.drawRoundRect(rectF, 30, 30, mPaint);
效果图如下
自定义Drawable实现圆角图片和圆形图片_第1张图片
源码如下:
package com.xujun.administrator.customdrawable.view;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable;

public class RoundImageDrawable extends Drawable {

    private Paint mPaint;
    private Bitmap mBitmap;

    private RectF rectF;

    public RoundImageDrawable(Bitmap bitmap) {
        mBitmap = bitmap;
        BitmapShader bitmapShader = new BitmapShader(bitmap, TileMode.CLAMP,
                TileMode.CLAMP);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setShader(bitmapShader);
    }

    @Override
    public void setBounds(int left, int top, int right, int bottom) {
        super.setBounds(left, top, right, bottom);
        rectF = new RectF(left, top, right, bottom);
    }

    @Override
    public void draw(Canvas canvas) {
//圆角的 半斤可以通过参数传递进来,这里写死了
        canvas.drawRoundRect(rectF, 30, 30, mPaint);

    }

    @Override
    public int getIntrinsicWidth() {
        return mBitmap.getWidth();
    }

    @Override
    public int getIntrinsicHeight() {
        return mBitmap.getHeight();
    }

    @Override
    public void setAlpha(int alpha) {
        mPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        mPaint.setColorFilter(cf);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

}


          2  自定义圆形图片

 a 继承drawable,重写里面的几个方法
 b 通过构造方法传入Bitmap对象,初始化一个BitmapShader,将其传递给我们的画笔   
    public CircleImageDrawable(Bitmap bitmap) {
        mBitmap = bitmap;
        BitmapShader bitmapShader = new BitmapShader(bitmap, TileMode.CLAMP,
                TileMode.CLAMP);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setShader(bitmapShader);
//        选择我们的半径为较小的那个
        mWidth = Math.min(mBitmap.getWidth(), mBitmap.getHeight());
    }


c 在Draw()方法里面调用
  canvas.draw()传入我们的矩形对象和画笔
canvas.drawCircle(mWidth / 2, mWidth / 2, mWidth / 2, mPaint);

效果图如下
自定义Drawable实现圆角图片和圆形图片_第2张图片

例子源码下载地址:
https://github.com/gdutxiaoxu/CustomDrawable

你可能感兴趣的:(自定义Drawable实现圆角图片和圆形图片)