ImageButtonクリック時のハイライト

ImageButtonに画像指定してクリックしても変化がないため、クリック感がないので、画像を使わずに色を変える。

enterFadeDurationとexitFadeDuration(ミリ秒)でフェードイン・アウト。

drawable/button_overlay.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:enterFadeDuration="500"
    android:exitFadeDuration="500">
    <!-- ImageButtonの画像クリック時に色を変える -->
    <item android:state_pressed="true">
        <!-- 丸ボタン用(oval) -->
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <!-- ボタンの色(半透明で薄いグレー) -->
            <solid android:color="#11000000"/>
            <!-- ボタンの枠線(透過してマージン代わり) -->
            <stroke android:width="10dp" android:color="@color/transparent"/>
        </shape>
    </item>
    <item android:drawable="@android:color/transparent"/>
</selector>

 

これをImageButtonのbackgroundに指定。

画像はsrcに指定。

layout.xml

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button_overlay"
    android:src="@mipmap/ic_play"
    />

 

 

参考

 

カテゴリー: Android, View タグ: , , パーマリンク

コメントを残す

メールアドレスが公開されることはありません。 ※ が付いている欄は必須項目です