【问题标题】:Making Android image larger when pressed, and normal when released按下时使Android图像变大,释放时正常
【发布时间】:2015-04-21 19:48:14
【问题描述】:

我有一个应用程序,它有一个 ImageViev,可以单击它来执行操作。我希望它在按下时稍微大一点,并在它释放时恢复正常。问题是,我有 10 个不同的图像出现在 ImageViev 中,具体取决于具体情况。我已经阅读了很多关于此的主题,但似乎没有一个解决方案可以解决我的问题。我需要:

  • 按下按钮时放大图像,或将其替换为更大的版本。
  • 松开按钮时使图像缩小,或返回到正常版本的图像。
  • 当我在 ImageView 中更改图像时,按下时它仍会更改其大小。

到目前为止我已经尝试过了:

  • 创建一个可绘制项目,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:drawable="@drawable/b1big" />
    <item
        android:state_pressed="false"
        android:drawable="@drawable/b1"/>
    </selector>
    

但它不会起作用,因为Android会自动将图像缩放到ImageView的大小,所以不管b1big是否大于b1。

  • 更改可绘制项的宽度和高度,但它没有这些参数。

正在创建 10 个普通图像,如 image1、image2...image10,然后创建 10 个大图像,如 image1big...image10big,然后创建 10 个可绘制对象,如:

<item
    android:state_pressed="true"
    android:drawable="@drawable/image1big" />
<item
    android:state_pressed="false"
    android:drawable="@drawable/image1"/>

并将它们用作我的 ImageView 的源代码?那我该如何防止它们缩放呢?

【问题讨论】:

  • 您是否为您的ImageView 设置了android:scaleType = "center" 属性?
  • 如果不需要重叠相邻视图并且不需要增加太多尺寸,则可以在正常状态下为其填充 x 像素,按下时为 0 像素跨度>
  • @WISHI您的答案可能是对的,但它有点太复杂了,因为我正在编写的程序非常简单。另外,我需要在图像被释放后立即恢复正常,而不是在第二次点击之后。
  • @electrocrat 我做到了,没有改变任何东西。

标签: android xml image android-imageview android-drawable


【解决方案1】:

将选择器的内容更新为

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_pressed="true"
    android:drawable="@drawable/b1big" />
<item
    android:drawable="@drawable/b1"/>
</selector>

【讨论】:

  • 试过了,没用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-24
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多