ChrisGeo
ChrisGeo

Reputation: 3907

Android ImageButton scale down on press

I want to add an ImageButton to my layout that has the effect of scaling down in order to look like it's being pressed. So I basically have the original image @drawable/scan and the pressed image which is @drawable/scanhot. Scanhot is basically the same image scaled down.

My code is the following but it doesnt seem to work.

As you can see I am using a Button as opposed to an ImageButton because a Button could at least "switch" different images. However it does not do anything with similar images like scan/scanhot

button_rescan.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:drawable="@drawable/scanhot"/>
    <item
        android:drawable="@drawable/scan"/>
</selector>    

layout

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    android:background="@android:color/transparent">

        <Button
            android:id="@+id/fragment_radar_rescan_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/button_rescan"
            android:layout_gravity="center_horizontal"
            android:layout_centerHorizontal="true"/>
</RelativeLayout

Upvotes: 2

Views: 752

Answers (2)

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

It looks that drawable/scanhot are not the same size drawable/scan (in pixels). So if you want effect of "zoom" (or whatever) you should still keep image size the same for both images (i.e. 100x100) and with the same size make scanhot look smaller. If you already got images scalled as you want, just load the smaller one to Gimp or Photoshop and scale its canvas to the size of the other image (which is bigger I assume) and then save. In that case canvas for both will be the same and you avoid additional scalling from ImageView

Upvotes: 2

Andrew Schuster
Andrew Schuster

Reputation: 3229

The problem with what you are doing is that when you set a picture to a background, it automatically is blown up to fit the View. Instead, both pictures should have the same resolution, but the picture in scanhot should just look smaller.

In otherwords, both pictures should have a 132x29 resolution (or rather, they should both be the same size)

Upvotes: 1

Related Questions