Android
 Computer >> コンピューター >  >> プログラミング >> Android

KotlinでAndroidのImageViewをクリアする方法をステップ解説

本記事では、Kotlinを使用してAndroidアプリのImageViewに表示されている画像をクリア(非表示に)する方法を、実際のコード例とともに詳しく解説します。ボタンをタップするとImageViewの背景画像が削除され、トーストメッセージで完了を通知するシンプルなサンプルです。

手順1:新規プロジェクトの作成

Android Studioを起動し、メニューから「File」→「New Project」を選択して、必要な情報を入力して新しいプロジェクトを作成します。

手順2:レイアウトファイル(activity_main.xml)の編集

res/layout/activity_main.xml に以下のコードを追加します。ボタンとImageViewを配置し、ImageViewには表示する画像を背景として設定しています。

コード例

<?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:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:onClick="clickHere"
    android:text="Click here" />
<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/button"
    android:layout_centerInParent="true"
    android:background="@drawable/image" />
</RelativeLayout>

手順3:MainActivity.ktの編集

src/MainActivity.kt に以下のコードを追加します。ボタンがクリックされると、setBackgroundDrawable(null) を呼び出してImageViewの背景画像をクリアし、トーストで「Image cleared」というメッセージを表示します。

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    lateinit var imageView: ImageView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
        imageView = findViewById(R.id.imageView)
    }
    fun clickHere(view: View) {
        imageView.setBackgroundDrawable(null)
        Toast.makeText(this, "Image cleared", Toast.LENGTH_SHORT).show()
    }
}

なお、現在のAndroid APIレベルでは setBackgroundDrawable() は非推奨となっているため、代わりに imageView.setImageDrawable(null)imageView.setBackgroundResource(0) を使用する方法も推奨されます。

手順4:AndroidManifest.xmlの編集

androidManifest.xml に以下のコードを追加します。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.q11">
    <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>

アプリの実行と動作確認

それでは、アプリを実行してみましょう。Android端末をパソコンに接続した状態で、Android Studioからプロジェクトのアクティビティファイルを開き、ツールバーの「Run」アイコンをクリックします。実行デバイスとして接続したモバイル端末を選択すると、端末の画面にアプリが表示されます。

初期状態ではImageViewに画像が表示されていますが、画面下部の「Click here」ボタンをタップすると、画像がクリアされ「Image cleared」というトーストメッセージが表示されます。

  1. 【Android】PicassoライブラリでURLからImageViewに画像を読み込む方法

    Androidアプリ開発では、ネットワーク上の画像をImageViewに表示したい場面がよくあります。本記事では、Square社が提供する画像読み込みライブラリ「Picasso」を使用して、URLから画像を取得しImageViewに表示する方法を、手順を追ってわかりやすく解説します。Picassoとは?Picassoは、Android向けの定番画像ライブラリです。URLを指定するだけで、画像のダウンロード・キャッシュ管理・メモリ効率の最適化を自動的に行ってくれるため、わずか1行のコードで画像を表示できます。実装手順ステップ1:新規プロジェクトを作成するAndroid Studioを起動し、「F

  2. 【Android】コードからImageViewのマージン(余白)を設定する方法

    この記事では、Androidアプリ開発においてJavaコードを使ってImageViewのマージン(余白)を動的に設定する方法を解説します。レイアウトXMLではなくプログラム側で余白を制御したい場合に役立つテクニックです。全体の流れ今回のサンプルでは、RelativeLayoutの中に配置したImageViewに対して、setMargins()メソッドを使って上下左右すべてに余白を設定します。ステップ1:新規プロジェクトを作成するまず、Android Studioで新しいプロジェクトを作成しましょう。メニューから「File ⇒ New Project」を選択し、必要な項目を入力してプロジェクトを