How to make button with custom background image show click animation in Android

前端 未结 5 1471
無奈伤痛
無奈伤痛 2020-12-05 16:27

How to make button show it is clicked (by setting it go down/some change) for buttons using custom background image in Android. I do not want to include more images and set

5条回答
  •  广开言路
    2020-12-05 16:59

    It's possible to do with just one image file using the ColorFilter method. However, ColorFilter expects to work with ImageViews and not Buttons, so you have to transform your buttons into ImageViews. This isn't a problem if you're using images as your buttons anyway, but it's more annoying if you had text... Anyway, assuming you find a way around the problem with text, here's the code to use:

    ImageView button = (ImageView) findViewById(R.id.button);
    button.setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
    

    That applies a red overlay to the button (the color code is the hex code for fully opaque red - first two digits are transparency, then it's RR GG BB.).

    You can make your ImageViews look like normal buttons by copying the btn_default_normal.9.png file from your sdkfolder/platforms/(android version/data/res/drawable to your own project. Then in your ImageView use android:background="@drawable/btn_normal_default" and android:src="..." to set an image inside the button.

提交回复
热议问题