wjc
2024-12-02 f50e8e60e9da99e4f69d8ab76810dc7e5fb448bc
app/src/main/java/com/hdl/photovoltaic/utils/GlideUtils.java
@@ -9,9 +9,11 @@
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;
@@ -28,6 +30,7 @@
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;
@@ -46,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);
        }
    }
    /**
@@ -61,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);
        }
    }
    /**
@@ -77,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加载图片圆角效果与ImageView的ScaleType冲突问题
//                .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加载图片圆角效果与ImageView的ScaleType冲突问题
//                .transform(roundedCorners)
                .into(imageview);
//        } else {
//            setDefaultImage(context, imageview, userImage);
//        }
    }
    /**
@@ -92,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);
        }
    }
@@ -201,5 +261,19 @@
                .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));
                }
            }
        });
    }
}