forked from hzuapps/android-labs-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
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
unknown
authored and
unknown
committed
Dec 23, 2020
1 parent
3643663
commit 2a1eafa
Showing
2 changed files
with
140 additions
and
82 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
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,53 @@ | ||
package edu.hzuapps.androidlabs.net1814080903202; | ||
|
||
import android.content.Context; | ||
import android.content.res.Resources; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapShader; | ||
import android.graphics.Canvas; | ||
import android.graphics.Paint; | ||
import android.graphics.RectF; | ||
|
||
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; | ||
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; | ||
import com.bumptech.glide.load.resource.bitmap.TransformationUtils; | ||
|
||
import java.security.MessageDigest; | ||
|
||
public class RoundTransform extends BitmapTransformation { | ||
private static float radius = 0f; | ||
public RoundTransform(Context context, int dp) { | ||
this.radius = Resources.getSystem().getDisplayMetrics().density * dp; | ||
} | ||
|
||
@Override | ||
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { | ||
Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight); | ||
return roundCrop(pool, bitmap); | ||
} | ||
|
||
private static Bitmap roundCrop(BitmapPool pool, Bitmap source) { | ||
if (source == null) return null; | ||
Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); | ||
if (result == null) { | ||
result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); | ||
} | ||
|
||
Canvas canvas = new Canvas(result); | ||
Paint paint = new Paint(); | ||
paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); | ||
paint.setAntiAlias(true); | ||
RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight()); | ||
canvas.drawRoundRect(rectF, radius, radius, paint); | ||
return result; | ||
} | ||
|
||
public String getId() { | ||
return getClass().getName() + Math.round(radius); | ||
} | ||
|
||
@Override | ||
public void updateDiskCacheKey(MessageDigest messageDigest) { | ||
|
||
} | ||
} |