使用对象动画师制作按钮动画

Animating button with object animator

我只想为我的按钮设置动画。

我得到:

W/PropertyValuesHolder: Method setAlpha() with type int not found on target class class android.support.v7.widget.AppCompatButton

Mainactivity.java:

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ObjectAnimator objectAnimator;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView mListView= (ListView) findViewById(R.id.listView);
        Button button= (Button) findViewById(R.id.button);
        objectAnimator=ObjectAnimator.ofInt(button,"alpha",0,1).setDuration(1000);
        objectAnimator.setTarget(button);
        objectAnimator.start();


        adapter myAdapter = new adapter(this);
        AlphaInAnimationAdapter animationAdapter = new AlphaInAnimationAdapter(myAdapter);
        animationAdapter.setAbsListView(mListView);
        mListView.setAdapter(animationAdapter);
    }

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="autogenie.mp.MainActivity">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:id="@+id/listView"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp" />
</RelativeLayout>

为什么没有动画?

objectanimator class 的方法 ofint()属性 名称 参数下是什么?

alpha 属性 的类型是 float。您正在尝试使用 int 值对其进行动画处理。对象动画师试图在 View class 中找到 setAlpha(int) 方法,但无法找到 - 因此出现异常。

您需要使用 ObjectAnimator.ofFloat() 或更简单的 button.animate().alpha() 变体。我个人会推荐第二个选项 - 它更简洁并且删除了一些样板代码。