您的位置:首页 > 移动开发 > Android开发

Android圆形图片和圆角图片的绘制

2017-02-21 18:06 211 查看
转载请标明出处:http://blog.csdn.NET/lmj623565791/article/details/24555655  
////////圆角图片绘制

FROM  GA_studio   http://blog.csdn.net/tianjian4592 
 //////////drawBitmap讲解

自定义ImageView

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.View.MeasureSpec;

import com.aynu.circleimageview.R;

/**
*
* @author zhy
*
*/
public class CustomImageView extends View
{

/**
* TYPE_CIRCLE / TYPE_ROUND
*/
private int type;
private static final int TYPE_CIRCLE = 0;
private static final int TYPE_ROUND = 1;

/**
*bitmap
*/
private Bitmap mSrc;

/**
*半径
*/
private int mRadius;

/**
* �ؼ�长度�
*/
private int mWidth;
/**
* �高度�
*/
private int mHeight;

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

public CustomImageView(Context context)
{
this(context, null);
}

/**
* �获取属性值��
*
* @param context
* @param attrs
* @param defStyle
*/
public <
4000
/span>CustomImageView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);

TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomImageView, defStyle, 0);
int n = a.getIndexCount();
for (int i = 0; i < n; i++)
{
int attr = a.getIndex(i);
switch (attr)
{
case R.styleable.CustomImageView_src://获取图片,自定义属性,设置图片并获取
mSrc = BitmapFactory.decodeResource(getResources(), a.getResourceId(attr, 0));
break;
case R.styleable.CustomImageView_type://获取图片要设置圆形还是圆角图片
type = a.getInt(attr, 0);
break;
case R.styleable.CustomImageView_borderRadius://半径值
mRadius = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f,
getResources().getDisplayMetrics()));
break;
}
}
a.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)// match_parent , accurate精确的
{
mWidth = specSize;
} else
{
// �
int desireByImg = getPaddingLeft() + getPaddingRight() + mSrc.getWidth();//图片的大小,和测量出来的大小比较
if (specMode == MeasureSpec.AT_MOST)// wrap_content
{
mWidth = Math.min(desireByImg, specSize);
}
}

/***
* ��获取高度的测量模式�
*/

specMode = MeasureSpec.getMode(heightMeasureSpec);
specSize = MeasureSpec.getSize(heightMeasureSpec);
if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate
{
mHeight = specSize;
} else
{
int desire = getPaddingTop() + getPaddingBottom() + mSrc.getHeight();
if (specMode == MeasureSpec.AT_MOST)// wrap_content
{
mHeight = Math.min(desire, specSize);
}
}
setMeasuredDimension(mWidth, mHeight);

}

/**
* ����
*/
@Override
protected void onDraw(Canvas canvas)
{

switch (type)
{
// ���圆形图片
case TYPE_CIRCLE:

int min = Math.min(mWidth, mHeight);
/**
* 使得当前的图片的宽和高一致Bitmap
*/
mSrc = Bitmap.createScaledBitmap(mSrc, min, min, false);
//绘制圆角图片
canvas.drawBitmap(createCircleImage(mSrc, min), 0, 0, null);
break;
case TYPE_ROUND:
//圆角图片
canvas.drawBitmap(createRoundConerImage(mSrc), 0, 0, null);
break;

}

}

/**
*
* @param source
* @param min
* @return
*/
private Bitmap createCircleImage(Bitmap source, int min)
{
//画笔
final Paint paint = new Paint();
paint.setAntiAlias(true);
//创建Bitmap对象
Bitmap target = Bitmap.createBitmap(min, min, Config.ARGB_8888);
/**
* 绘制�
*/
Canvas canvas = new Canvas(target);
/**
* 画圆
*/
canvas.drawCircle(min / 2, min / 2, min / 2, paint);
/**
* ʹ��设置mode���˵��
*/
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
/**
* ���画图像
*/
canvas.drawBitmap(source, 0, 0, paint);
return target;
}

/**
* ��
*
* @param source
* @return
*/
private Bitmap createRoundConerImage(Bitmap source)
{
final Paint paint = new Paint();
paint.setAntiAlias(true);
//创建bitmap
Bitmap target = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
//绘制图像
Canvas canvas = new Canvas(target);
RectF rect = new RectF(0, 0, source.getWidth(), source.getHeight());
canvas.drawRoundRect(rect, 50f, 50f, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//图片,大小,绘制的地方
canvas.drawBitmap(source, 0, 0, paint);
return target;
}
}

 

 

 

activity_main.xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:zhy="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:src="@mipmap/icon" />

<com.aynu.circleimageview.CustomImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
zhy:src="@mipmap/icon"
zhy:type="circle" />

<com.aynu.circleimageview.CustomImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
zhy:borderRadius="10dp"
zhy:src="@mipmap/icon"
zhy:type="round" />

<com.aynu.circleimageview.CustomImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
zhy:borderRadius="20dp"
zhy:src="@mipmap/icon"
zhy:type="round" />

</LinearLayout>

res/attrs文件

<?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"></attr>

<declare-styleable name="CustomImageView">
<attr name="borderRadius" />
<attr name="type" />
<attr name="src" />
</declare-styleable>

</resources>

代码下载地址:

http://download.csdn.net/detail/lmj623565791/7269005
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: