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

วิธีล้างภาพใน ImageView บน Android

ภาพรวม

ตัวอย่างนี้สาธิตวิธีล้าง (Clear) รูปภาพที่แสดงใน ImageView ให้กลับสู่สถานะว่างเปล่า ใน Android มีหลายวิธี การเลือกวิธีขึ้นอยู่กับว่า set รูปภาพเข้ามาแบบใด (setImageResource, setBackground, setImageDrawable)

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

สร้างโปรเจกต์ Android ใหม่ใน Android Studio

ขั้นตอนที่ 2: ออกแบบ Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:onClick="clickHere"
        android:text="ล้างรูปภาพ" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_below="@id/imageView"
        android:background="@drawable/wallpaper" />

</RelativeLayout>

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

package app.com.sample;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.imageView);
    }

    public void clickHere(View view) {
        // วิธีที่ 1: ล้าง background drawable (สำหรับ android:background)
        imageView.setBackgroundDrawable(null);
        
        // วิธีอื่นๆ ที่ใช้ได้:
        // imageView.setImageDrawable(null);     // สำหรับ setImageDrawable/setImageResource
        // imageView.setImageBitmap(null);       // สำหรับ setImageBitmap
        // imageView.setImageResource(0);        // ตั้งค่าเป็น 0 (transparent)
        // imageView.setImageURI(null);          // สำหรับ setImageURI
    }
}

ขั้นตอนที่ 4: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

สรุปวิธีล้าง ImageView ตามประเภทการตั้งค่า

วิธีตั้งค่ารูปวิธีล้างที่ถูกต้อง
android:background="@drawable/..."setBackgroundDrawable(null) หรือ setBackground(null)
setImageResource(R.drawable.xxx)setImageDrawable(null) หรือ setImageResource(0)
setImageDrawable(drawable)setImageDrawable(null)
setImageBitmap(bitmap)setImageBitmap(null)
setImageURI(uri)setImageURI(null)
Glide/Picasso .into(imageView)Glide.with(context).clear(imageView) หรือ Picasso.get().clear(imageView)

หมายเหตุ

  • setBackgroundDrawable() เป็น deprecated ใน API 16+ ควรใช้ setBackground() แทน
  • ถ้าใช้ Library โหลดรูป (Glide, Picasso, Coil) ต้องใช้คำสั่ง clear ของ library นั้นๆ
  • การตั้งค่า android:background และ setImageResource เป็นคนละ layer กัน ต้องล้างให้ถูก layer