LoginSignup
9
12

More than 5 years have passed since last update.

ImageViewをタッチしたらハイライトさせる

Posted at

画像をタップすると拡大表示させたり、画像付きボタンを使ったりするシーンはよくあると思います。以下はその画像をタップ時にピカッとハイライトさせてあげる方法です。

ImageViewHighlighter.java
public class ImageViewHighlighter implements View.OnTouchListener {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                ((ImageView) v).setColorFilter(Color.argb(100, 255, 255, 255));
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                ((ImageView) v).setColorFilter(null);
                break;
        }
        return false;
    }
}
呼び出し側
imageView.setOnTouchListener(new ImageViewHighlighter());

ImageViewを継承しているImageButtonでも使用できます。

9
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
12