【问题标题】:Large Image Not Showing in ImageView using Picasso使用毕加索的 ImageView 中未显示大图像
【发布时间】:2021-06-09 07:25:15
【问题描述】:

这是我的Image

我想用ScrollView显示ImageView

代码:

  Picasso.get().load(img_url2)
            .fit().centerInside()
            .into(imageView2);

XML:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#dddddd">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/img2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"/>

    </LinearLayout>
</ScrollView>

【问题讨论】:

    标签: android imageview picasso


    【解决方案1】:

    创建自定义图像视图以显示比例。

    import android.content.Context;
    import android.graphics.drawable.Drawable;
    import android.util.AttributeSet;
    
    public class ProportionalImageView extends androidx.appcompat.widget.AppCompatImageView {
     
        public ProportionalImageView(Context context) {
            super(context);
        }
     
        public ProportionalImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
     
        public ProportionalImageView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
     
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            Drawable d = getDrawable();
            if (d != null) {
                int w = MeasureSpec.getSize(widthMeasureSpec);
                int h = w * d.getIntrinsicHeight() / d.getIntrinsicWidth();
                setMeasuredDimension(w, h);
            }
            else super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
    

    如下使用这个 ImageView

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toBottomOf="@id/actionBar">
    
                <com.dukaan.customviews.ProportionalImageView
                    android:id="@+id/ivBanner"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    app:layout_constraintTop_toBottomOf="@id/actionBar" />
            </ScrollView>
    

    然后在设置图像之后,像往常一样,使用 Glide 或 Picasso。

    我更喜欢像下面这样设置

    Glide.with(context)
                .load(url.equals("") ? R.drawable.task_info_image : url)
                .addListener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, 
                       Object model, Target<Drawable> target, boolean 
                          isFirstResource) {
                             dataBinding.progress.setVisibility(View.GONE);
                             return false;
                    }
    
                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        dataBinding.progress.setVisibility(View.GONE);
                        dataBinding.ivBanner.setImageDrawable(resource);
                        return false;
                    }
                })
                .into(dataBinding.ivBanner);
    

    如你所愿。

    【讨论】:

    • 感谢您的效果,但我想要 Picasso 或 Glide 库
    【解决方案2】:

    我认为问题在于您没有在没有布局大小的情况下向 imageView 添加任何图像。 首先,您应该将图像添加到 imageView。要做到这一点,请按照以下步骤操作

    1 复制您的图像并粘贴到可绘制文件(app-res-drawable)。然后将其命名为文件名

    2 将此行添加到您的 imageView

    android:background="@drawable/file-name

    【讨论】:

    • 我的图片很大,所以我应该使用云存储
    【解决方案3】:

    我花了几天时间寻找解决方案。下面我添加了整个必要的代码

    在清单中

     android:hardwareAccelerated="false"
    

    在 XML 文件中

    <ProgressBar
        android:id="@+id/progressBarId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
    
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#dddddd">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <com.jsibbold.zoomage.ZoomageView
                android:id="@+id/imageViewId"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scrollbars="vertical"
                android:scaleType="matrix"
                app:zoomage_animateOnReset="true"
                app:zoomage_doubleTapToZoom="true"
                app:zoomage_maxScale="8"
                app:zoomage_zoomable="true" />
        </LinearLayout>
    </ScrollView>
    

    在 Java 代码中

        progressBar.setVisibility(View.VISIBLE);
        Glide.with(getApplicationContext())
                .load(pdf_link)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }
    
                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        progressBar.setVisibility(View.GONE);
                        return false;
                    }
                })
                .into(imageView);
    

    在依赖中

    implementation 'com.jsibbold:zoomage:1.3.1'
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-02
      • 2019-12-12
      • 1970-01-01
      相关资源
      最近更新 更多