Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

android - How to make Button highlight?

I set a image as Button backgroud,but Button has no highlight when click it. Is there any way to solve it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If this is an ImageButton and you don't want to use several drawable for each pressed/unpressed state you could use the color filter attibute of images. This solution is similar to the one used by Omar.

Create an OnTouchListener modifying the color filter on touch.

public class ButtonHighlighterOnTouchListener implements OnTouchListener {

  final ImageButton imageButton;

  public ButtonHighlighterOnTouchListener(final ImageButton imageButton) {
    super();
    this.imageButton = imageButton;
  }

  public boolean onTouch(final View view, final MotionEvent motionEvent) {
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
      //grey color filter, you can change the color as you like
      imageButton.setColorFilter(Color.argb(155, 185, 185, 185));
    } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
      imageButton.setColorFilter(Color.argb(0, 185, 185, 185)); 
    }
    return false;
  }

}

Assign this listener to your button:

  myButton = (ImageButton) findViewById(R.id.myButton);
  myButton.setOnTouchListener(new ButtonHighlighterOnTouchListener(myButton));

Update

Improved class to apply highlighter to ImageView, ImageButton or TextView via its compound Drawable.

public class ButtonHighlighterOnTouchListener implements OnTouchListener {

  private static final int TRANSPARENT_GREY = Color.argb(0, 185, 185, 185);
  private static final int FILTERED_GREY = Color.argb(155, 185, 185, 185);

  ImageView imageView;
  TextView textView;

  public ButtonHighlighterOnTouchListener(final ImageView imageView) {
    super();
    this.imageView = imageView;
  }

  public ButtonHighlighterOnTouchListener(final TextView textView) {
    super();
    this.textView = textView;
  }

  public boolean onTouch(final View view, final MotionEvent motionEvent) {
    if (imageView != null) {
      if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        imageView.setColorFilter(FILTERED_GREY);
      } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
        imageView.setColorFilter(TRANSPARENT_GREY); // or null
      }
    } else {
      for (final Drawable compoundDrawable : textView.getCompoundDrawables()) {
        if (compoundDrawable != null) {
          if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            // we use PorterDuff.Mode. SRC_ATOP as our filter color is already transparent
            // we should have use PorterDuff.Mode.LIGHTEN with a non transparent color
            compoundDrawable.setColorFilter(FILTERED_GREY, PorterDuff.Mode.SRC_ATOP);
          } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
            compoundDrawable.setColorFilter(TRANSPARENT_GREY, PorterDuff.Mode.SRC_ATOP); // or null
          }
        }
      }
    }
    return false;
  }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...