use of android.graphics.PorterDuffXfermode in project AndroidUtilCode by Blankj.
the class ImageUtils method toRound.
/**
* 转为圆形图片
*
* @param src 源图片
* @param recycle 是否回收
* @return 圆形图片
*/
public static Bitmap toRound(Bitmap src, boolean recycle) {
if (isEmptyBitmap(src))
return null;
int width = src.getWidth();
int height = src.getHeight();
int radius = Math.min(width, height) >> 1;
Bitmap ret = Bitmap.createBitmap(width, height, src.getConfig());
Paint paint = new Paint();
Canvas canvas = new Canvas(ret);
Rect rect = new Rect(0, 0, width, height);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawCircle(width >> 1, height >> 1, radius, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(src, rect, rect, paint);
if (recycle && !src.isRecycled())
src.recycle();
return ret;
}
use of android.graphics.PorterDuffXfermode in project AndroidUtilCode by Blankj.
the class ImageUtils method addReflection.
/**
* 添加倒影
*
* @param src 源图片的
* @param reflectionHeight 倒影高度
* @param recycle 是否回收
* @return 带倒影图片
*/
public static Bitmap addReflection(Bitmap src, int reflectionHeight, boolean recycle) {
if (isEmptyBitmap(src))
return null;
// 原图与倒影之间的间距
final int REFLECTION_GAP = 0;
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionBitmap = Bitmap.createBitmap(src, 0, srcHeight - reflectionHeight, srcWidth, reflectionHeight, matrix, false);
Bitmap ret = Bitmap.createBitmap(srcWidth, srcHeight + reflectionHeight, src.getConfig());
Canvas canvas = new Canvas(ret);
canvas.drawBitmap(src, 0, 0, null);
canvas.drawBitmap(reflectionBitmap, 0, srcHeight + REFLECTION_GAP, null);
Paint paint = new Paint();
paint.setAntiAlias(true);
LinearGradient shader = new LinearGradient(0, srcHeight, 0, ret.getHeight() + REFLECTION_GAP, 0x70FFFFFF, 0x00FFFFFF, Shader.TileMode.MIRROR);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
canvas.drawRect(0, srcHeight + REFLECTION_GAP, srcWidth, ret.getHeight(), paint);
if (!reflectionBitmap.isRecycled())
reflectionBitmap.recycle();
if (recycle && !src.isRecycled())
src.recycle();
return ret;
}
use of android.graphics.PorterDuffXfermode in project AndroidUtilCode by Blankj.
the class ImageUtils method toRoundCorner.
/**
* 转为圆角图片
*
* @param src 源图片
* @param radius 圆角的度数
* @param recycle 是否回收
* @return 圆角图片
*/
public static Bitmap toRoundCorner(Bitmap src, float radius, boolean recycle) {
if (null == src)
return null;
int width = src.getWidth();
int height = src.getHeight();
Bitmap ret = Bitmap.createBitmap(width, height, src.getConfig());
Paint paint = new Paint();
Canvas canvas = new Canvas(ret);
Rect rect = new Rect(0, 0, width, height);
paint.setAntiAlias(true);
canvas.drawRoundRect(new RectF(rect), radius, radius, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(src, rect, rect, paint);
if (recycle && !src.isRecycled())
src.recycle();
return ret;
}
use of android.graphics.PorterDuffXfermode in project AndroidChromium by JackyAndroid.
the class AccountManagementFragment method makeRoundUserPicture.
/**
* Converts a square user picture to a round user picture.
* @param bitmap A bitmap to convert.
* @return A rounded picture bitmap.
*/
public static Bitmap makeRoundUserPicture(Bitmap bitmap) {
if (bitmap == null)
return null;
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
canvas.drawARGB(0, 0, 0, 0);
paint.setAntiAlias(true);
paint.setColor(0xFFFFFFFF);
canvas.drawCircle(bitmap.getWidth() * 0.5f, bitmap.getHeight() * 0.5f, bitmap.getWidth() * 0.5f, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
use of android.graphics.PorterDuffXfermode in project AndroidChromium by JackyAndroid.
the class AccountManagementFragment method overlayChildBadgeOnUserPicture.
/**
* Creates a new image with the picture overlaid by the badge.
* @param userPicture A bitmap to overlay on.
* @param badge A bitmap to overlay with.
* @return A bitmap with the badge overlaying the {@code userPicture}.
*/
private static Bitmap overlayChildBadgeOnUserPicture(Bitmap userPicture, Bitmap badge, Resources resources) {
assert userPicture.getWidth() == resources.getDimensionPixelSize(R.dimen.user_picture_size);
int borderSize = resources.getDimensionPixelOffset(R.dimen.badge_border_size);
int badgeRadius = resources.getDimensionPixelOffset(R.dimen.badge_radius);
// Create a larger image to accommodate the badge which spills the original picture.
int badgedPictureWidth = resources.getDimensionPixelOffset(R.dimen.badged_user_picture_width);
int badgedPictureHeight = resources.getDimensionPixelOffset(R.dimen.badged_user_picture_height);
Bitmap badgedPicture = Bitmap.createBitmap(badgedPictureWidth, badgedPictureHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(badgedPicture);
canvas.drawBitmap(userPicture, 0, 0, null);
// Cut a transparent hole through the background image.
// This will serve as a border to the badge being overlaid.
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
int badgeCenterX = badgedPictureWidth - badgeRadius;
int badgeCenterY = badgedPictureHeight - badgeRadius;
canvas.drawCircle(badgeCenterX, badgeCenterY, badgeRadius + borderSize, paint);
// Draw the badge
canvas.drawBitmap(badge, badgeCenterX - badgeRadius, badgeCenterY - badgeRadius, null);
return badgedPicture;
}
Aggregations