android圆形图片显示ImageView超简单控件
2017-03-02 17:51
435 查看
关于android显示圆形图片的控件
第一次写技术类博客,先来个简单的,话不多说,只是码上。1.先创建一个View。
public class RoundImageView extends ImageView { /** 图片的类型,圆形or圆角 */ private int type; private static final int TYPE_CIRCLE = 0; private static final int TYPE_ROUND = 1; /** 圆角大小的默认值 */ private static final int BODER_RADIUS_DEFAULT = 10; /** * 圆角的大小 */ private int mBorderRadius; /** * 绘图的Paint */ private Paint mBitmapPaint; /** * 圆角的半径 */ private int mRadius; /** * 3x3 矩阵,主要用于缩小放大 */ private Matrix mMatrix; /** * 渲染图像,使用图像为绘制图形着色 */ private BitmapShader mBitmapShader; /** * view的宽度 */ private int mWidth; private RectF mRoundRect; public RoundImageView(Context context, AttributeSet attrs) { super(context, attrs); mMatrix = new Matrix(); mBitmapPaint = new Paint(); mBitmapPaint.setAntiAlias(true); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView); mBorderRadius = a.getDimensionPixelSize( R.styleable.RoundImageView_borderRadius, (int) TypedValue .applyDimension(TypedValue.COMPLEX_UNIT_DIP, BODER_RADIUS_DEFAULT, getResources() .getDisplayMetrics()));// 默认为10dp type = a.getInt(R.styleable.RoundImageView_type, TYPE_CIRCLE);// 默认为Circle a.recycle(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (type == TYPE_CIRCLE) { mWidth = Math.min(getMeasuredWidth(), getMeasuredHeight()); mRadius = mWidth / 2; setMeasuredDimension(mWidth, mWidth); } } private void setUpShader() { Drawable drawable = getDrawable(); if (drawable == null) return; Bitmap bmp = drawableToBitamp(drawable); // 将bmp作为着色器,就是在指定区域内绘制bmp mBitmapShader = new BitmapShader(bmp, TileMode.CLAMP, TileMode.CLAMP); float scale = 1.0f; if (type == TYPE_CIRCLE) { int bSize = Math.min(bmp.getWidth(), bmp.getHeight()); scale = mWidth * 1.0f / bSize; } else if (type == TYPE_ROUND) { scale = Math.max(getWidth() * 1.0f / bmp.getWidth(), getHeight() * 1.0f / bmp.getHeight()); } // shader的变换矩阵,我们这里主要用于放大或者缩小 mMatrix.setScale(scale, scale); // 设置变换矩阵 mBitmapShader.setLocalMatrix(mMatrix); // 设置shader mBitmapPaint.setShader(mBitmapShader); } private Bitmap drawableToBitamp(Drawable drawable) { if (drawable instanceof BitmapDrawable) { BitmapDrawable bd = (BitmapDrawable) drawable; return bd.getBitmap(); } int w = drawable.getIntrinsicWidth(); int h = drawable.getIntrinsicHeight(); Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, w, h); drawable.draw(canvas); return bitmap; } @Override protected void onDraw(Canvas canvas) { if (getDrawable() == null) return; setUpShader(); if (type == TYPE_ROUND) { canvas.drawRoundRect(mRoundRect, mBorderRadius, mBorderRadius, mBitmapPaint); } else { canvas.drawCircle(mRadius, mRadius, mRadius, mBitmapPaint); } } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); // 圆角图片的范围 if (type == TYPE_ROUND) mRoundRect = new RectF(0, 0, getWidth(), getHeight()); } }
2.创建布局文件XML,”…”为该View的路径
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <...RoundImageView android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
完成了,觉得还可以的小伙伴,赶紧试试吧
相关文章推荐
- android 自定义ImageView控件实现圆形图片-适用于用户头像
- android 自定义ImageView显示圆形图片
- 安卓学习笔记(一)Android ImageButton、ImageView控件属性设置 图片显示问题
- Android自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示
- Android深入浅出系列之实例应用—简单的手指拖动图片,图片滑来滑去显示应用Gallery和BaseAdapter以及ImageView的使用
- Android 重定义ImageView可显示出圆形或圆角形图片效果
- android 自定义ImageView控件实现圆形图片-适用于用户头像
- Android imageview显示圆形图片
- Android自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示
- Android深入浅出系列之实例应用—简单的手指拖动图片,图片滑来滑去显示应用Gallery和BaseAdapter以及ImageView的使用
- Android圆形图片控件CircleImageView的使用
- 【Android 开发】:UI控件之显示图片控件 ImageView 的使用方法
- Android开发第三方库glide和圆形控件circleimageview一起使用第一次进入无法显示问题
- Android:ImageView控件显示图片
- Android自定义ImageView实现圆形控件显示
- Android ImageView,布局或控件显示网络图片教程
- Android圆形图片控件RoundedImageView
- Android自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示
- Android ImageView控件 按下与松开显示不同的背景图片
- 自定义圆形图片显示控件CircleImageView