import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Shader;
albumImage.setImageBitmap(bitmap);
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
// final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 8;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
// paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
package com.pateo.as21.music.activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
/**
* @author :
* @email
* @date : 2012-11-8
* @desc :
*/
public class BitmapFillet {
public static final int ALL = 347120;
public static final int TOP = 547120;
public static final int LEFT = 647120;
public static final int RIGHT = 747120;
public static final int BOTTOM = 847120;
/**
*
* 指定图片的切边,对图片进行圆角处理
* @param type 具体参见:{@link BitmapFillet.ALL} , {@link BitmapFillet.TOP} ,
* {@link BitmapFillet.LEFT} , {@link BitmapFillet.RIGHT} , {@link BitmapFillet.BOTTOM}
* @param bitmap 需要被切圆角的图片
* @param roundPx 要切的像素大小
* @return
*
*/
public static Bitmap fillet(int type,Bitmap bitmap,int roundPx) {
try {
// 其原理就是:先建立一个与图片大小相同的透明的Bitmap画板
// 然后在画板上画出一个想要的形状的区域。
// 最后把源图片帖上。
final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
Bitmap paintingBoard = Bitmap.createBitmap(width,height, Config.ARGB_8888);
Canvas canvas = new Canvas(paintingBoard);
canvas.drawARGB(Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT);
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
if( TOP == type ){
clipTop(canvas,paint,roundPx,width,height);
}else if( LEFT == type ){
clipLeft(canvas,paint,roundPx,width,height);
}else if( RIGHT == type ){
clipRight(canvas,paint,roundPx,width,height);
}else if( BOTTOM == type ){
clipBottom(canvas,paint,roundPx,width,height);
}else{
clipAll(canvas,paint,roundPx,width,height);
}
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
//帖子图
final Rect src = new Rect(0, 0, width, height);
final Rect dst = src;
canvas.drawBitmap(bitmap, src, dst, paint);
return paintingBoard;
} catch (Exception exp) {
return bitmap;
}
}
private static void clipLeft(final Canvas canvas,final Paint paint,int offset,int width,int height){
final Rect block = new Rect(offset,0,width,height);
canvas.drawRect(block, paint);
final RectF rectF = new RectF(0, 0, offset * 2 , height);
canvas.drawRoundRect(rectF, offset, offset, paint);
}
private static void clipRight(final Canvas canvas,final Paint paint,int offset,int width,int height){
final Rect block = new Rect(0, 0, width-offset, height);
canvas.drawRect(block, paint);
final RectF rectF = new RectF(width - offset * 2, 0, width , height);
canvas.drawRoundRect(rectF, offset, offset, paint);
}
private static void clipTop(final Canvas canvas,final Paint paint,int offset,int width,int height){
final Rect block = new Rect(0, offset, width, height);
canvas.drawRect(block, paint);
final RectF rectF = new RectF(0, 0, width , offset * 2);
canvas.drawRoundRect(rectF, offset, offset, paint);
}
private static void clipBottom(final Canvas canvas,final Paint paint,int offset,int width,int height){
final Rect block = new Rect(0, 0, width, height - offset);
canvas.drawRect(block, paint);
final RectF rectF = new RectF(0, height - offset * 2 , width , height);
canvas.drawRoundRect(rectF, offset, offset, paint);
}
private static void clipAll(final Canvas canvas,final Paint paint,int offset,int width,int height){
final RectF rectF = new RectF(0, 0, width , height);
canvas.drawRoundRect(rectF, offset, offset, paint);
}
}
package com.pateo.as21.music.activity.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;
public class RoundAngleImageView extends ImageView {
private Paint paint;
private int roundWidth = 15;
private int roundHeight = 15;
private Paint paint2;
public RoundAngleImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
public RoundAngleImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public RoundAngleImageView(Context context) {
super(context);
init(context, null);
}
private void init(Context context, AttributeSet attrs) {
float density = context.getResources().getDisplayMetrics().density;
roundWidth = (int) (roundWidth * density);
roundHeight = (int) (roundHeight * density);
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
paint2 = new Paint();
paint2.setXfermode(null);
}
@Override
public void draw(Canvas canvas) {
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
Canvas canvas2 = new Canvas(bitmap);
super.draw(canvas2);
// drawLiftUp(canvas2);
drawRightUp(canvas2);
// drawLiftDown(canvas2);
// drawRightDown(canvas2);
canvas.drawBitmap(bitmap, 0, 0, paint2);
bitmap.recycle();
}
private void drawLiftUp(Canvas canvas) {
Path path = new Path();
path.moveTo(0, roundHeight);
path.lineTo(0, 0);
path.lineTo(roundWidth, 0);
path.arcTo(new RectF(
0,
0,
roundWidth*2,
roundHeight*2),
-90,
-90);
path.close();
canvas.drawPath(path, paint);
}
private void drawLiftDown(Canvas canvas) {
Path path = new Path();
path.moveTo(0, getHeight()-roundHeight);
path.lineTo(0, getHeight());
path.lineTo(roundWidth, getHeight());
path.arcTo(new RectF(
0,
getHeight()-roundHeight*2,
0+roundWidth*2,
getHeight()),
90,
90);
path.close();
canvas.drawPath(path, paint);
}
private void drawRightDown(Canvas canvas) {
Path path = new Path();
path.moveTo(getWidth()-roundWidth, getHeight());
path.lineTo(getWidth(), getHeight());
path.lineTo(getWidth(), getHeight()-roundHeight);
path.arcTo(new RectF(
getWidth()-roundWidth*2,
getHeight()-roundHeight*2,
getWidth(),
getHeight()), 0, 90);
path.close();
canvas.drawPath(path, paint);
}
private void drawRightUp(Canvas canvas) {
Path path = new Path();
path.moveTo(getWidth(), roundHeight);
path.lineTo(getWidth(), 0);
path.lineTo(getWidth()-roundWidth, 0);
path.arcTo(new RectF(
getWidth()-roundWidth*2,
0,
getWidth(),
0+roundHeight*2),
-90,
90);
path.close();
canvas.drawPath(path, paint);
}
}