Android开发使用自定义View将圆角矩形绘制在Canvas上的方法
本文实例讲述了Android开发使用自定义View将圆角矩形绘制在Canvas上的方法。分享给大家供大家参考,具体如下:
前几天,公司一个项目中,头像图片需要添加圆角,这样UI效果会更好看,于是写了一个小的demo进行圆角的定义,该处主要是使用BitmapShader进行了渲染(如果要将一张图片裁剪成椭圆或圆形显示在屏幕上,也可以使用BitmapShader来完成).
BitmapShader类完成渲染图片的基本步骤如下:
1、创建BitmapShader类的对象
/**
*Callthistocreateanewshaderthatwilldrawwithabitmap.
*
*@parambitmapThebitmaptouseinsidetheshader
*@paramtileXThetilingmodeforxtodrawthebitmapin.
*@paramtileYThetilingmodeforytodrawthebitmapin.
*/
publicBitmapShader(Bitmapbitmap,TileModetileX,TileModetileY){
......
}
其中,Shader.TitleMode类型有三种,CALMP、MIRROR、REPEAT
CALMP:使用边界颜色来填充剩余空间
MIRROR:使用镜像方式
REPEAT:使用重复方式
2、通过Paint的setShader(bitmapShafer)来设置画笔
3、使用已经setShader(bitmapShafer)的画笔来绘制图形
下面展示绘制圆角图片的demo
1、自定义RounderCornerImageView.java类
packagecom.example.test;
importandroid.content.Context;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapShader;
importandroid.graphics.Canvas;
importandroid.graphics.Matrix;
importandroid.graphics.Paint;
importandroid.graphics.RectF;
importandroid.graphics.Shader;
importandroid.util.AttributeSet;
importandroid.view.View;
publicclassRounderCornerImageViewextendsView{
privateBitmapmImage;//sourcebitmap
privatePaintmBitmapPaint;//paint
privateRectFmBrounds;//rect
privatefloatmRadius=20.0f;//round
publicRounderCornerImageView(Contextcontext){
this(context,null);
}
publicRounderCornerImageView(Contextcontext,AttributeSetattrs){
this(context,attrs,0);
}
publicRounderCornerImageView(Contextcontext,AttributeSetattrs,
intdefStyleAttr){
super(context,attrs,defStyleAttr);
init();
}
privatevoidinit(){
mBitmapPaint=newPaint(Paint.ANTI_ALIAS_FLAG);
mBrounds=newRectF();
}
@Override
protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
//TODOAuto-generatedmethodstub
intheight,width;
height=width=0;
//obtainbitmapsize
intimageHeight,imageWidth;
if(null!=mImage){
imageHeight=imageWidth=0;
}else
{
imageHeight=mImage.getHeight();
imageWidth=mImage.getWidth();
}
//obtainbestmeasuredataandsetonView
width=getMeasurement(widthMeasureSpec,imageWidth);
height=getMeasurement(heightMeasureSpec,imageHeight);
//setViewlastsize
setMeasuredDimension(width,height);
}
/**
*measurewidthandheightbyspecMode
**/
privateintgetMeasurement(intmeasureSpec,intcontentSize){
intspecSize=MeasureSpec.getSize(measureSpec);
switch(MeasureSpec.getMode(measureSpec)){
caseMeasureSpec.AT_MOST:
returnMath.min(specSize,contentSize);
caseMeasureSpec.UNSPECIFIED:
returncontentSize;
caseMeasureSpec.EXACTLY:
returnspecSize;
default:
return0;
}//switch
}
@Override
protectedvoidonSizeChanged(intw,inth,intoldw,intoldh){
if(w!=oldw||h!=oldh){
intimageWidth,imageHeight;
if(null==mImage){
imageWidth=imageHeight=0;
}else
{
imageWidth=mImage.getWidth();
imageHeight=mImage.getHeight();
}
//centerpoint
intleft=(w-imageWidth)/2;
inttop=(h-imageHeight)/2;
mBrounds.set(left,top,left+imageWidth,top+imageHeight);
if(null!=mBitmapPaint.getShader()){
Matrixm=newMatrix();
m.setTranslate(left,top);
mBitmapPaint.getShader().setLocalMatrix(m);
}
}
}
publicvoidsetImage(Bitmapbitmap){
if(mImage!=bitmap){
mImage=bitmap;
if(null!=mImage){
BitmapShadershader=newBitmapShader(bitmap,Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
mBitmapPaint.setShader(shader);
}else{
mBitmapPaint.setShader(null);
}
requestLayout();//invalidatedthelayoutofthisviewbyonDraw()
}
}
@Override
protectedvoidonDraw(Canvascanvas){
super.onDraw(canvas);
if(null!=mBitmapPaint){
//drawRoundRect
canvas.drawRoundRect(mBrounds,mRadius,mRadius,mBitmapPaint);
}
}
}
2、显示圆角图片的RoundActivity.java类
packagecom.example.test;
importandroid.app.Activity;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.os.Bundle;
publicclassRoundActivityextendsActivity{
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
RounderCornerImageViewview=newRounderCornerImageView(this);
BitmapsouBitmap=BitmapFactory.decodeResource(getResources(),R.drawable.sun);
view.setImage(souBitmap);
setContentView(view);
}
}
另外,附注下自定义View的一些基本步骤和必须实现的方法
1、继承view
2、重写自定义View的构造方法
3、如需要对view进行位置进行测量和重写布局,则需要重写onMeasure()、onLayout()、onDraw()方法
onMeasure():view本身大小多少,可以测量出来
onLayout():view在ViewGroup中的位置可以决定
onDraw():定义了如何绘制该view
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android控件用法总结》、《Android开发入门与进阶教程》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android数据库操作技巧总结》及《Android资源操作技巧汇总》
希望本文所述对大家Android程序设计有所帮助。