Skip to content

Commit

Permalink
添加loadBorderImage
Browse files Browse the repository at this point in the history
  • Loading branch information
BzCoder committed Apr 19, 2019
1 parent ffa2a2b commit a68a461
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
22 changes: 22 additions & 0 deletions LibEasyGlide/src/main/java/me/bzcoder/easyglide/EasyGlide.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import me.bzcoder.easyglide.progress.OnProgressListener;
import me.bzcoder.easyglide.progress.ProgressManager;
import me.bzcoder.easyglide.transformation.BlurTransformation;
import me.bzcoder.easyglide.transformation.BorderTransformation;
import me.bzcoder.easyglide.transformation.CircleWithBorderTransformation;
import me.bzcoder.easyglide.transformation.GrayscaleTransformation;
import me.bzcoder.easyglide.transformation.RoundedCornersTransformation;
Expand Down Expand Up @@ -199,6 +200,27 @@ public static void loadCircleWithBorderImage(Context context, String url, int bo
.build());
}


public static void loadBorderImage(Context context, String url, ImageView imageView) {
loadBorderImage(context, url, 2, Color.parseColor("#ACACAC"), imageView, placeHolderImageView);
}

public static void loadBorderImage(Context context, String url, int borderWidth, @ColorInt int borderColor, ImageView imageView) {
loadBorderImage(context, url, borderWidth, borderColor, imageView, placeHolderImageView);
}

public static void loadBorderImage(Context context, String url, int borderWidth, @ColorInt int borderColor, ImageView imageView, @DrawableRes int placeHolder) {
loadImage(context,
GlideConfigImpl
.builder()
.url(url)
.transformation(new BorderTransformation(borderWidth, borderColor))
.isCrossFade(true)
.errorPic(placeHolder)
.placeholder(placeHolder)
.imageView(imageView)
.build());
}
/**
* 提供了一下如下变形类,支持叠加使用
* BlurTransformation
Expand Down
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.security.MessageDigest;

/**
* 带边框的原型
* 带边框的圆形
* @author : BaoZhou
* @date : 2019/3/22 21:49
*/
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/me/bzcoder/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class MainActivity extends AppCompatActivity {
private ImageView iv10;
private ImageView iv11;
private ImageView iv12;
private ImageView iv13;
private CircleProgressView circleProgressView;

@Override
Expand Down Expand Up @@ -70,7 +71,7 @@ public void onClick(View v) {
iv10 = (ImageView) findViewById(R.id.iv_10);
iv11 = (ImageView) findViewById(R.id.iv_11);
iv12 = (ImageView) findViewById(R.id.iv_12);

iv13 = (ImageView) findViewById(R.id.iv_13);
EasyGlide.loadImage(this, url4, iv0, (isComplete, percentage, bytesRead, totalBytes) -> {
// if (isComplete) {
// circleProgressView.setVisibility(View.GONE);
Expand Down Expand Up @@ -122,6 +123,8 @@ public void onClick(View v) {

EasyGlide.loadImage(this, R.drawable.test, iv11);
EasyGlide.loadImage(this, "", iv12);

EasyGlide.loadBorderImage(this, url2, iv13);
}

private boolean hasStoragePermission() {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginBottom="15dp" />

<ImageView
android:id="@+id/iv_13"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginBottom="15dp" />
</LinearLayout>

</ScrollView>

0 comments on commit a68a461

Please sign in to comment.