package com.view;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
public class Util {
private static final int STROKE_WIDTH = 4;
public static Bitmap toRoundBitmap(Context context,Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;
if (width <= height) {
roundPx = width / 2;
top = 0;
left = 0;
bottom = width;
right = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap.createBitmap(width,
height, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int)left, (int)top, (int)right, (int)bottom);
final Rect dst = new Rect((int)dst_left, (int)dst_top, (int)dst_right, (int)dst_bottom);
final RectF rectF = new RectF(dst);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(4);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, src, dst, paint);
//画白色圆圿
paint.reset();
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(STROKE_WIDTH);
paint.setAntiAlias(true);
canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
canvas.drawCircle(width / 2, width / 2, width / 2 - STROKE_WIDTH / 2, paint);
return output ;
}
/*public static void drawImage(Canvas canvas, Bitmap blt, int x, int y, int w, int h, int bx, int by)
{ //x,y表示绘画的起点,
Rect src = new Rect();// 图片
Rect dst = new Rect();// 屏幕位置及尺寿
//src 这个是表示绘画图片的大小
src.left = bx; //0,0
src.top = by;
src.right = bx + w;// mBitDestTop.getWidth();,这个是桌面图的宽度,
src.bottom = by + h;//mBitDestTop.getHeight()/2;// 这个是桌面图的高度的丿卿
// 下面皿 dst 是表礿 绘画这个图片的位罿
dst.left = x; //miDTX,//这个是可以改变的,也就是绘图的起点X位置
dst.top = y; //mBitQQ.getHeight();//这个是QQ图片的高度㿿 也就相当亿 桌面图片绘画起点的Y坐标
dst.right = x + w; //miDTX + mBitDestTop.getWidth();// 表示霿绘画的图片的右上觿
dst.bottom = y + h; // mBitQQ.getHeight() + mBitDestTop.getHeight();//表示霿绘画的图片的右下觿
canvas.drawBitmap(blt, src, dst, null);//这个方法 第一个参数是图片原来的大小,第二个参数是 绘画该图片需显示多少。也就是说你想绘画该图片的某丿些地方,而不是全部图片,第三个参数表示该图片绘画的位罿
src = null;
dst = null;
}*/
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
public class Util {
private static final int STROKE_WIDTH = 4;
public static Bitmap toRoundBitmap(Context context,Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;
if (width <= height) {
roundPx = width / 2;
top = 0;
left = 0;
bottom = width;
right = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap.createBitmap(width,
height, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int)left, (int)top, (int)right, (int)bottom);
final Rect dst = new Rect((int)dst_left, (int)dst_top, (int)dst_right, (int)dst_bottom);
final RectF rectF = new RectF(dst);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(4);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, src, dst, paint);
//画白色圆圿
paint.reset();
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(STROKE_WIDTH);
paint.setAntiAlias(true);
canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
canvas.drawCircle(width / 2, width / 2, width / 2 - STROKE_WIDTH / 2, paint);
return output ;
}
/*public static void drawImage(Canvas canvas, Bitmap blt, int x, int y, int w, int h, int bx, int by)
{ //x,y表示绘画的起点,
Rect src = new Rect();// 图片
Rect dst = new Rect();// 屏幕位置及尺寿
//src 这个是表示绘画图片的大小
src.left = bx; //0,0
src.top = by;
src.right = bx + w;// mBitDestTop.getWidth();,这个是桌面图的宽度,
src.bottom = by + h;//mBitDestTop.getHeight()/2;// 这个是桌面图的高度的丿卿
// 下面皿 dst 是表礿 绘画这个图片的位罿
dst.left = x; //miDTX,//这个是可以改变的,也就是绘图的起点X位置
dst.top = y; //mBitQQ.getHeight();//这个是QQ图片的高度㿿 也就相当亿 桌面图片绘画起点的Y坐标
dst.right = x + w; //miDTX + mBitDestTop.getWidth();// 表示霿绘画的图片的右上觿
dst.bottom = y + h; // mBitQQ.getHeight() + mBitDestTop.getHeight();//表示霿绘画的图片的右下觿
canvas.drawBitmap(blt, src, dst, null);//这个方法 第一个参数是图片原来的大小,第二个参数是 绘画该图片需显示多少。也就是说你想绘画该图片的某丿些地方,而不是全部图片,第三个参数表示该图片绘画的位罿
src = null;
dst = null;
}*/
}
Bitmap roundBitmap = Util.toRoundBitmap(RegisterActivity.this,
bitmap);