AndroidでのImageViewの基本的な使用法
1.共通の属性
一般的に使用されるscaleTypeの属性値:
- fitXY:コントロールに完全にフィットし、アスペクト比が変わる可能性があります
- fitCenter:完全に表示されるまでアスペクト比ズームを維持します
- centerCrop:コントロールが完全に覆われ、トリミングされたディスプレイになるまで、アスペクト比のズームを維持します
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ImageViewActivity">
<ImageView
android:id="@+id/iv_1"
android:layout_width="300dp"
android:layout_height="100dp"
android:src="@mipmap/beautiful"
android:layout_marginBottom="15dp"
android:contentDescription="@string/beautiful"
android:scaleType="fitXY"/>
<ImageView
android:id="@+id/iv_2"
android:layout_width="300dp"
android:layout_height="100dp"
android:src="@mipmap/beautiful"
android:layout_marginBottom="15dp"
android:contentDescription="@string/beautiful"
android:scaleType="fitCenter"/>
<ImageView
android:id="@+id/iv_3"
android:layout_width="300dp"
android:layout_height="50dp"
android:src="@mipmap/beautiful"
android:contentDescription="@string/beautiful"
android:scaleType="centerCrop"/>
</LinearLayout>
2.Web写真をロードします
グライドライブラリを使用します
。https://github.com/bumptech/glide
使い方
権限を追加する
<uses-permission android:name="android.permission.INTERNET" />
package com.example.hello;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class ImageViewActivity extends AppCompatActivity {
// 声明
private ImageView iv4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_view);
// 得到 ImageView
iv4 = findViewById(R.id.iv_4);
// 加载网路图
Glide.with(this).load("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=295289102,3688591987&fm=26&gp=0.jpg").into(iv4);
}
}
注:許可なく実行しても表示が表示されない場合、コンソールからエラーが報告されます。このとき、許可を追加した後、電話でテストをアンインストールして実行し、正常に表示する必要があります。