您的位置:首页 > 其它

图片圆形,矩形圆角

2016-06-29 10:59 211 查看
转载:http://blog.csdn.net/lmj623565791/article/details/40162163

public class CustomImageView   extends View {
private  int type;
private static final int TYPE_CIRCLE = 0;
private static  final int TYPE_ROUND = 1;

//图片
private Bitmap mSrc;
//圆角的大小
private int mRadius;
//控件的宽度,高度
private int mWidth,mHeight;
public CustomImageView(Context context) {
this(context,null,0);
}

public CustomImageView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}

public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomImageView, defStyleAttr, 0);
int indexCount = typedArray.getIndexCount();
for (int i = 0; i < indexCount; i++) {
int attr = typedArray.getIndex(i);
switch (attr){
case R.styleable.CustomImageView_src:
mSrc = BitmapFactory.decodeResource(getResources(), typedArray.getResourceId(attr, 0));
break;
case R.styleable.CustomImageView_type:
type = typedArray.getInt(attr, 0);
break;
case R.styleable.CustomImageView_borderRadius:
mRadius = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, getResources().getDisplayMetrics()));
break;
}
}
typedArray.recycle();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//设置宽度
int specMode = MeasureSpec.getMode(widthMeasureSpec);
int specSize = MeasureSpec.getSize(widthMeasureSpec);

if(specMode == MeasureSpec.EXACTLY){
mWidth = specSize;
}else {
//由图片决定的宽度
int desireByImg = getPaddingLeft() +getPaddingRight()+mSrc.getWidth();
if(specMode == MeasureSpec.AT_MOST){
mWidth = Math.min(desireByImg, specSize);
}else {
mWidth = desireByImg;
}
}
/***设置高度***/
specMode = MeasureSpec.getMode(heightMeasureSpec);
specSize = MeasureSpec.getSize(heightMeasureSpec);
if(specMode == MeasureSpec.EXACTLY){
mHeight = specSize;
}else {
int desire = getPaddingBottom()+getPaddingTop() + mSrc.getHeight();
if(specMode == MeasureSpec.AT_MOST){
mHeight = Math.min(desire,specSize);
}else{
mHeight = desire;
}
}
setMeasuredDimension(mWidth,mHeight);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
switch (type){
//如果type_cicle绘制圆形
case TYPE_CIRCLE:
int min = Math.min(mWidth, mHeight);
//长度不一致,按小的值进行压缩
mSrc = Bitmap.createScaledBitmap(mSrc, min, min, false);
canvas.drawBitmap(creatCircleImage(mSrc, min), 0, 0, null);
break;
case TYPE_ROUND:
canvas.drawBitmap(createRoundConerImage(mSrc),0,0,null);
break;
}
}

/**
* 根据图原长和边长绘图形图片
*/
private Bitmap creatCircleImage(Bitmap soure,int min){
final Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap target = Bitmap.createBitmap(min, min, Bitmap.Config.ARGB_8888);
//产生一个同样的画布
Canvas canvas = new Canvas(target);
//首先绘制圆形
canvas.drawCircle(min / 2, min / 2, min / 2, paint);
//使用SRC_IN,
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//绘制 图片
canvas.drawBitmap(soure,0,0,paint);
return target;
}
/**
* 根据图添加圆角
*
*/

private Bitmap createRoundConerImage(Bitmap soure){
final Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap target = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(target);
RectF rect = new RectF(0, 0, soure.getWidth(), soure.getHeight());
canvas.drawRoundRect(rect,mRadius,mRadius,paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(soure,0,0,paint);
return target;
}

}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:xxl="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.testapplication.Main2Activity"
>
<com.example.administrator.testapplication.CustomImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
xxl:borderRadius="5dp"
xxl:src="@mipmap/a"
xxl:type="round"
></com.example.administrator.testapplication.CustomImageView>
<com.example.administrator.testapplication.CustomImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
xxl:borderRadius="5dp"
xxl:src="@mipmap/a"
xxl:type="circle"
></com.example.administrator.testapplication.CustomImageView>
</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="borderRadius" format="dimension"/>
<attr name="type">
<enum name="circle" value="0"/>
<enum name="round" value="1"/>
</attr>
<attr name="src" format="reference"/>
<declare-styleable name="CustomImageView">
<attr name="borderRadius"/>
<attr name="type"/>
<attr name="src"/>
</declare-styleable>
</resources>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: