android : imageButton 背景 : 在 state_pressed 上实施以缩放图像

android : imageButton background : implement on state_pressed to scale the image

我的 android xml 布局中有:

 <ImageButton
    android:id="@+id/imageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="8dp"
    android:background="@null"
    android:src="@drawable/ic_action_next_item_up" />

我需要在后台实现 Imagebutton 属性 : 当用户点击 imageButton 时,它会放大图像。 (setScaleX(1.5)...)

请问有办法吗?

也许 inset Drawable 可以帮助你- 我正在使用以下代码-

     <inset xmlns:android="http://schemas.android.com/apk/res/android"
     android:drawable="@drawable/your_shape_drawable" android:insetBottom="10dip"
     android:insetLeft="10dip" android:insetRight="10dip" android:insetTop="10dip"
     android:visible="true" />

编辑:示例

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <inset android:insetBottom="-10dp" android:insetLeft="-10dp" android:insetRight="-10dp" android:insetTop="-10dp">
            <bitmap android:src="@drawable/ic_launcher" />
        </inset>
    </item>

    <item>
        <inset android:insetBottom="10dp" android:insetLeft="10dp" android:insetRight="10dp" android:insetTop="10dp">
            <bitmap android:src="@drawable/ic_launcher" />
        </inset>
    </item>
</selector>

在 Java 中执行并使用 ScaleAnimation ,例如:

   ScaleAnimation scaleAnimation = new ScaleAnimation(1, 1.5f, 1, 1.5f);

    scaleAnimation.setDuration(1000);  // animates for 1000 ms
    scaleAnimation.setFillAfter(true); // stays 1.5f after animation

    yourImageView.startAnimation(scaleAnimation);

并在您的 ImageButton OnClickListener 中启动动画