Skip to content

Commit

Permalink
添加加载本地图片功能
Browse files Browse the repository at this point in the history
  • Loading branch information
BzCoder committed Mar 26, 2019
1 parent d713ecb commit 7797622
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 20 deletions.
1 change: 0 additions & 1 deletion LibEasyGlide/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,4 @@ dependencies {
implementation rootProject.ext.dependencies["rxjava2"]
implementation rootProject.ext.dependencies["okhttp3"]
implementation rootProject.ext.dependencies["rxandroid2"]

}
2 changes: 2 additions & 0 deletions LibEasyGlide/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ public *;
}
# for DexGuard only
-keepresourcexmlelements manifest/application/meta-data@value=GlideModule
-keep class me.jessyan.progressmanager.** { *; }
-keep interface me.jessyan.progressmanager.** { *; }
42 changes: 33 additions & 9 deletions LibEasyGlide/src/main/java/me/bzcoder/easyglide/EasyGlide.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.annotation.RawRes;
import android.text.TextUtils;
import android.widget.ImageView;

Expand Down Expand Up @@ -42,6 +44,8 @@ public class EasyGlide {
public static int placeHolderImageView = R.color.transparent;
public static int circlePlaceholderImageView = R.color.transparent;



public static void loadImage(Context context, String url, ImageView imageView) {
loadImage(context, url, imageView, placeHolderImageView, null);
}
Expand Down Expand Up @@ -113,7 +117,7 @@ public static void loadGrayImage(Context context, String url, ImageView imageVie
GlideConfigImpl
.builder()
.url(url)
.transformation(new CenterCrop(),new GrayscaleTransformation())
.transformation(new CenterCrop(), new GrayscaleTransformation())
.isCrossFade(true)
.errorPic(placeHolder)
.placeholder(placeHolder)
Expand All @@ -135,7 +139,7 @@ public static void loadBlurImage(Context context, String url, int radius, ImageV
GlideConfigImpl
.builder()
.url(url)
.transformation(new CenterCrop(),new BlurTransformation(context, radius))
.transformation(new CenterCrop(), new BlurTransformation(context, radius))
.isCrossFade(true)
.errorPic(placeHolder)
.placeholder(placeHolder)
Expand All @@ -157,7 +161,7 @@ public static void loadRoundCornerImage(Context context, String url, int radius,
GlideConfigImpl
.builder()
.url(url)
.transformation(new CenterCrop(),new RoundedCornersTransformation(radius, margin))
.transformation(new CenterCrop(), new RoundedCornersTransformation(radius, margin))
.isCrossFade(true)
.errorPic(placeHolder)
.placeholder(placeHolder)
Expand All @@ -173,7 +177,7 @@ public static void loadCircleWithBorderImage(Context context, String url, int bo
loadCircleWithBorderImage(context, url, borderWidth, borderColor, imageView, placeHolderImageView);
}

public static void loadCircleWithBorderImage(Context context, String url, int borderWidth, @ColorInt int borderColor, ImageView imageView, @DrawableRes int placeHolder) {
public static void loadCircleWithBorderImage(Context context, String url, int borderWidth, @ColorInt int borderColor, ImageView imageView, @DrawableRes int placeHolder) {
loadImage(context,
GlideConfigImpl
.builder()
Expand Down Expand Up @@ -211,6 +215,21 @@ public static void loadImageWithTransformation(Context context, String url, Imag
.build());
}


/**
* 加载本地图片
* @param context
* @param drawableId
* @param imageView
*/
public static void loadImage(Context context, @RawRes @DrawableRes @Nullable Integer drawableId, ImageView imageView) {
loadImage(context, GlideConfigImpl
.builder()
.drawableId(drawableId)
.isCropCenter(true)
.imageView(imageView)
.build());
}
/**
* 预加载
*
Expand All @@ -224,14 +243,19 @@ public static void preloadImage(Context context, String url) {
public static void loadImage(Context context, GlideConfigImpl config) {
Preconditions.checkNotNull(context, "Context is required");
Preconditions.checkNotNull(config, "ImageConfigImpl is required");
if (TextUtils.isEmpty(config.getUrl())) {
//throw new NullPointerException("Url is required");
}

Preconditions.checkNotNull(config.getImageView(), "ImageView is required");
GlideRequests requests;
requests = EasyGlideApp.with(context);
GlideRequest<Drawable> glideRequest;
glideRequest = requests.load(config.getUrl());
GlideRequest<Drawable> glideRequest = null;
if (!TextUtils.isEmpty(config.getUrl())) {
glideRequest = requests.load(config.getUrl());
}

if (config.getDrawableId() != 0) {
glideRequest = requests.load(config.getDrawableId());
}

//缓存策略
switch (config.getCacheStrategy()) {
case 0:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,43 @@
* ================================================
*/
public class GlideConfigImpl extends ImageConfig {
private int cacheStrategy;//0对应DiskCacheStrategy.all,1对应DiskCacheStrategy.NONE,2对应DiskCacheStrategy.SOURCE,3对应DiskCacheStrategy.RESULT
private int fallback; //请求 url 为空,则使用此图片作为占位符

/**
* 0 对应DiskCacheStrategy.all
* 1 对应DiskCacheStrategy.NONE
* 2 对应DiskCacheStrategy.SOURCE
* 3 对应DiskCacheStrategy.RESULT
*/

private int cacheStrategy;
//请求 url 为空,则使用此图片作为占位符
private int fallback;
private BitmapTransformation[] transformation;//glide用它来改变图形的形状
private ImageView[] imageViews;
private boolean isClearMemory;//清理内存缓存
private boolean isClearDiskCache;//清理本地缓存
//清理内存缓存
private boolean isClearMemory;
//清理本地缓存
private boolean isClearDiskCache;
private Drawable placeholderDrawable;
private int resizeX;
private boolean isCropCenter;
private boolean isCropCircle;
private boolean isFitCenter;
private DecodeFormat formate;//图片格式
//图片格式
private DecodeFormat formate;
private int resizeY;
private int imageRadius;//图片每个圆角的大小
private int blurValue;//高斯模糊值, 值越大模糊效果越大
private boolean isCrossFade;//是否使用淡入淡出过渡动画
private OnProgressListener onProgressListener;//监听加载进度
//图片每个圆角的大小
private int imageRadius;
//高斯模糊值, 值越大模糊效果越大
private int blurValue;
//是否使用淡入淡出过渡动画
private boolean isCrossFade;
//监听加载进度
private OnProgressListener onProgressListener;

private GlideConfigImpl(Builder builder) {
this.url = builder.url;
this.drawableId = builder.drawableId;
this.imageView = builder.imageView;
this.placeholder = builder.placeholder;
this.placeholderDrawable = builder.placeholderDrawable;
Expand Down Expand Up @@ -161,6 +178,7 @@ public static Builder builder() {
public static final class Builder {
private int resizeX;
private String url;
private int drawableId;
private ImageView imageView;
private int placeholder;
private Drawable placeholderDrawable;
Expand Down Expand Up @@ -189,6 +207,11 @@ public Builder url(String url) {
return this;
}

public Builder drawableId(int drawableId) {
this.drawableId = drawableId;
return this;
}

public Builder placeholder(int placeholder) {
this.placeholder = placeholder;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

public class ImageConfig {
protected String url;

protected int drawableId;
protected ImageView imageView;
protected int placeholder;
protected int errorPic;
Expand All @@ -31,6 +33,9 @@ public String getUrl() {
public ImageView getImageView() {
return imageView;
}
public int getDrawableId() {
return drawableId;
}

public int getPlaceholder() {
return placeholder;
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 @@ -35,6 +35,7 @@ public class MainActivity extends AppCompatActivity {
private ImageView iv8;
private ImageView iv9;
private ImageView iv10;
private ImageView iv11;
private CircleProgressView circleProgressView;

@Override
Expand Down Expand Up @@ -63,7 +64,7 @@ public void onClick(View v) {
iv8 = (ImageView) findViewById(R.id.iv_8);
iv9 = (ImageView) findViewById(R.id.iv_9);
iv10 = (ImageView) findViewById(R.id.iv_10);

iv11 = (ImageView) findViewById(R.id.iv_11);


EasyGlide.loadImage(this, url4, iv0, (isComplete, percentage, bytesRead, totalBytes) -> {
Expand Down Expand Up @@ -107,6 +108,8 @@ public void onClick(View v) {

EasyGlide.loadImageWithTransformation(this, url2, iv10, new BlurTransformation(this, 20)
, new GrayscaleTransformation(), new CircleCrop());

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


Expand Down
Binary file added app/src/main/res/drawable/test.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 @@ -100,6 +100,12 @@
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginBottom="15dp" />

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

</ScrollView>

0 comments on commit 7797622

Please sign in to comment.