商品详情图片模糊问题

商品详情图片模糊问题

老师您好,ui是这样的:

http://img1.sycdn.imooc.com//climg/5fd799e409cdffd411041476.jpg

原因是

http://img1.sycdn.imooc.com//climg/5fd79a13080a486910801920.jpg

长图数量不定,索引也不确定。

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
LovelyChubby 2020-12-15 09:31:28

这是一张图这么大?让ui分开切图,分开给,这样巨长的图加载进来慢不说,glide会做内存压缩就模糊了

你看淘宝就是分开给图

  • 提问者 我有明珠一颗 #1

    老师,领导要求我在ui不分开切图的情况下,想办法加载巨长图,使页面看起来不要太模糊。

    做法如下:

    adapter中,

    @Override
    protected void bindData(@NonNull BaseViewHolder holder, String data) {
    ImageView imageView = holder.getViewById(R.id.img_detail);
    // imageView.setImageBitmap(data);
    Glide.with(context).downloadOnly().load(data).into(new CustomTarget<File>() {
    @Override
    public void onResourceReady(@NonNull File resource, @Nullable Transition<? super File> transition) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P){
    imageView.setImageBitmap(decodeSampledBitmapFromResource(resource.getPath(),300,300));
    }else{
    imageView.setImageBitmap(decodeSampledBitmapFromResource(resource.getPath(),500,500));
    }
    }

    @Override
    public void onLoadCleared(@Nullable Drawable placeholder) {

    }
    });
    }
    public static Bitmap decodeSampledBitmapFromResource(String pathName, int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pathName, options);
    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(pathName, options);
    }
    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;
    if (height > reqHeight || width > reqWidth) {
    final int halfHeight = height / 2;
    final int halfWidth = width / 2;
    // Calculate the largest inSampleSize value that is a power of 2 and keeps both
    // height and width larger than the requested height and width.
    while ((halfHeight / inSampleSize) >= reqHeight
    && (halfWidth / inSampleSize) >= reqWidth) {
    inSampleSize *= 2;
    }
    }
    return inSampleSize;
    }
    onResourceReady中的处理逻辑不优雅,老师您觉得怎么处理优雅,或者说怎么处理才能更好的解决问题?


    2020-12-17 00:51:53
  • LovelyChubby 回复 提问者 我有明珠一颗 #2
    巨长途使用glide会被压缩模糊,应当使用bitmapRegionDecoder
    2020-12-18 10:06:52
  • 提问者 我有明珠一颗 回复 LovelyChubby #3
    package com.phdj.commodity.ui.view;

    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.util.AttributeSet;
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.appcompat.widget.AppCompatImageView;

    public class BigImageView extends AppCompatImageView {

    private String filePath;

    private int bitmapHeight;

    public BigImageView(@NonNull Context context) {
    this(context, null);
    }

    public BigImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, 0);
    }

    public BigImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
    if (bitmapHeight != 0 && bitmapHeight > canvas.getMaximumBitmapHeight()) {
    Bitmap bitmap = decodeSampledBitmapFromResource(filePath, canvas.getMaximumBitmapHeight() - 1);
    setImageBitmap(bitmap);
    }
    bitmapHeight = 0;
    super.onDraw(canvas);
    }

    public void setFilePath(String filePath) {
    this.filePath = filePath;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);
    bitmapHeight = options.outHeight;
    options.inJustDecodeBounds = false;

    setImageBitmap(BitmapFactory.decodeFile(filePath, options));
    }


    public static int calculateInSampleSize(BitmapFactory.Options options, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;

    int inSampleSize = 1;
    if (height > width * 2) {
    if (height > reqHeight) {
    while ((height / inSampleSize) >= reqHeight) {
    inSampleSize *= 2;
    }
    }
    }
    return inSampleSize;
    }

    public static Bitmap decodeSampledBitmapFromResource(String pathName, int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pathName, options);
    options.inSampleSize = calculateInSampleSize(options, reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(pathName, options);
    }
    }

    老师,这是我的方案,烦请看下和bitmapRegionDecoder,哪一个性能更佳?

    2021-01-28 20:52:32
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师