Android开发使用自定义View将圆角矩形绘制在Canvas上的方法
本文实例讲述了android开发使用自定义view将圆角矩形绘制在canvas上的方法。分享给大家供大家参考,具体如下:
前几天,公司一个项目中,头像图片需要添加圆角,这样ui效果会更好看,于是写了一个小的demo进行圆角的定义,该处主要是使用bitmapshader进行了渲染(如果要将一张图片裁剪成椭圆或圆形显示在屏幕上,也可以使用bitmapshader来完成).
bitmapshader类完成渲染图片的基本步骤如下:
1、创建bitmapshader类的对象
/** * call this to create a new shader that will draw with a bitmap. * * @param bitmap the bitmap to use inside the shader * @param tilex the tiling mode for x to draw the bitmap in. * @param tiley the tiling mode for y to draw the bitmap in. */ public bitmapshader(bitmap bitmap, tilemode tilex, tilemode tiley) { ...... }
其中,shader.titlemode类型有三种,calmp、mirror、repeat
calmp:使用边界颜色来填充剩余空间
mirror:使用镜像方式
repeat:使用重复方式
2、通过paint的setshader(bitmapshafer)
来设置画笔
3、使用已经setshader(bitmapshafer)
的画笔来绘制图形
下面展示绘制圆角图片的demo
1、自定义roundercornerimageview.java类
package com.example.test; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapshader; import android.graphics.canvas; import android.graphics.matrix; import android.graphics.paint; import android.graphics.rectf; import android.graphics.shader; import android.util.attributeset; import android.view.view; public class roundercornerimageview extends view { private bitmap mimage;// source bitmap private paint mbitmappaint;//paint private rectf mbrounds;//rect private float mradius=20.0f;//round public roundercornerimageview(context context) { this(context, null); } public roundercornerimageview(context context, attributeset attrs) { this(context, attrs, 0); } public roundercornerimageview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); init(); } private void init() { mbitmappaint=new paint(paint.anti_alias_flag); mbrounds=new rectf(); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { // todo auto-generated method stub int height,width; height=width=0; //obtain bitmap size int imageheight,imagewidth; if (null!=mimage) { imageheight=imagewidth=0; }else { imageheight=mimage.getheight(); imagewidth=mimage.getwidth(); } //obtain best measure data and set on view width=getmeasurement(widthmeasurespec,imagewidth); height=getmeasurement(heightmeasurespec, imageheight); //set view last size setmeasureddimension(width, height); } /** * measure width and height by specmode **/ private int getmeasurement(int measurespec, int contentsize) { int specsize=measurespec.getsize(measurespec); switch (measurespec.getmode(measurespec)) { case measurespec.at_most: return math.min(specsize, contentsize); case measurespec.unspecified: return contentsize; case measurespec.exactly: return specsize; default: return 0; }//switch } @override protected void onsizechanged(int w, int h, int oldw, int oldh) { if (w!=oldw || h!=oldh) { int imagewidth,imageheight; if (null==mimage) { imagewidth=imageheight=0; }else { imagewidth=mimage.getwidth(); imageheight=mimage.getheight(); } //center point int left=(w-imagewidth)/2; int top=(h-imageheight)/2; mbrounds.set(left, top, left+imagewidth, top+imageheight); if (null!=mbitmappaint.getshader()) { matrix m=new matrix(); m.settranslate(left, top); mbitmappaint.getshader().setlocalmatrix(m); } } } public void setimage(bitmap bitmap) { if (mimage!=bitmap) { mimage=bitmap; if (null!=mimage) { bitmapshader shader=new bitmapshader(bitmap, shader.tilemode.clamp, shader.tilemode.clamp); mbitmappaint.setshader(shader); }else { mbitmappaint.setshader(null); } requestlayout();//invalidated the layout of this view by ondraw() } } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); if (null!=mbitmappaint) { //draw round rect canvas.drawroundrect(mbrounds, mradius, mradius, mbitmappaint); } } }
2、显示圆角图片的roundactivity.java类
package com.example.test; import android.app.activity; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.os.bundle; public class roundactivity extends activity{ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); roundercornerimageview view=new roundercornerimageview(this); bitmap soubitmap=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程序设计有所帮助。