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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gdutxiaoxu/article/details/51226071

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

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

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);

效果图如下

源码如下:

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);

效果图如下

例子源码下载地址:

https://github.com/gdutxiaoxu/CustomDrawable

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

扫码关注云+社区

领取腾讯云代金券