android - 如何使来自互联网的图像响应 Android 布局?
问题描述
我的布局上有一个 Imageview,我用从网上下载的 .jpg 填充。问题是我试图让图像容器响应屏幕尺寸,因为在较大的手机中,图像看起来很小。
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String imageUrl="https:url";
new DownloadImageTask((ImageView) findViewById(R.id.imvAd))
.execute(imageUrl);
}
}
我不想给出固定值。
任何关于寻找什么的帮助或建议都会很棒
这是布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".startActivities.LoginActivity"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5"
android:background="@color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imvLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@mipmap/logo"
tools:ignore="ContentDescription" />
<LinearLayout
android:id="@+id/lytProgressBarLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<ProgressBar
android:id="@+id/pgbLogin"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/txtwelcome"
style="@style/styleTextview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/welcome" />
<EditText
android:id="@+id/edtPwdworker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/password"
android:inputType="numberPassword" />
<Button
android:id="@+id/btnLogin"
style="@style/styleButtonConsult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/login" />
</LinearLayout>
<TextView
android:id="@+id/txtInfoRoute"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_weight="90"
android:gravity="center" />
<TextView
android:id="@+id/txtPinDeviceLogin"
style="@style/styleTextviewsubTitlebold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="@string/pinDevice" />
<ImageView
android:id="@+id/imvAd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
tools:ignore="ContentDescription" />
</LinearLayout>
</ScrollView>
<TextView
android:id="@+id/txtVersionLogin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="95"
android:gravity="center" />
</LinearLayout>
解决方案
如果您的 ImageView 是固定的,您可以通过设置ScaleType
为CENTER_CROP
或其他相关比例类型来缩放图像,以使图像按您的ImageView
方式缩放。
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop" />
也不要使用wrap_content
,您可以使用它match_parent
来适应布局。或使用0dp
( in ConstraintLayout
) 并将 ImageView 约束在其他两个视图之间。
但是如果您希望 ImageView 的大小相对于屏幕的大小发生变化,您可以使用 aConstraintlayout
作为父级并layout_constraintDimensionRatio
像这样使用。
<androidx.constraintlayout.widget.ConstraintLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1" />
</androidx.constraintlayout.widget.ConstraintLayout>
除此之外,我建议使用图像加载器库,例如Glide。它比自己加载图像更有效,您也可以直接从那里设置比例类型。
推荐阅读
- java - 使用 Selenium WebDriver 启动 Firefox Profile 需要 6 分 15 秒?这正常吗?
- javascript - 在 localhost 上运行时无法在 React 应用程序上播放音频
- flutter - getter 在 null 上被调用
- javascript - Firestore 查询多个字段中的相同值
- python - 如何在python中的字符串中忽略以'\ xa0'错误开头的非UTF-8代码
- regex - 如何检查字符串是否是正则表达式的有效前缀?
- python - 在 Python 3.6.10 上运行 Async Flask 2.0.0 时出错
- javascript - 以编程方式从 3rd 方站点获取 js 数组
- javascript - 如何存储和遍历对象数组
- javascript - 使用搜索功能在 Next.js 应用程序中更新状态的问题