Basic use of ImageView in Android

Basic use of ImageView in Android

1. Common attributes

Attribute values ​​of commonly used scaleType :

  1. fitXY : Fully fit the control, the aspect ratio may change
  2. fitCenter : Keep the aspect ratio zoom until it can be fully displayed
  3. centerCrop : Keep the aspect ratio zoom until the control is completely covered, cropped display

Insert picture description here

<?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. Load web photos

Use the glide library.
https://github.com/bumptech/glide

Insert picture description here
Insert picture description here

how to use

Insert picture description here

Add permissions

Insert picture description here

<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);
    }
}

Note: If there is no display after running without the permission, the console reports an error. At this time, after adding the permission, you need to uninstall the test on the phone and run it to display normally.

Guess you like

Origin blog.csdn.net/YKenan/article/details/112689081