Android自己定义圆角ImageView
2017-05-13 15:51
190 查看
我们常常看到一些app中能够显示圆角图片。比方qq的联系人图标等等,实现圆角图片一种办法是直接使用圆角图片资源,当然假设没有圆角图片资源。我们也能够自己通过程序实现的,以下介绍一个自己定义圆角ImageView的方法:
package com.yulongfei.imageview; 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; public class RoundAngleImageView extends ImageView { private int roundWidth = 13; private int roundHeight = 13; public RoundAngleImageView(Context context) { super(context); init(context, null); } 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); } 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); a.recycle(); } else { float density = context.getResources().getDisplayMetrics().density; roundWidth = (int)(roundWidth * density); roundHeight = (int)(roundHeight * density); } } /** 重写draw() */ @Override public void draw(Canvas canvas) { //实例化一个和ImageView一样大小的bitmap Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888); //实例化一个canvas,这个canvas相应的内存为上面的bitmap Canvas canvas2 = new Canvas(bitmap); if (bitmap.isRecycled()) { bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888); canvas2 = new Canvas(bitmap); } //将imageView自己绘制到canvas2上,这个导致bitmap里面存放了imageView super.draw(canvas2); //利用canvas画一个圆角矩形,这个会改动bitmap的数据 drawRoundAngle(canvas2); //将裁剪好的bitmap绘制到系统当前canvas上,这样裁剪好的imageview就能显示到屏幕上 Paint paint = new Paint(); paint.setXfermode(null); canvas.drawBitmap(bitmap, 0, 0, paint); bitmap.recycle(); } public void setRoundWidth(int roundWidth, int roundHeight) { this.roundWidth = roundWidth; this.roundHeight = roundHeight; } private void drawRoundAngle(Canvas canvas) { Paint maskPaint = new Paint(); maskPaint.setAntiAlias(true); maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); Path maskPath = new Path(); maskPath.addRoundRect(new RectF(0.0F, 0.0F, getWidth(), getHeight()), roundWidth, roundHeight, Path.Direction.CW); //这是设置了填充模式。很关键 maskPath.setFillType(Path.FillType.INVERSE_WINDING); canvas.drawPath(maskPath, maskPaint); } }
相关文章推荐
- android在假设绘制自己定义的bitmap,然后返回给ImageView
- Android 自己定义ImageView实现圆角/圆形 附加OnTouchListener具体凝视以及Button圆角
- Android自己定义圆角ImageView 支持网络图片
- 【Android】自己定义控件——仿天猫Indicator
- Android中通过反射获取资源Id(特别用在自己定义一个工具将其打成.jar包时,特别注意资源的获取)
- android ui定义自己的dialog(项目框架搭建时就写好,之后事半功倍)
- android下为自己定义wifimanager类
- Android中常用适配器及定义自己的适配器
- Android中常用适配器及定义自己的适配器
- Android中常用适配器及定义自己的适配器
- Android中常用适配器及定义自己的适配器
- Android-自己定义图像资源的使用(1)
- Android中ContentProvider的实现及定义自己的ContentProvider
- Android自己定义控件而且使其能够在xml中自己定义属性
- android中自己定义dialog
- Android自己定义组件系列【3】——自己定义ViewGroup实现側滑
- Android 如何自己定义控件的样式 Shape
- Android中常用适配器及定义自己的适配器
- android 使用xml定义自己的View
- android ui定义自己的dialog