-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
LibEasyGlide/src/main/java/me/bzcoder/easyglide/transformation/BorderTransformation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package me.bzcoder.easyglide.transformation; | ||
|
||
import android.content.res.Resources; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapShader; | ||
import android.graphics.Canvas; | ||
import android.graphics.ColorMatrix; | ||
import android.graphics.ColorMatrixColorFilter; | ||
import android.graphics.Paint; | ||
import android.graphics.Rect; | ||
import android.support.annotation.ColorInt; | ||
import android.support.annotation.NonNull; | ||
|
||
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; | ||
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; | ||
|
||
import java.security.MessageDigest; | ||
|
||
/** | ||
* 带边框 | ||
* | ||
* @author : BaoZhou | ||
* @date : 2019/3/22 21:49 | ||
*/ | ||
public class BorderTransformation extends BitmapTransformation { | ||
private Paint mBorderPaint; | ||
private float mBorderWidth; | ||
private final String ID = getClass().getName(); | ||
|
||
|
||
public BorderTransformation(int borderWidth, @ColorInt int borderColor) { | ||
mBorderWidth = Resources.getSystem().getDisplayMetrics().density * borderWidth; | ||
mBorderPaint = new Paint(); | ||
mBorderPaint.setDither(true); | ||
mBorderPaint.setAntiAlias(true); | ||
mBorderPaint.setColor(borderColor); | ||
mBorderPaint.setStyle(Paint.Style.STROKE); | ||
mBorderPaint.setStrokeWidth(mBorderWidth); | ||
} | ||
|
||
@Override | ||
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { | ||
if (toTransform == null) { | ||
return null; | ||
} | ||
int width = toTransform.getWidth(); | ||
int height = toTransform.getHeight(); | ||
|
||
Bitmap bitmap = pool.get(width, height, Bitmap.Config.ARGB_8888); | ||
if (bitmap == null) { | ||
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); | ||
} | ||
Canvas canvas = new Canvas(bitmap); | ||
Paint paint = new Paint(); | ||
|
||
//绘制原图像 | ||
canvas.drawBitmap(toTransform, null, new Rect(0, 0, width, height), paint); | ||
|
||
//描绘边框 | ||
paint.setAntiAlias(true); | ||
if (mBorderPaint != null) { | ||
canvas.drawRect(0 + mBorderWidth/2, 0 + mBorderWidth/2, width - mBorderWidth/2, height - mBorderWidth/2, mBorderPaint); | ||
} | ||
return bitmap; | ||
} | ||
|
||
|
||
@Override | ||
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) { | ||
messageDigest.update((ID + mBorderWidth * 10).getBytes(CHARSET)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters