扩展Drawable,使用BitmapShader实现圆角图,灵活性强
package com.example.robin.study.drawable;
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;
import android.graphics.drawable.Drawable;
/**
* 描述:
* Created by Robin on 2015/4/4 19:11.
* Update by Robin on 2015/4/4 19:11.
*/
public class SelectRoundDrawable extends Drawable {
private Paint mPaint;
private Bitmap mBitmap;
private RectF mRect;
public SelectRoundDrawable(Bitmap bitmap) {
mBitmap=bitmap;
//初始化BitmapShader
BitmapShader mBitmapShader=new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
mPaint=new Paint();
mPaint.setAntiAlias(true);
mPaint.setShader(mBitmapShader);
}
@Override
public void setBounds(int left, int top, int right, int bottom) {
super.setBounds(left, top, right, bottom);
//在设置边界方法中,创建RectF
mRect=new RectF(left,top,right,bottom);
}
@Override
public void draw(Canvas canvas) {
//用设置了shader的画笔画圆角矩形
canvas.drawRoundRect(mRect,30,30,mPaint);
}
/**
* 当布局为wrap_content时,默认返回图片的宽度
* @return 图片宽度
*/
@Override
public int getIntrinsicWidth() {
return mBitmap.getWidth();
}
/**
* 当布局为wrap_content时,默认返回图片的高度
* @return 图片高度
*/
@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;
}
}
转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/10400.html
微信打赏
支付宝打赏
感谢您对作者wangbin的打赏,我们会更加努力! 如果您想成为作者,请点我