实现自定义图片View圆形、圆角,椭圆
2017-11-21 12:01
323 查看
显示图片的时候为美观需要对图片进行形状上的处理,以下实现了3种,圆形。椭圆,矩形四个角带有弧度的
1、自定义ZQImageViewRoundOval.java
public class ZQImageViewRoundOval extends ImageView {
private Paint mPaint;
private int mWidth;
private int mHeight;
private int mRadius;// 圆半径
private RectF mRect;// 矩形凹行大小
private int mRoundRadius;// 圆角大小
private BitmapShader mBitmapShader;// 图形渲染
private Matrix mMatrix;
private int mType;// 记录是圆形还是圆角矩形
public static final int TYPE_CIRCLE = 0;// 圆形
public static final int TYPE_ROUND = 1;// 圆角矩形
public static final int TYPE_OVAL = 2;// 椭圆形
public static final int DEFAUT_ROUND_RADIUS = 10;// 默认圆角大小
public ZQImageViewRoundOval(Context context) {
this(context, null);
}
public ZQImageViewRoundOval(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ZQImageViewRoundOval(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
initView();
}
private void initView() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mMatrix = new Matrix();
mRoundRadius = DEFAUT_ROUND_RADIUS;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 如果是绘制圆形,则强制宽高大小一致
if (mType == TYPE_CIRCLE) {
mWidth = Math.min(getMeasuredWidth(), getMeasuredHeight());
mRadius = mWidth / 2;
setMeasuredDimension(mWidth, mWidth);
}
}
@Override
protected void onDraw(Canvas canvas) {
if (null == getDrawable()) {
return;
}
setBitmapShader();
if (mType == TYPE_CIRCLE) {
canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
} else if (mType == TYPE_ROUND) {
mPaint.setColor(Color.RED);
canvas.drawRoundRect(mRect, mRoundRadius, mRoundRadius, mPaint);
} else if (mType == TYPE_OVAL) {
canvas.drawOval(mRect, mPaint);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mRect = new RectF(0, 0, getWidth(), getHeight());
}
/**
* 设置BitmapShader
*/
private void setBitmapShader() {
Drawable drawable = getDrawable();
if (null == drawable) {
return;
}
Bitmap bitmap = drawableToBitmap(drawable);
// 将bitmap作为着色器来创建一个BitmapShader
mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP);
float scale = 1.0f;
if (mType == TYPE_CIRCLE) {
// 拿到bitmap宽或高的小值
int bSize = Math.min(bitmap.getWidth(), bitmap.getHeight());
scale = mWidth * 1.0f / bSize;
} else if (mType == TYPE_ROUND || mType == TYPE_OVAL) {
// 如果图片的宽或者高与view的宽高不匹配,计算出需要缩放的比例;缩放后的图片的宽高,一定要大于我们view的宽高;所以我们这里取大值;
scale = Math.max(getWidth() * 1.0f / bitmap.getWidth(), getHeight()
* 1.0f / bitmap.getHeight());
}
// shader的变换矩阵,我们这里主要用于放大或者缩小
mMatrix.setScale(scale, scale);
// 设置变换矩阵
mBitmapShader.setLocalMatrix(mMatrix);
mPaint.setShader(mBitmapShader);
}
/**
* drawable转bitmap
*
* @paramdrawable
* @return
*/
private Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
return bitmapDrawable.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);
retur
4000
n bitmap;
}
/**
* 单位dp转单位px
*/
public int dpTodx(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
getResources().getDisplayMetrics());
}
public int getType() {
return mType;
}
/**
* 设置图片类型:圆形、圆角矩形、椭圆形
*
* @param mType
*/
public void setType(int mType) {
if (this.mType != mType) {
this.mType = mType;
invalidate();
}
}
public int getRoundRadius() {
return mRoundRadius;
}
/**
* 设置圆角大小
*
* @parammRoundRadius
*/
public void setRoundRadius(int mRoundRadius) {
if (this.mRoundRadius != mRoundRadius) {
this.mRoundRadius = mRoundRadius;
invalidate();
}
}
}
2、使用自定义view的地方需要设置背景 img_icon_shape.xml 这个是放在 drawable文件夹下的
3、Activity 中调用方法进行显示
ZQImageViewRoundOvalmImg_info_icon = (ZQImageViewRoundOval) findViewById(R.id.img_info_icon);
mImg_info_icon.setType(ZQImageViewRoundOval.TYPE_ROUND);
mImg_info_icon.setRoundRadius(10);// 矩形凹行大小
mImg_info_icon.setBackgroundResource(R.drawable.ic_launcher);
这里是显示本地的图片,也可以加载网络图片
1、自定义ZQImageViewRoundOval.java
public class ZQImageViewRoundOval extends ImageView {
private Paint mPaint;
private int mWidth;
private int mHeight;
private int mRadius;// 圆半径
private RectF mRect;// 矩形凹行大小
private int mRoundRadius;// 圆角大小
private BitmapShader mBitmapShader;// 图形渲染
private Matrix mMatrix;
private int mType;// 记录是圆形还是圆角矩形
public static final int TYPE_CIRCLE = 0;// 圆形
public static final int TYPE_ROUND = 1;// 圆角矩形
public static final int TYPE_OVAL = 2;// 椭圆形
public static final int DEFAUT_ROUND_RADIUS = 10;// 默认圆角大小
public ZQImageViewRoundOval(Context context) {
this(context, null);
}
public ZQImageViewRoundOval(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ZQImageViewRoundOval(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
initView();
}
private void initView() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mMatrix = new Matrix();
mRoundRadius = DEFAUT_ROUND_RADIUS;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 如果是绘制圆形,则强制宽高大小一致
if (mType == TYPE_CIRCLE) {
mWidth = Math.min(getMeasuredWidth(), getMeasuredHeight());
mRadius = mWidth / 2;
setMeasuredDimension(mWidth, mWidth);
}
}
@Override
protected void onDraw(Canvas canvas) {
if (null == getDrawable()) {
return;
}
setBitmapShader();
if (mType == TYPE_CIRCLE) {
canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
} else if (mType == TYPE_ROUND) {
mPaint.setColor(Color.RED);
canvas.drawRoundRect(mRect, mRoundRadius, mRoundRadius, mPaint);
} else if (mType == TYPE_OVAL) {
canvas.drawOval(mRect, mPaint);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mRect = new RectF(0, 0, getWidth(), getHeight());
}
/**
* 设置BitmapShader
*/
private void setBitmapShader() {
Drawable drawable = getDrawable();
if (null == drawable) {
return;
}
Bitmap bitmap = drawableToBitmap(drawable);
// 将bitmap作为着色器来创建一个BitmapShader
mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP);
float scale = 1.0f;
if (mType == TYPE_CIRCLE) {
// 拿到bitmap宽或高的小值
int bSize = Math.min(bitmap.getWidth(), bitmap.getHeight());
scale = mWidth * 1.0f / bSize;
} else if (mType == TYPE_ROUND || mType == TYPE_OVAL) {
// 如果图片的宽或者高与view的宽高不匹配,计算出需要缩放的比例;缩放后的图片的宽高,一定要大于我们view的宽高;所以我们这里取大值;
scale = Math.max(getWidth() * 1.0f / bitmap.getWidth(), getHeight()
* 1.0f / bitmap.getHeight());
}
// shader的变换矩阵,我们这里主要用于放大或者缩小
mMatrix.setScale(scale, scale);
// 设置变换矩阵
mBitmapShader.setLocalMatrix(mMatrix);
mPaint.setShader(mBitmapShader);
}
/**
* drawable转bitmap
*
* @paramdrawable
* @return
*/
private Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
return bitmapDrawable.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);
retur
4000
n bitmap;
}
/**
* 单位dp转单位px
*/
public int dpTodx(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
getResources().getDisplayMetrics());
}
public int getType() {
return mType;
}
/**
* 设置图片类型:圆形、圆角矩形、椭圆形
*
* @param mType
*/
public void setType(int mType) {
if (this.mType != mType) {
this.mType = mType;
invalidate();
}
}
public int getRoundRadius() {
return mRoundRadius;
}
/**
* 设置圆角大小
*
* @parammRoundRadius
*/
public void setRoundRadius(int mRoundRadius) {
if (this.mRoundRadius != mRoundRadius) {
this.mRoundRadius = mRoundRadius;
invalidate();
}
}
}
2、使用自定义view的地方需要设置背景 img_icon_shape.xml 这个是放在 drawable文件夹下的
3、Activity 中调用方法进行显示
ZQImageViewRoundOvalmImg_info_icon = (ZQImageViewRoundOval) findViewById(R.id.img_info_icon);
mImg_info_icon.setType(ZQImageViewRoundOval.TYPE_ROUND);
mImg_info_icon.setRoundRadius(10);// 矩形凹行大小
mImg_info_icon.setBackgroundResource(R.drawable.ic_launcher);
这里是显示本地的图片,也可以加载网络图片
相关文章推荐
- 安卓学习笔记---自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示(矩形圆角加边框)
- Android自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示
- Android使用BitmapShader图形渲染实现圆形、圆角和椭圆自定义图片View
- Android自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示
- Android自定义view实现圆形、圆角和椭圆图片(BitmapShader图形渲染)
- Android自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示
- Android 自定义View修炼-Android实现圆形、圆角和椭圆自定义图片View(使用BitmapShader图形渲染方法)
- 自定义imageVIew实现圆角和圆形图,片圆形图片缩小放大正常,圆角图片放大缩小有点问题
- 用开源项目RoundedImageView来实现 圆形 / 圆角 / 椭圆的图片
- Android使用Xfermode图形渲染方法实现自定义圆形、圆角和椭圆ImageView
- 自定义ImageView实现圆角图片和圆形图片
- Android自定义ImageView实现图片缩放滑动,双击放大缩小,多点触控旋转,圆角矩形,圆形和仿刮刮卡效果
- 自定义圆角的ImageView 还可以实现图片的圆形、椭圆形展示。
- [AndroidUI]自定义view(四):实现圆形圆角图片
- Android 自定义View修炼-实现自定义圆形、圆角和椭圆ImageView(使用Xfermode图形渲染方法)
- 自定义View(1)--圆形图片、圆角图片的实现
- 自定义view实现图片圆形与圆角
- Android 自定义 RoundImageView 实现圆角矩形或圆形图片
- 通过自定义View实现圆形图片
- Android绘图机制(二)——自定义View绘制形, 圆形, 三角形, 扇形, 椭圆, 曲线,文字和图片的坐标讲解