Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีโหลดและแสดงภาพใน ImageView บน Android App

ภาพรวม

ตัวอย่างนี้สาธิตวิธีการโหลดและแสดงภาพใน ImageView บน Android App โดยเปลี่ยนภาพเมื่อกดปุ่ม

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio → File → New Project → กรอกรายละเอียดที่จำเป็น

ขั้นตอนที่ 2: จัดทำ Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MyAndroidAppActivity">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btnChangeImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="เปลี่ยนภาพ" />

</LinearLayout>

ขั้นตอนที่ 3: เขียนโค้ด MainActivity (src/MyAndroidAppActivity.java)

package com.example.sample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
import android.view.View.OnClickListener;

public class MyAndroidAppActivity extends AppCompatActivity {
    Button button;
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addListenerOnButton();
    }

    public void addListenerOnButton() {
        image = (ImageView) findViewById(R.id.imageView1);
        button = (Button) findViewById(R.id.btnChangeImage);

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // โหลดภาพจาก drawable resource
                image.setImageResource(R.drawable.ic_launcher_background);
            }
        });
    }
}

ขั้นตอนที่ 4: กำหนดค่า AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.sample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MyAndroidAppActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

วิธีการโหลดภาพแบบต่างๆ

เมธอด คำอธิบาย
setImageResource(int resId) โหลดจาก drawable resource (R.drawable.xxx)
setImageDrawable(Drawable d) ตั้งค่า Drawable โดยตรง
setImageBitmap(Bitmap bm) ตั้งค่า Bitmap object
setImageURI(Uri uri) โหลดจาก URI (content provider, file, etc.)

การทดสอบ

รันแอปบนอุปกรณ์จริง กดปุ่ม "เปลี่ยนภาพ" จะแสดงภาพ ic_launcher_background

วิธีโหลดและแสดงภาพใน ImageView บน Android App