本文实例讲述了Android开发基于Drawable实现圆角矩形的方法。分享给大家供大家参考,具体如下:
第一步:写个类继承drawable,重写里面的方法,实现的核心代码在draw里
关键技术:BitmapShader
public BitmapShader(Bitmap bitmap,Shader.TileMode tileX,Shader.TileMode tileY)
调用这个方法来产生一个画有一个位图的渲染器(Shader)。
bitmap 在渲染器内使用的位图
tileX The tiling mode for x to draw the bitmap in. 在位图上X方向渲染器平铺模式
tileY The tiling mode for y to draw the bitmap in. 在位图上Y方向渲染器平铺模式
TileMode:
CLAMP :如果渲染器超出原始边界范围,会复制范围内边缘染色。
REPEAT :横向和纵向的重复渲染器图片,平铺。
MIRROR :横向和纵向的重复渲染器图片,这个和REPEAT重复方式不一样,他是以镜像方式平铺。
/**
* 四种屏幕尺寸分类:: small, normal, large, and xlarge
* 四种密度分类: ldpi (low), mdpi (medium), hdpi (high), and xhdpi (extra high)
* 四种屏幕的DPI:ldpi是120dpi,mdpi是160dpi,hdpi是240dpi,xhdpi是320dpi
* 四种屏幕对应density:0.75,1,1.5,2
* 四种图片资源文件夹:drawable-ldpi,drawable-mdpi,drawable-hdpi,drawable-xhdpi
*/
/**
* 圆角
*
* @Project App_View
* @Package com.android.view.drawable
* @author chenlin
* @version 1.0
* @Note TODO
*/
public class RoundImageDrawable extends Drawable {
private Paint mPaint;
private Bitmap mBitmap;
private RectF mRectF;
private int mRound;
public RoundImageDrawable(Bitmap bitmap) {
this.mBitmap = bitmap;
mPaint = new Paint();
mPaint.setAntiAlias(true);
BitmapShader shader = new BitmapShader(mBitmap, TileMode.CLAMP, TileMode.CLAMP);
mPaint.setShader(shader);
}
/**
* 初始化区域
*/
@Override
public void setBounds(int left, int top, int right, int bottom) {
mRectF = new RectF(left, top, right, bottom);
super.setBounds(left, top, right, bottom);
}
/**
* 核心代码: 绘制圆角
*/
@Override
public void draw(Canvas canvas) {
canvas.drawRoundRect(mRectF, mRound, mRound, mPaint);
}
/**
* 暴露给外面设置圆角的大小
*
* @param round
*/
public void setRound(int round) {
this.mRound = round;
}
/**
* getIntrinsicWidth、getIntrinsicHeight主要是为了在View使用wrap_content的时候,
* 提供一下尺寸,默认为-1可不是我们希望的
*/
@Override
public int getIntrinsicHeight() {
return mBitmap.getHeight();
}
@Override
public int getIntrinsicWidth() {
return mBitmap.getWidth();
}
/**
* 根据画笔设定drawable的透明度
*/
@Override
public void setAlpha(int alpha) {
mPaint.setAlpha(alpha);
}
/**
* 根据画笔设定drawable的颜色过滤器
*/
@Override
public void setColorFilter(ColorFilter cf) {
mPaint.setColorFilter(cf);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}
第二步:实现类
public class RoundActivity extends Activity {
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_round_drawable);
setContentView(R.layout.activity_round_drawable);
mImageView = (ImageView) findViewById(R.id.iv_round);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.aa);
RoundImageDrawable drawable = new RoundImageDrawable(bitmap);
drawable.setRound(30);
mImageView.setImageDrawable(drawable); }
}
activity_round_drawable.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/iv_round"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY" />
绘制圆形图片
核心代码:
mWidth = Math.min(bitmap.getWidth(), bitmap.getHeight());
canvas.drawCircle(mWidth / 2, mWidth / 2, mRadius, mPaint);
/**
* 圆形
* @Project App_View
* @Package com.android.view.drawable
* @author chenlin
* @version 1.0
* @Note TODO
*/
public class CircleImageDrawable extends Drawable {
private Bitmap mBitmap;
private Paint mPaint;
private int mWidth;
private int mRadius;
public CircleImageDrawable(Bitmap bitmap){
this.mBitmap = bitmap;
mPaint.setAntiAlias(true);
BitmapShader shader = new BitmapShader(mBitmap, TileMode.CLAMP, TileMode.CLAMP);
mPaint.setShader(shader);
mWidth = Math.min(bitmap.getWidth(), bitmap.getHeight());
mRadius = mWidth / 2;
}
/**
* 核心代码
*/
@Override
public void draw(Canvas canvas) {
canvas.drawCircle(mWidth / 2, mWidth / 2, mRadius, mPaint);
}
/**
* getIntrinsicWidth、getIntrinsicHeight主要是为了在View使用wrap_content的时候,
* 提供一下尺寸,默认为-1可不是我们希望的
*/
@Override
public int getIntrinsicHeight() {
return mWidth;
}
@Override
public int getIntrinsicWidth() {
return mWidth;
}
@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;
}
}
希望本文所述对大家Android程序设计有所帮助。