izuki
8/9/2017 - 5:56 AM

ImageViewを押した時に画像を変更させる

ImageViewを押した時に画像を変更させる

<!-- res/drawable/d_image_brightness_on.xml -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- selectは上から順に評価していき条件の一致したパターンを採用する事に注意する -->

    <!-- ボタンが押されたとき -->
    <item
        android:state_pressed="true"
        android:drawable="@drawable/btn_image_brightness_on_dwn" />

    <!-- 通常の状態 -->
    <item
        android:drawable="@drawable/btn_image_brightness_on_nml" />

</selector>
<!-- res/drawable/d_image_brightness_off.xml -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- selectは上から順に評価していき条件の一致したパターンを採用する事に注意する -->

    <!-- ボタンが押されたとき -->
    <item
        android:state_pressed="true"
        android:drawable="@drawable/btn_image_brightness_off_dwn" />

    <!-- 通常の状態 -->
    <item
        android:drawable="@drawable/btn_image_brightness_off_nml" />

</selector>
<?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:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!-- ビューを重ねて表示させたい場合は、FrameLayoutを使うと便利です -->
    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imgview_brightness_on"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_gravity="left"
        android:paddingLeft="15dp"
        android:paddingRight="15dp" />
    <ImageView
        android:id="@+id/imgview_brightness_off"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_gravity="left"
        android:paddingLeft="15dp"
        android:paddingRight="15dp" />
    </FrameLayout>
</LinearLayout>
package mekeizu.com.buttontapapp;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private enum BrightnessMode {
        ON,
        OFF
    }

    private ImageView imageViewOn = null;
    private ImageView imageViewOff = null;

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

        imageViewOn = (ImageView) findViewById(R.id.imgview_brightness_on);
        imageViewOff = (ImageView) findViewById(R.id.imgview_brightness_off);
        
        // drawableのXMLパラメータをImageViewに設定する
        imageViewOn.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.d_image_brightness_on, null));
        imageViewOff.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.d_image_brightness_off, null));

        imageViewOn.setOnClickListener(this);
        imageViewOff.setOnClickListener(this);

        // 初期値としてON状態をセットする
        setBrightnessMode(BrightnessMode.ON);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imgview_brightness_on:
                setBrightnessMode(BrightnessMode.OFF);
                break;
            case R.id.imgview_brightness_off:
                setBrightnessMode(BrightnessMode.ON);
                break;
        }
    }

    private void setBrightnessMode(@Nullable BrightnessMode mode) {
        if (mode == BrightnessMode.ON) {
            imageViewOn.setVisibility(View.VISIBLE);    // ビューの表示
            imageViewOff.setVisibility(View.INVISIBLE); // ビューの非表示
        } else if (mode == BrightnessMode.OFF) {
            imageViewOn.setVisibility(View.INVISIBLE);  // ビューの非表示
            imageViewOff.setVisibility(View.VISIBLE);   // ビューの表示
        }
    }
}