问题描述
这是我的Image
我想用ImageView
展示ScrollView
代码:
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>
解决方法
创建自定义图像视图以显示比例。
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,DataSource dataSource,boolean isFirstResource) {
dataBinding.progress.setVisibility(View.GONE);
dataBinding.ivBanner.setImageDrawable(resource);
return false;
}
})
.into(dataBinding.ivBanner);
这符合您的期望。
,我认为问题在于您没有在没有布局大小的情况下向 imageView 添加任何图像。 首先,您应该将图像添加到 imageView。要做到这一点,请按照此步骤
1 复制您的图像并粘贴到可绘制文件(app-res-drawable)中。然后将其命名为文件名
2 将此行添加到您的 imageView
android:background="@drawable/file-name
我花了几天时间寻找解决方案。下面我添加了全部必要的代码
在清单中
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,boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource,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'