Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
Expand All @@ -34,6 +38,8 @@ public class ProgressImageView extends android.support.v7.widget.AppCompatImageV
private int mBorderWidth = 20;
private int mMaskColor = 0x66000000;

private Bitmap mBlurBitmap ;

public ProgressImageView(Context context) {

this(context, null);
Expand Down Expand Up @@ -68,8 +74,6 @@ private void init(AttributeSet attrs) {
}
ta.recycle();

mCornerRadius = 30;
setWillNotDraw(false);
mPaint = new Paint();
mPaint.setColor(mBorderColor);
mPaint.setStyle(Paint.Style.STROKE);
Expand Down Expand Up @@ -167,6 +171,11 @@ protected void onDraw(Canvas canvas) {
//2.画imageView 使用相同的Scale和Path 那么边框的内边界会被遮挡 原因是如果按照实际比例去缩放会导致图片圆角和边框圆角之间有空白
Rect rect = (new Rect(0, 0, mWidth, mHeight));
canvas.drawBitmap(mFinalBitmap, rect, rect, mPaint);
if (mNeedMask) {
mPaint.setAlpha((int) ((1 - mCurrentProgress / 100F) * 255));
canvas.drawBitmap(mBlurBitmap, rect, rect, mPaint);
mPaint.setAlpha(255);
}
canvas.restore();
}

Expand All @@ -177,18 +186,20 @@ public Bitmap createFinalBitmap(int width, int height) {
int layerID = canvas.saveLayer(0, 0, width, height, mPaint, Canvas.ALL_SAVE_FLAG);
//1.画ImageView
super.onDraw(canvas);
//2.画遮罩
if (mNeedMask) {
Paint paint = new Paint();
paint.setColor(mMaskColor);
// paint.setAlpha(100);
canvas.drawRect(new Rect(0, 0, width, height), paint);
}
// //2.画遮罩
// if (mNeedMask) {
// Paint paint = new Paint();
// paint.setColor(mMaskColor);
//// paint.setAlpha(100);
// canvas.drawRect(new Rect(0, 0, width, height), paint);
// }
//3.设置混合模式为IN 并且画边框
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawBitmap(createSrcBitmap(width, height), 0, 0, mPaint);
mPaint.setXfermode(null);
canvas.restoreToCount(layerID);

mBlurBitmap = blur(getContext(), bitmap);
return bitmap;
}

Expand All @@ -202,4 +213,21 @@ public Bitmap createSrcBitmap(int width, int height) {
return bitmap;
}


public static Bitmap blur(Context context, Bitmap image) {
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, image.getWidth(), image.getHeight(), false);
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);

blurScript.setRadius(25);
blurScript.setInput(tmpIn);
blurScript.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);

return outputBitmap;
}
}