Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách xóa hình ảnh trong ImageView trên Android

Giới thiệu

Bài viết này hướng dẫn cách xóa (clear) hình ảnh đang hiển thị trong ImageView trên Android bằng cách đặt background về null.

Các bước thực hiện

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.

Bước 2: Thiết kế giao diện (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="Xóa hình ảnh" />

    <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>

Bước 3: Viết code xử lý (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) {
        // Cách 1: Xóa background (API cũ, deprecated từ API 22)
        imageView.setBackgroundDrawable(null);

        // Cách 2: Khuyên dùng (API 16+)
        // imageView.setBackground(null);

        // Cách 3: Nếu dùng setImageResource/setImageDrawable
        // imageView.setImageDrawable(null);
        // imageView.setImageResource(0);
    }
}

Bước 4: Cấu hình Manifest (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>

Chạy ứng dụng

Kết nối thiết bị, nhấn Run, chọn thiết bị. Ban đầu hình ảnh hiển thị, nhấn nút "Xóa hình ảnh" để xóa.

Các phương pháp xóa ImageView

Phương pháp Mô tả API Level
setImageDrawable(null) Xóa drawable được set qua setImageDrawable hoặc setImageResource 1+
setImageResource(0) Đặt resource ID về 0 (không có resource nào) 1+
setBackground(null) Xóa background drawable (khuyên dùng) 16+
setBackgroundDrawable(null) Deprecated từ API 22 1-21
setImageBitmap(null) Xóa bitmap được set qua setImageBitmap 1+

Lưu ý

  • Phân biệt rõ src (image content) và background. setImageDrawable ảnh hưởng đến src, setBackground ảnh hưởng đến background.
  • Nếu dùng Glide/Picasso: Glide.with(context).clear(imageView) hoặc Picasso.get().cancelRequest(imageView).
  • Để giải phóng bộ nhớ bitmap lớn, cân nhắc gọi bitmap.recycle() (API < 19) hoặc để GC tự xử lý.