Android 点击按钮或视图后,如何通过编程放大按钮或视图

Android 点击按钮或视图后,如何通过编程放大按钮或视图,android,button,android-animation,Android,Button,Android Animation,我的应用程序中有一些按钮,我想在单击时放大它们 为此,我创建了一个选择器 选择器\u sms\u 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_p

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

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

选择器\u sms\u 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个按钮可以执行此操作

尝试删除较小按钮上的安卓: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>

你也应该考虑这个问题:

  • 当使用 键盘/dpad/轨迹球/等
  • 调用View.setActivated(true)时使用状态_activated。这用于“持久选择”(例如,请参见平板电脑上的设置)
  • 当用户通过触摸或按键按下项目时,使用状态_pressed 键盘还是鼠标
  • 如果项目标记为可聚焦,并通过键盘/dpad/轨迹球等用户接收焦点,或者项目在触摸模式下可聚焦,则使用状态聚焦

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

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);

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

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

在你的按钮上试试这样的东西

button.animate()
      .setDuration(2000)
      .scaleX(3.0f)
      .scaleY(3.0f);

解决方案是在较小版本的按钮上添加填充
让它采用与较大图像完全相同的像素量,但有大量的填充,然后单击图像更改,图标显示为增长

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

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);
}
我已经用这个达到了预期的效果。
缩小后的视图将严格位于视图的中心。

是否为裁剪按钮的父视图?是否需要对其调用
invalidate()
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);