Android自定义圆角ImageView
程序员文章站
2024-02-12 14:51:34
废话不多说了,直接给大家贴代码了。
java类如下:
import android.content.context;
import android.con...
废话不多说了,直接给大家贴代码了。
java类如下:
import android.content.context; import android.content.res.typedarray; import android.graphics.bitmap; import android.graphics.bitmap.config; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.path; import android.graphics.porterduff; import android.graphics.porterduffxfermode; import android.graphics.rectf; import android.util.attributeset; import android.widget.imageview; import cn.dotcreate.tt.r; public class roundangleimageview extends imageview { private paint paint; private int roundwidth = 5; private int roundheight = 5; private paint paint2; public roundangleimageview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(context, attrs); } public roundangleimageview(context context, attributeset attrs) { super(context, attrs); init(context, attrs); } public roundangleimageview(context context) { super(context); init(context, null); } private void init(context context, attributeset attrs) { if(attrs != null) { typedarray a = context.obtainstyledattributes(attrs, r.styleable.roundangleimageview); roundwidth= a.getdimensionpixelsize(r.styleable.roundangleimageview_roundwidth, roundwidth); roundheight= a.getdimensionpixelsize(r.styleable.roundangleimageview_roundheight, roundheight); }else { float density = context.getresources().getdisplaymetrics().density; roundwidth = (int) (roundwidth*density); roundheight = (int) (roundheight*density); } paint = new paint(); paint.setcolor(color.white); paint.setantialias(true); paint.setxfermode(new porterduffxfermode(porterduff.mode.dst_out)); paint2 = new paint(); paint2.setxfermode(null); } @override public void draw(canvas canvas) { bitmap bitmap = bitmap.createbitmap(getwidth(), getheight(), config.argb_8888); canvas canvas2 = new canvas(bitmap); super.draw(canvas2); drawliftup(canvas2); drawrightup(canvas2); drawliftdown(canvas2); drawrightdown(canvas2); canvas.drawbitmap(bitmap, 0, 0, paint2); bitmap.recycle(); } private void drawliftup(canvas canvas) { path path = new path(); path.moveto(0, roundheight); path.lineto(0, 0); path.lineto(roundwidth, 0); path.arcto(new rectf( 0, 0, roundwidth*2, roundheight*2), -90, -90); path.close(); canvas.drawpath(path, paint); } private void drawliftdown(canvas canvas) { path path = new path(); path.moveto(0, getheight()-roundheight); path.lineto(0, getheight()); path.lineto(roundwidth, getheight()); path.arcto(new rectf( 0, getheight()-roundheight*2, 0+roundwidth*2, getheight()), 90, 90); path.close(); canvas.drawpath(path, paint); } private void drawrightdown(canvas canvas) { path path = new path(); path.moveto(getwidth()-roundwidth, getheight()); path.lineto(getwidth(), getheight()); path.lineto(getwidth(), getheight()-roundheight); path.arcto(new rectf( getwidth()-roundwidth*2, getheight()-roundheight*2, getwidth(), getheight()), 0, 90); path.close(); canvas.drawpath(path, paint); } private void drawrightup(canvas canvas) { path path = new path(); path.moveto(getwidth(), roundheight); path.lineto(getwidth(), 0); path.lineto(getwidth()-roundwidth, 0); path.arcto(new rectf( getwidth()-roundwidth*2, 0, getwidth(), 0+roundheight*2), -90, 90); path.close(); canvas.drawpath(path, paint); } }
定义一个attr.xml的文件,放在values目录下面,内容如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="roundangleimageview"> <attr name="roundwidth" format="dimension" /> <attr name="roundheight" format="dimension" /> </declare-styleable> </resources>
使用示例如下:
先要声明属性的名字空间:
然后再写跟一般定义view一样:
<cn.dotcreate.tt.ui.roundangleimageview android:id="@+id/headiv" android:layout_width="75dp" android:layout_height="75dp" android:layout_centervertical="true" android:layout_marginleft="2dp" app:roundwidth="10dp" app:roundheight="10dp" android:src="@drawable/default_head_icon" />
效果如图:
以上代码简单介绍了android自定义圆角imageview的相关知识,希望本文分享对大家有所帮助。
推荐阅读
-
Android-自定义控件之ListView下拉刷新的实现
-
Android开发实现popupWindow弹出窗口自定义布局与位置控制方法
-
Android自定义闹钟功能
-
自定义Android圆形进度条(附源码)
-
Android中Dialog自定义上下文花式菜单
-
Android RecyclerView自定义上拉和下拉刷新效果
-
Android 自定义可拖拽View界面渲染刷新后不会自动回到起始位置
-
Android 中自定义ContentProvider与ContentObserver的使用简单实例
-
Android开发实现自定义水平滚动的容器示例
-
Android自定义GestureDetector实现手势ImageView