自定义类是为了 实现圆形、圆角,椭圆等自定义图片View,在xml中使用的方法,可以参照下面的代码
xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.myimage.AImageViewRoundOval
android:id="@+id/circle"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/monkey1">
</com.example.myimage.AImageViewRoundOval>
<com.example.myimage.AImageViewRoundOval
android:id="@+id/roundRect"
android:layout_width="286dp"
android:layout_height="190dp"
android:layout_marginTop="10dp"
android:src="@mipmap/bg2">
</com.example.myimage.AImageViewRoundOval>
<com.example.myimage.AImageViewRoundOval
android:id="@+id/oval"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:src="@mipmap/pic7">
</com.example.myimage.AImageViewRoundOval>
</LinearLayout>
Main.java (需要在manifest.java中声明)
package com.example.myimage
import android.app.Activity;
import android.os.Bundle;
/****
* 自定义ImageView实现圆形图片,圆角矩形图片 椭圆图片
*/
public class Main extends Activity {
private AImageViewRoundOval iv_circle;//圆形图片
private AImageViewRoundOval iv_roundRect;//圆角矩形图片
private AImageViewRoundOval iv_oval;//椭圆图片
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
initViews();
}
/**
* 初始化Views
*/
private void initViews(){
iv_circle =(AImageViewRoundOval)findViewById(R.id.cicle);
iv_roundRect =(AImageViewRoundOval)findViewById(R.id.roundRect);
iv_oval =(AImageViewRoundOval)findViewById(R.id.oval);
iv_roundRect.setType(AImageViewRoundOval.TYPE_ROUND);
iv_roundRect.setRoundRadius(6);//矩形凹行大小
iv_oval.setType(AImageViewRoundOval.TYPE_OVAL);
iv_oval.setRoundRadius(45);//圆角大小
}
}
已经测试好的类:AImageViewRoundOval.java——自定义类(无需在manifest.java中声明)
如何调用参照上面Main.java中的代码
package com.example.myimage;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
/**
* 实现圆形、圆角,椭圆等自定义图片View。
*/
public class AImageViewRoundOval extends android.support.v7.widget.AppCompatImageView {
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 AImageViewRoundOval(Context context) {
this(context, null);
// TODOAuto-generated constructor stub
}
public AImageViewRoundOval(Context context, AttributeSet attrs) {
this(context,attrs, 0);
// TODOAuto-generated constructor stub
}
public AImageViewRoundOval(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) {
// TODOAuto-generated method stub
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) {
// TODOAuto-generated method stub
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);
return 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();
}
}
}
最后附上网络上找的备用类:ImageServer.java——自定义类(无需在manifest.java中声明)
作用和上面的AImageViewRoundOval.java一样,只是我并没有实验,因为上面的类已经达到我的要求了。
package com.example.myimage
import android.content.Context;
import android.graphics.*;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
import java.lang.ref.WeakReference;
/**
* 使用Xfermode渲染方案实现圆角矩形、椭圆ImageView
*/
public class ImageServer extends ImageView {
//数据定义
private Paint mPaint;
private Xfermode mXfermode = new PorterDuffXfermode(Mode.DST_IN);
private Bitmap mMaskBitmap; //用来做
private int mRoundBorderRadius;//圆角大小
private int mType;//类型:圆形、圆角或椭圆
private WeakReference<Bitmap> mBufferBitmap;//使用缓存技术,避免每次都执行onDraw
public static final int TYPE_CIRCLE = 1;//圆形
public static final int TYPE_ROUND = 2;//圆角矩形
public static final int TYPE_OVAL = 3;//椭圆形
private static final int DEFAULT_ROUND_BORDER_RADIUS = 10;//默认圆角大小
//构造方法
public ImageServer(Context context){
this(context, null);
}
public ImageServer(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
mPaint = new Paint();
mPaint.setAntiAlias(true);//设置消除锯齿
mType = TYPE_ROUND;
mRoundBorderRadius = DEFAULT_ROUND_BORDER_RADIUS;
}
/**
* 测量view的大小
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//如果类型是圆形,则强制设置view的宽高一致,取宽高的较小值
if(mType == TYPE_CIRCLE){
int width = Math.min(getMeasuredWidth(),getMeasuredHeight());
setMeasuredDimension(width, width);
}
}
/**
* 绘制view的内容
*/
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
//从缓存中取出bitmap
Bitmap bmp = (mBufferBitmap == null ? null:mBufferBitmap.get());
if(bmp == null || bmp.isRecycled()){
//如果没有缓存存在的情况
//获取drawable
Drawable drawable = getDrawable();
//获取drawable的宽高
int dwidth = drawable.getIntrinsicWidth();
int dheight = drawable.getIntrinsicHeight();
Log.v("czm","dwidth="+dwidth+",width="+getWidth());
if(null != drawable){
bmp = Bitmap.createBitmap(getWidth(), getHeight(),
Config.ARGB_8888);
float scale = 1.0f;
//创建画布
Canvas drawCanvas = new Canvas(bmp);
//按照bitmap的宽高,以及view的宽高,计算缩放比例;因为设置的src宽高
//比例可能和imageview的宽高比例不同,这里我们不希望图片失真;
if(mType == TYPE_CIRCLE)
{//如果是圆形
scale = getWidth() * 1.0F / Math.min(dwidth, dheight);
}else if (mType == TYPE_ROUND || mType == TYPE_OVAL)
{//如果是圆角矩形或椭圆
// 如果图片的宽或者高与view的宽高不匹配,计算出需要缩放的比例;
//缩放后的图片的宽高,一定要大于我们view的宽高;所以我们这里取大值;
scale = Math.max(getWidth() * 1.0f / dwidth, getHeight()
* 1.0f / dheight);
}
Log.v("czm","scale="+scale);
//根据缩放比例,设置bounds,即相当于做缩放图片
drawable.setBounds(0, 0, (int)(scale * dwidth), (int)(scale * dheight));
drawable.draw(drawCanvas);
//获取bitmap,即圆形、圆角或椭圆的bitmap
if(mMaskBitmap == null || mMaskBitmap.isRecycled()){
mMaskBitmap = getDrawBitmap();
}
//为paint设置Xfermode 渲染模式
mPaint.reset();
mPaint.setFilterBitmap(false);
mPaint.setXfermode(mXfermode);
//绘制不同形状
drawCanvas.drawBitmap(mMaskBitmap, 0, 0,mPaint);
mPaint.setXfermode(null);
//将准备好的bitmap绘制出来
canvas.drawBitmap(bmp, 0, 0, null);
//bitmap缓存起来,避免每次调用onDraw,分配内存
mBufferBitmap = new WeakReference<Bitmap>(bmp);
}
}else{
//如果缓存还存在的情况
mPaint.setXfermode(null);
canvas.drawBitmap(bmp, 0.0f, 0.0f, mPaint);
return;
}
}
/**
* 绘制不同的图形Bitmap
*/
private Bitmap getDrawBitmap(){
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLACK);
if(mType == TYPE_CIRCLE)
{//绘制圆形
canvas.drawCircle(getWidth() / 2, getWidth() / 2, getWidth() / 2,
paint);
}else if(mType == TYPE_ROUND)
{//绘制圆角矩形
canvas.drawRoundRect(new RectF(0, 0, getWidth(), getHeight()),
mRoundBorderRadius, mRoundBorderRadius, paint);
}else if(mType == TYPE_OVAL ){
//绘制椭圆
canvas.drawOval(new RectF(0, 0, getWidth(), getHeight()), mPaint);
}
return bitmap;
}
/**
* 因为使用了缓存技术,所以需要在invalidate中做些回收释放资源的处理
*/
@Override
public void invalidate() {
// TODO Auto-generated method stub
mBufferBitmap = null;
if(mMaskBitmap != null){
mMaskBitmap.recycle();
mMaskBitmap = null;
}
super.invalidate();
}
public int getRoundBorderRadius() {
return mRoundBorderRadius;
}
/**
* 设置圆角大小
* @parammRoundRadius
*/
public void setRoundBorderRadius(int mRoundBorderRadius) {
if(this.mRoundBorderRadius != mRoundBorderRadius){
this.mRoundBorderRadius = mRoundBorderRadius;
invalidate();
}
}
public int getType() {
return this.mType;
}
/**
* 设置图片类型:圆形、圆角矩形、椭圆形
* @param mType
*/
public void setType(int mType) {
if(this.mType != mType){
this.mType = mType;
invalidate();
}
}
}
下一篇文章将介绍一下如何点击按钮上传图片,并且可以实现裁剪的功能、然后是上传图片到数据库。