下面列出了android.graphics.PaintFlagsDrawFilter#android.graphics.PorterDuffXfermode 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@SuppressLint({"WrongConstant"})
public void onDraw(Canvas canvas) {
float src;
super.onDraw(canvas);
if (VERSION.SDK_INT <= 19) {
this.mPaint.setColor(Color.argb(1, 0, 0, 0));
}
canvas.saveLayer(0.0f, 0.0f, (float) getWidth(), (float) getHeight(), null, 31);
Rect dst = new Rect();
dst.left = 0;
dst.top = 0;
dst.right = getWidth();
dst.bottom = getHeight();
canvas.drawBitmap(this.mBitmap, null, dst, null);
this.mPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
if (this.percent == 0) {
src = 1.0f;
} else {
src = (100.0f - (((((float) this.percent) / 100.0f) * 85.0f) + 15.0f)) / 100.0f;
}
canvas.drawRect(new Rect((int) (((float) getWidth()) - (((float) getWidth()) * src)), 0, getWidth(), getHeight()), this.mPaint);
this.mPaint.setXfermode(null);
}
public void drawRectangleOrCircle(Canvas canvas) {
Bitmap bitmap =
Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
Canvas temp = new Canvas(bitmap);
Paint transparentPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
PorterDuffXfermode porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);
transparentPaint.setColor(Color.TRANSPARENT);
temp.drawRect(0, 0, temp.getWidth(), temp.getHeight(), mPaint);
transparentPaint.setXfermode(porterDuffXfermode);
if (mDrawCircleFlag) {
float cx = mClipBorder.left + mClipBorder.width() / 2f;
float cy = mClipBorder.top + mClipBorder.height() / 2f;
float radius = mClipBorder.height() / 2f;
temp.drawCircle(cx, cy, radius, transparentPaint);
} else {
RectF rectF =
new RectF(mClipBorder.left, mClipBorder.top, mClipBorder.right, mClipBorder.bottom);
temp.drawRoundRect(rectF, mRoundCorner, mRoundCorner, transparentPaint);
}
canvas.drawBitmap(bitmap, 0, 0, null);
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
if (requiersShapeUpdate) {
calculateLayout(canvas.getWidth(), canvas.getHeight());
requiersShapeUpdate = false;
}
if (requiresBitmap()) {
clipPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawBitmap(clipBitmap, 0, 0, clipPaint);
} else {
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1){
canvas.drawPath(clipPath, clipPaint);
} else {
canvas.drawPath(rectView, clipPaint);
}
}
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) {
setLayerType(LAYER_TYPE_HARDWARE, null);
}
}
/**
* Construct an instance for the given subtitle and ancillary page ids.
*
* @param subtitlePageId The id of the subtitle page carrying the subtitle to be parsed.
* @param ancillaryPageId The id of the ancillary page containing additional data.
*/
public DvbParser(int subtitlePageId, int ancillaryPageId) {
defaultPaint = new Paint();
defaultPaint.setStyle(Paint.Style.FILL_AND_STROKE);
defaultPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
defaultPaint.setPathEffect(null);
fillRegionPaint = new Paint();
fillRegionPaint.setStyle(Paint.Style.FILL);
fillRegionPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));
fillRegionPaint.setPathEffect(null);
canvas = new Canvas();
defaultDisplayDefinition = new DisplayDefinition(719, 575, 0, 719, 0, 575);
defaultClutDefinition = new ClutDefinition(0, generateDefault2BitClutEntries(),
generateDefault4BitClutEntries(), generateDefault8BitClutEntries());
subtitleService = new SubtitleService(subtitlePageId, ancillaryPageId);
}
private Bitmap drawBevel(@NonNull Bitmap bitmap){
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int radius = Math.min(h / 2 + (bevelStrokeWidth / 2), w / 2 + (bevelStrokeWidth / 2));
Bitmap output = Bitmap.createBitmap(w + (bevelStrokeWidth * 2), h + (bevelStrokeWidth * 2), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(output);
// Draw the source image (i.e., picture inside the circle)
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
c.drawARGB(0, 0, 0, 0);
c.drawBitmap(bitmap, bevelStrokeWidth, bevelStrokeWidth, paint);
// Draw the bevel around it
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
paint.setStyle(Paint.Style.STROKE);
paint.setColor(getResources().getColor(R.color.overlay_white_with_40));
paint.setStrokeWidth(bevelStrokeWidth);
c.drawCircle((w / 2) + bevelStrokeWidth, (h / 2) + bevelStrokeWidth, radius, paint);
return output;
}
@Override
public Bitmap transform(@NonNull Bitmap source) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(preset.mode));
paint.setColor(preset.color);
paint.setAlpha(preset.alpha);
Bitmap outBitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outBitmap);
canvas.drawBitmap(source, 0, 0, null);
canvas.drawRect(0, 0, outBitmap.getWidth(), outBitmap.getHeight(), paint);
if (outBitmap != source && !source.isRecycled()) {
source.recycle();
}
return outBitmap;
}
public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.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 = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
private void initSource(int width, int height){
//获取半径 * 曲率,曲率越大,半径越大
float mRadius = getRadius(width, mDistanceTop) * mCurvature;
//计算圆点
float mStartX = (float) (width / 2);
float mStartY = mDistanceTop - mRadius;
Paint mPaint = new Paint();
//遮罩圆形区域内颜色
mPaint.setColor(maskInColor);
mPaint.setAntiAlias(true);
//显示相交部分
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
//遮罩圆形区域外部
mForeground = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
//遮罩圆形区域外部颜色
mForeground.eraseColor(maskOutColor);
Canvas mForeCanvas = new Canvas(mForeground);
mForeCanvas.drawCircle(mStartX, mStartY, mRadius, mPaint);
}
private static Bitmap getCroppedBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.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());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
public void draw(Canvas canvas) {
super.draw(canvas);
this.paint.setStyle(Style.STROKE);
this.paint.setAntiAlias(true);
float padding = AbViewUtil.dip2px(getContext(), 0.5f);
this.rect.set(padding, padding, ((float) getWidth()) - padding, ((float) getHeight()) - padding);
this.paint.setColor(603979775);
this.paint.setStrokeWidth(AbViewUtil.dip2px(getContext(), 0.7f));
canvas.drawRoundRect(this.rect, this.radius, this.radius, this.paint);
this.paint.setStyle(Style.FILL);
this.paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER));
this.rect.set((this.spotX - 1.0f) - this.radius, this.centerY - this.radius, (this.spotX + 1.1f) + this.radius, this.centerY + this.radius);
this.paint.setColor(0);
canvas.drawRoundRect(this.rect, this.radius, this.radius, this.paint);
float spotR = ((float) this.spotSize) * 0.45f;
this.rect.set(this.spotX - spotR, this.centerY - spotR, this.spotX + spotR, this.centerY + spotR);
this.paint.setColor(this.spotColor);
canvas.drawRoundRect(this.rect, spotR, spotR, this.paint);
}
public static Bitmap getCircularBitmap(Bitmap square) {
if (square == null) return null;
Bitmap output = Bitmap.createBitmap(square.getWidth(), square.getHeight(), Bitmap.Config.ARGB_8888);
final Rect rect = new Rect(0, 0, square.getWidth(), square.getHeight());
Canvas canvas = new Canvas(output);
int halfWidth = square.getWidth() / 2;
int halfHeight = square.getHeight() / 2;
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
canvas.drawCircle(halfWidth, halfHeight, Math.min(halfWidth, halfHeight), paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(square, rect, rect, paint);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
output.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
return output;
}
/**
* Create a new bitmap that has all pixels beyond the oval shape transparent. Old bitmap is
* recycled.
*/
public static Bitmap toOvalBitmap(@NonNull Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
int color = 0xff424242;
Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
RectF rect = new RectF(0, 0, width, height);
canvas.drawOval(rect, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, 0, 0, paint);
bitmap.recycle();
return output;
}
/**
* Create a new bitmap that has all pixels beyond the oval shape transparent. Old bitmap is
* recycled.
*/
public static Bitmap toOvalBitmap(@NonNull Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
int color = 0xff424242;
Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
RectF rect = new RectF(0, 0, width, height);
canvas.drawOval(rect, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, 0, 0, paint);
bitmap.recycle();
return output;
}
private ShadowDrawable(int shape, int[] bgColor, int shapeRadius, int shadowColor, int shadowRadius, int offsetX, int offsetY) {
this.mShape = shape;
this.mBgColor = bgColor;
this.mShapeRadius = shapeRadius;
this.mShadowRadius = shadowRadius;
this.mOffsetX = offsetX;
this.mOffsetY = offsetY;
mShadowPaint = new Paint();
mShadowPaint.setColor(Color.TRANSPARENT);
mShadowPaint.setAntiAlias(true);
mShadowPaint.setShadowLayer(shadowRadius, offsetX, offsetY, shadowColor);
mShadowPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP));
mBgPaint = new Paint();
mBgPaint.setAntiAlias(true);
}
private Bitmap createCircleBitmap(Bitmap resource, int backgroundColor) {
int width = resource.getWidth();
Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap circleBitmap = Bitmap.createBitmap(resource.getWidth(), resource.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(circleBitmap);
if (backgroundColor != Color.TRANSPARENT) {
paint.setColor(backgroundColor);
}
canvas.drawCircle(width / 2, width / 2, width / 2, paint);
//设置画笔为取交集模式
if (backgroundColor == Color.TRANSPARENT) {
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
} else {
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
}
//裁剪图片
canvas.drawBitmap(resource, 0, 0, paint);
return circleBitmap;
}
/**
* Creates a bitmap with rounded corners.
*
* @param bitmap The bitmap
* @param i the corner radius in pixels
* @return The bitmap with rounded corners
*/
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int i) {
if (bitmap == null) {
return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
}
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.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);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, (float) i, (float) i, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
BarcodeGraphicBase(GraphicOverlay overlay) {
super(overlay);
boxPaint = new Paint();
boxPaint.setColor(ContextCompat.getColor(context, R.color.barcode_reticle_stroke));
boxPaint.setStyle(Paint.Style.STROKE);
boxPaint.setStrokeWidth(
context.getResources().getDimensionPixelOffset(R.dimen.barcode_reticle_stroke_width));
scrimPaint = new Paint();
scrimPaint.setColor(ContextCompat.getColor(context, R.color.barcode_reticle_background));
eraserPaint = new Paint();
eraserPaint.setStrokeWidth(boxPaint.getStrokeWidth());
eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
boxCornerRadius =
context.getResources().getDimensionPixelOffset(R.dimen.barcode_reticle_corner_radius);
pathPaint = new Paint();
pathPaint.setColor(Color.WHITE);
pathPaint.setStyle(Paint.Style.STROKE);
pathPaint.setStrokeWidth(boxPaint.getStrokeWidth());
pathPaint.setPathEffect(new CornerPathEffect(boxCornerRadius));
boxRect = PreferenceUtils.getBarcodeReticleBox(overlay);
}
@Test
public void testDefaultPaintAttributes() {
BrushDrawingView brushDrawingView = new BrushDrawingView(mContext);
Paint drawingPaint = brushDrawingView.getDrawingPaint();
assertEquals(drawingPaint.getColor(), Color.BLACK);
assertEquals(drawingPaint.getStyle(), Paint.Style.STROKE);
assertEquals(drawingPaint.getStrokeJoin(), Paint.Join.ROUND);
assertEquals(drawingPaint.getStrokeCap(), Paint.Cap.ROUND);
assertEquals(drawingPaint.getStrokeWidth(), BrushDrawingView.DEFAULT_BRUSH_SIZE);
assertEquals(drawingPaint.getAlpha(), BrushDrawingView.DEFAULT_OPACITY);
assertTrue(drawingPaint.getXfermode() instanceof PorterDuffXfermode);
// Spy is not working properly
/*Paint spyPaint = Mockito.spy(drawingPaint);
verify(spyPaint, times(1)).setColor(Color.BLACK);*/
}
/**
* 根据原图和变长绘制圆形图片
*
* @param source
* @param min
*
* @return
*/
public static Bitmap createCircleImage(Bitmap source, int min) {
final Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap target = Bitmap.createBitmap(min, min, Bitmap.Config.ARGB_8888);
/**
* 产生一个同样大小的画布
*/
Canvas canvas = new Canvas(target);
/**
* 首先绘制圆形
*/
canvas.drawCircle(min / 2, min / 2, min / 2, paint);
/**
* 使用SRC_IN
*/
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
/**
* 绘制图片
*/
canvas.drawBitmap(source, 0, 0, paint);
return target;
}
/**
* 将图片剪裁为圆形
*/
static Bitmap createCircleImage(Bitmap source) {
if (source == null) {
return null;
}
int length = Math.min(source.getWidth(), source.getHeight());
Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap target = Bitmap.createBitmap(length, length, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(target);
canvas.drawCircle(source.getWidth() / 2, source.getHeight() / 2, length / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(source, 0, 0, paint);
return target;
}
/**
* Construct an instance for the given subtitle and ancillary page ids.
*
* @param subtitlePageId The id of the subtitle page carrying the subtitle to be parsed.
* @param ancillaryPageId The id of the ancillary page containing additional data.
*/
public DvbParser(int subtitlePageId, int ancillaryPageId) {
defaultPaint = new Paint();
defaultPaint.setStyle(Paint.Style.FILL_AND_STROKE);
defaultPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
defaultPaint.setPathEffect(null);
fillRegionPaint = new Paint();
fillRegionPaint.setStyle(Paint.Style.FILL);
fillRegionPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));
fillRegionPaint.setPathEffect(null);
canvas = new Canvas();
defaultDisplayDefinition = new DisplayDefinition(719, 575, 0, 719, 0, 575);
defaultClutDefinition = new ClutDefinition(0, generateDefault2BitClutEntries(),
generateDefault4BitClutEntries(), generateDefault8BitClutEntries());
subtitleService = new SubtitleService(subtitlePageId, ancillaryPageId);
}
/**
* Create a new bitmap that has all pixels beyond the oval shape transparent.
* Old bitmap is recycled.
*/
public static Bitmap toOvalBitmap(@NonNull Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
int color = 0xff424242;
Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
RectF rect = new RectF(0, 0, width, height);
canvas.drawOval(rect, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, 0, 0, paint);
bitmap.recycle();
return output;
}
public Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Bitmap.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);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, (float) pixels, (float) pixels, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
private void init(Context context, AttributeSet attrs) {
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundAngleImageView);
roundWidth = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth);
roundHeight = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundHeight, roundHeight);
} else {
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);
}
private void init(Context context, AttributeSet attrs) {
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundAngleImageView);
roundWidth = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth);
roundHeight = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundHeight, roundHeight);
} else {
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);
}
/**
* 绘制温度计水银高度
* 注:须与{@link #drawShape(Paint, Canvas)}方法结合
*
* @param shapePaint Paint
* @param canvas Canvas
*/
private void drawWaveShape(Paint shapePaint, Canvas canvas) {
float waveTop = mPaddingTop + titleHeight + minThermometerRadius
+ (maxScaleValue - curScaleValue) * 10 * scaleSpaceHeight;
shapePaint.setColor(leftMercuryColor);
shapePaint.clearShadowLayer();
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
canvas.drawRect(leftWaveLeft, waveTop, leftWaveRight, waveBottom, shapePaint);
shapePaint.setColor(rightMercuryColor);
shapePaint.clearShadowLayer();
canvas.drawRect(rightWaveLeft, waveTop, rightWaveRight, waveBottom, shapePaint);
}
Bitmap getBitmapWithId(String id) {
if (userGravatar.containsKey(id) && userGravatar.get(id) != null){
return userGravatar.get(id);
}
UserVCard vCard = ProfileUtils.getLocalVCard(id);
if (!TextUtils.isEmpty(vCard.gravantarUrl)) {
String filePath = MyDiskCache.getSmallFile(QtalkStringUtils.getGravatar(vCard.gravantarUrl,
true)).getAbsolutePath();
Bitmap bmp = BitmapFactory.decodeFile(filePath);
if(bmp!=null) {
int width = bmp.getWidth();
Bitmap resultBmp = Bitmap.createBitmap(width, width,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(resultBmp);
Paint paint = new Paint();
canvas.drawCircle(width / 2.0f, width / 2.0f, width / 2.0f, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bmp, 0, 0, paint);
bmp.recycle();
userGravatar.put(id, resultBmp);
return resultBmp;
}
}
return null;
}
public DonutProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setLayerType(LAYER_TYPE_SOFTWARE, null);
arcPaint = new Paint();
arcPaint.setColor(Color.WHITE);
arcPaint.setAntiAlias(true);
textPaint = new Paint();
textPaint.setColor(Color.WHITE);
textPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, getResources().getDisplayMetrics()));
FontsManager.set(getContext(), textPaint, R.font.roboto_light);
textPaint.setAntiAlias(true);
transparentPaint = new Paint();
transparentPaint.setColor(Color.TRANSPARENT);
transparentPaint.setAlpha(0xFF);
transparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
if (isInEditMode())
setProgress(new Random().nextInt(100));
else
setProgress(0);
}
/**
* 合并图片
* @param bgd 后景 Bitmap
* @param fg 前景 Bitmap
* @param mode 合并模式 {@link PorterDuff.Mode}
* @param bgdPoint 后景绘制 left、top 坐标
* @param fgPoint 前景绘制 left、top 坐标
* @return 合并后的图片
*/
public static Bitmap combine(final Bitmap bgd, final Bitmap fg, final PorterDuff.Mode mode, final Point bgdPoint, final Point fgPoint) {
if (isEmpty(bgd) || isEmpty(fg)) return null;
int width = bgd.getWidth() > fg.getWidth() ? bgd.getWidth() : fg.getWidth();
int height = bgd.getHeight() > fg.getHeight() ? bgd.getHeight() : fg.getHeight();
Paint paint = new Paint();
if (mode != null) {
paint.setXfermode(new PorterDuffXfermode(mode));
}
Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawBitmap(bgd, (bgdPoint != null) ? bgdPoint.x : 0, (bgdPoint != null) ? bgdPoint.y : 0, null);
canvas.drawBitmap(fg, (fgPoint != null) ? fgPoint.x : 0, (fgPoint != null) ? fgPoint.y : 0, paint);
return newBitmap;
}
/**
* 图片圆角处理 ( 非圆形 )
* @param bitmap 待操作源图片
* @param pixels 圆角大小
* @return 圆角处理后的图片
*/
public static Bitmap roundCorner(final Bitmap bitmap, final float pixels) {
if (isEmpty(bitmap)) return null;
// 创建一个同源图片一样大小的矩形, 用于把源图片绘制到这个矩形上
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
RectF rectF = new RectF(rect); // 创建一个精度更高的矩形, 用于画出圆角效果
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(0xff424242); // 设置画笔的颜色为不透明的灰色
Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawRoundRect(rectF, pixels, pixels, paint);
// 绘制底圆后, 进行合并 ( 交集处理 )
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return newBitmap;
}