【问题标题】:Android button animateAndroid 按钮动画
【发布时间】:2015-12-14 02:03:34
【问题描述】:

我在这里有这些按钮,我希望它们都有一些动画 onclick。 长大一点,然后恢复正常。

这里是按钮的 xml 代码。

<ImageButton
        android:layout_width="165dp"
        android:layout_height="60dp"
        android:text="next"
        android:id="@+id/next"
        android:src="@drawable/next"
        android:background="@android:color/transparent"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/linearLayout"
        android:layout_alignEnd="@+id/linearLayout" />

    <ImageButton
        android:layout_width="170dp"
        android:layout_height="60dp"
        android:text="confirm"
        android:src="@drawable/confirm"
        android:background="@android:color/transparent"
        android:id="@+id/confirm"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
         />

这些按钮位于一个 activity_main.xml 文件中。

有人可以帮我展示一些我应该做些什么来实现它吗?

非常感谢您的宝贵时间。

【问题讨论】:

标签: android android-layout android-studio android-button


【解决方案1】:

使用Animator可以轻松实现目标:

findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Animator scale = ObjectAnimator.ofPropertyValuesHolder(v,
                    PropertyValuesHolder.ofFloat(View.SCALE_X, 1, 1.5f, 1),
                    PropertyValuesHolder.ofFloat(View.SCALE_Y, 1, 1.5f, 1)
                    );
            scale.setDuration(1000);
            scale.start();
        }
    });

【讨论】:

  • 我得到错误(“无法解析符号“objectanimator”和“propertyvaluesholder”)知道我应该为这两个声明什么吗?
  • 你的目标api是什么?如果要使用Animator,目标api应该在11以上。
  • minSdkVersion 14 targetSdkVersion 23
  • 你添加了这样的导入import android.animation.Animator; import android.animation.ObjectAnimator; import android.animation.PropertyValuesHolder;吗?
  • 是的,但是当我尝试运行它时,它会因此错误而崩溃。 java.lang.RuntimeException:无法实例化活动 ComponentInfo{com.example.ioulios.ellak/com.example.ioulios.ellak.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法 'android.view.View android. view.Window.findViewById(int)' 在空对象引用上
【解决方案2】:

在按钮的 onclick 中,我们使用view.startAnimation() 方法启动我们想要使用的动画。我们首先使用AnimationUtils.loadAnimation()加载所需的动画

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        view.startAnimation(AnimationUtils.loadAnimation(context, R.anim.button_click));            
    }
}

现在在 anim 文件夹下创建一个名为 button_click.xml 的文件或任何你想命名的文件。

动画/button_click.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
    android:fromXScale = "1"
    android:toXScale = "0.9"
    android:fromYScale = "1"
    android:toYScale = "0.9"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration = "50">
    </scale>
</set>

【讨论】:

  • drawable文件夹下?
  • 我得到错误(“无法解析符号我的按钮名称“确认”)
  • 嗯。你把你的 button_click 文件放在哪里了?
  • res>anim>button_click ...我声明了一些东西,现在我只有在最后一个“}”中没有错误(预期为','或')')
  • 您似乎遇到了语法错误。你能告诉我你的代码吗?
猜你喜欢
  • 2013-09-18
  • 2017-03-09
  • 2013-02-04
  • 2015-01-27
  • 1970-01-01
  • 1970-01-01
  • 2015-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多