如何在点击按钮或视图后以编程方式放大它



我的应用程序中有一些按钮,我想在单击它们时放大它们

为此,我创建了一个选择器

selector_sms_button.xml

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

并声明我的按钮如下

  <Button
        android:id="@+id/sms"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/selector_sms_button" />

问题是 - 按钮在单击时根本不
会增长ic_sms_big的大小为 300x300dp,ic_sms的大小为 72x72dp

如果我绝对没有必要,我不想为他们创建动画文件。问题是按钮在单击时应该增长到至少 3 倍的大小,而它没有。

我能做些什么来修复它?

编辑:我的请求"我不想创建动画文件"并不意味着我不想为它创建 XML 文件,而是我根本不想在代码中为它创建引用。我有 6 个按钮应该这样做

如果你不想添加文件,你可以以编程方式添加

Animation anim = new ScaleAnimation(
        1f, 1f, // Start and end values for the X axis scaling
        startScale, endScale, // Start and end values for the Y axis scaling
        Animation.RELATIVE_TO_SELF, 0f, // Pivot point of X scaling
        Animation.RELATIVE_TO_SELF, 1f); // Pivot point of Y scaling
anim.setFillAfter(true); // Needed to keep the result of the animation
v.startAnimation(anim);

解决方案是向按钮的较小版本添加填充让它采用与较大图像完全相同的像素数量,但有很多填充,然后单击图像更改并且图标似乎增长

要以编程方式实现它,您可以使用以下代码:

private void animateView() {
    AnimationSet animationSet = new AnimationSet(true);
    ScaleAnimation growAnimation = new ScaleAnimation(1.0f, 3.0f, 1.0f, 3.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    ScaleAnimation shrinkAnimation = new ScaleAnimation(3.0f, 1.0f, 3.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    animationSet.addAnimation(growAnimation);
    animationSet.addAnimation(shrinkAnimation);
    animationSet.setDuration(200);
    viewToAnimate.startAnimation(animationSet);
}

您应该在onClick方法中更改按钮的大小:

button.setLayoutParams (new LayoutParams(width, height));

尝试删除较小按钮上的android:state_pressed="false":

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

您还应该考虑这一点:

  • state_selected当使用键盘/键盘/轨迹球/等
  • state_activated 在调用 View.setActivated (true) 时使用。这用于"持久选择"(例如,请参阅平板电脑上的设置)
  • state_pressed当用户通过触摸或键盘或鼠标
  • 如果
  • 项目被标记为可聚焦,并且它通过键盘/DPaD/轨迹球/等的用户接收焦点,或者如果该项目在触摸模式下可聚焦,则使用state_focused

在按钮上尝试这样的事情

button.animate()
      .setDuration(2000)
      .scaleX(3.0f)
      .scaleY(3.0f);
Animation anim = new ScaleAnimation(
                    1f, 0.7f, // Start and end values for the X axis scaling
                    1f, 0.7f, // Start and end values for the Y axis scaling
                    Animation.RELATIVE_TO_SELF, 0.5f, // Pivot point of X scaling
                    Animation.RELATIVE_TO_SELF, 0.5f); // Pivot point of Y scaling
            anim.setFillAfter(false); // Needed to keep the result of the animation
            anim.setDuration(300);
            iv_logo.startAnimation(anim);

我用这个达到了预期的结果。缩小后将严格位于视图的中心。

最新更新