From f50e8e60e9da99e4f69d8ab76810dc7e5fb448bc Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期一, 02 十二月 2024 10:29:09 +0800 Subject: [PATCH] 2024年12月02日10:29:05 --- app/src/main/java/com/hdl/photovoltaic/utils/GlideUtils.java | 160 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 138 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/hdl/photovoltaic/utils/GlideUtils.java b/app/src/main/java/com/hdl/photovoltaic/utils/GlideUtils.java index cfeb8b5..887b410 100644 --- a/app/src/main/java/com/hdl/photovoltaic/utils/GlideUtils.java +++ b/app/src/main/java/com/hdl/photovoltaic/utils/GlideUtils.java @@ -9,18 +9,28 @@ import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.RectF; +import android.text.TextUtils; import android.widget.ImageView; +import androidx.annotation.Nullable; +import androidx.appcompat.content.res.AppCompatResources; import androidx.core.graphics.drawable.RoundedBitmapDrawable; import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory; import com.bumptech.glide.Glide; +import com.bumptech.glide.load.DataSource; +import com.bumptech.glide.load.engine.DiskCacheStrategy; +import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.load.resource.bitmap.CenterCrop; import com.bumptech.glide.load.resource.bitmap.CircleCrop; import com.bumptech.glide.load.resource.bitmap.GranularRoundedCorners; import com.bumptech.glide.load.resource.bitmap.RoundedCorners; +import com.bumptech.glide.load.resource.gif.GifDrawable; +import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestOptions; +import com.bumptech.glide.request.target.Target; import com.hdl.photovoltaic.R; +import com.hdl.photovoltaic.other.HdlLogLogic; import com.hdl.photovoltaic.other.HdlThreadLogic; import java.io.InputStream; @@ -39,10 +49,14 @@ * @param imageview 鏄剧ず鍥剧墖缁勪欢 */ public static void getGeneralImage(Context context, String url, ImageView imageview) { - Glide.with(context) - .load(url) - .apply(getRequestOptions()) - .into(imageview); + if (url.startsWith("http://") || url.startsWith("https://")) { + Glide.with(context) + .load(url) + .apply(getRequestOptions(false)) + .into(imageview); + } else { + setDefaultImage(context, imageview, true); + } } /** @@ -54,11 +68,16 @@ * @param granularRoundedCorners 璁剧疆鍥剧墖瑙掑害 */ public static void getGranularRoundedCornersImage(Context context, String url, ImageView imageview, GranularRoundedCorners granularRoundedCorners) { - Glide.with(context) - .load(url) - .apply(getRequestOptions()) - .transform(granularRoundedCorners)//new GranularRoundedCorners(30f,80f,80f,30f) - .into(imageview); + if (url.startsWith("http://") || url.startsWith("https://")) { + + Glide.with(context) + .load(url) + .apply(getRequestOptions(false)) + .transform(granularRoundedCorners)//new GranularRoundedCorners(30f,80f,80f,30f) + .into(imageview); + } else { + setDefaultImage(context, imageview, true); + } } /** @@ -70,12 +89,48 @@ * @param roundedCorners 璁剧疆鍥剧墖瑙掑害鍊� */ public static void getRoundedCornersImage(Context context, String url, ImageView imageview, RoundedCorners roundedCorners) { + if (!(url.startsWith("http://") || url.startsWith("https://"))) { + imageview.setImageDrawable(AppCompatResources.getDrawable(context, R.drawable.plant_default_picture)); + return; + } +// if (url.startsWith("http://") || url.startsWith("https://")) { Glide.with(context) .load(url) - .apply(getRequestOptions()) + .apply(new RequestOptions() + .placeholder(R.drawable.plant_default_picture) + .error(R.drawable.plant_default_picture) + .fallback(R.drawable.plant_default_picture)) .transform(new CenterCrop(), roundedCorners)//Glide鍔犺浇鍥剧墖鍦嗚鏁堟灉涓嶪mageView鐨凷caleType鍐茬獊闂 // .transform(roundedCorners) .into(imageview); +// } else { +// setDefaultImage(context, imageview, userImage); +// } + } + + /** + * 鍥剧墖鍥涗釜瑙掔粺涓�鎸囧畾 + * + * @param context 涓婁笅鏂� + * @param url 鍥剧墖鍦板潃 + * @param imageview 鏄剧ず鍥剧墖缁勪欢 + * @param roundedCorners 璁剧疆鍥剧墖瑙掑害鍊� + */ + public static void getRoundedCornersImage(Context context, String url, ImageView imageview, RoundedCorners roundedCorners, boolean userImage) { + if (!(url.startsWith("http://") || url.startsWith("https://"))) { + setDefaultImage(context, imageview, userImage); + return; + } +// if (url.startsWith("http://") || url.startsWith("https://")) { + Glide.with(context) + .load(url) + .apply(getRequestOptions(userImage)) + .transform(new CenterCrop(), roundedCorners)//Glide鍔犺浇鍥剧墖鍦嗚鏁堟灉涓嶪mageView鐨凷caleType鍐茬獊闂 +// .transform(roundedCorners) + .into(imageview); +// } else { +// setDefaultImage(context, imageview, userImage); +// } } /** @@ -85,21 +140,33 @@ * @param url 鍥剧墖鍦板潃 * @param imageview 鏄剧ず鍥剧墖缁勪欢 */ - public static void getCircleCropImage(Context context, String url, ImageView imageview) { - Glide.with(context) - .load(url) - .apply(getRequestOptions()) - .transform(new CircleCrop()) - .into(imageview); + public static void getCircleCropImage(Context context, String url, ImageView imageview, boolean userImage) { + + if (url.startsWith("http://") || url.startsWith("https://")) { + Glide.with(context) + .load(url) + .apply(getRequestOptions(userImage)) + .transform(new CircleCrop()) + .into(imageview); + } else { + setDefaultImage(context, imageview, userImage); + } } - private static RequestOptions getRequestOptions() { - return new RequestOptions() - .placeholder(R.drawable.default_user) - .error(R.drawable.default_user) - .fallback(R.drawable.default_user); + private static RequestOptions getRequestOptions(boolean userImage) { + if (userImage) { + return new RequestOptions() + .placeholder(R.drawable.default_image) + .error(R.drawable.default_image) + .fallback(R.drawable.default_image); + } else { + return new RequestOptions() + .placeholder(R.drawable.default_user) + .error(R.drawable.default_user) + .fallback(R.drawable.default_user); // .override(113, 130); + } } @@ -123,7 +190,7 @@ if (conn.getResponseCode() == 200) { InputStream inStream = conn.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inStream); - imageView.setImageBitmap(getRoundedCornerBitmap(bitmap,pixels)); + imageView.setImageBitmap(getRoundedCornerBitmap(bitmap, pixels)); } } catch (Exception e) { } @@ -160,4 +227,53 @@ canvas.drawBitmap(bitmap, rect, rect, paint); return output; } + + + /** + * 鐣岄潰绾挎暟鎹樉绀烘牱寮� + * + * @param context 涓婁笅鏂� + * @param imageview 鏄剧ず鍥剧墖缁勪欢 + */ + public static void getDrawableGifAnimation(Context context, ImageView imageview) { + if (context == null) { + return; + } + if (imageview == null) { + return; + } + Glide.with(context) + .asGif() + .load(R.drawable.null_data_icon)//(res鎴栬�単if鐨剈rl閾炬帴) + .diskCacheStrategy(DiskCacheStrategy.ALL) + .listener(new RequestListener<GifDrawable>() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) { + return false; + } + + @Override + public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) { +// resource.setLoopCount(1);鎺у埗鎾斁娆℃暟 + return false; + } + }) + .into(imageview); + } + + private static void setDefaultImage(Context context, ImageView imageview, boolean userImage) { + HdlThreadLogic.runMainThread(new Runnable() { + @Override + public void run() { + if (userImage) { + imageview.setImageDrawable(AppCompatResources.getDrawable(context, R.drawable.default_image)); + } else { + imageview.setImageDrawable(AppCompatResources.getDrawable(context, R.drawable.default_user)); + } + } + }); + + } + + } -- Gitblit v1.8.0