3

我的主要目标是让它有一个最初是黑暗的“按钮”,当按下它时,它会循环播放它的动画,就像进度条一样顺时针方向点亮。但我似乎根本无法让动画绘制工作。

我的 xml 动画名为 squareanimation,位于 res 目录中的可绘制资源文件夹中

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">

    <item android:drawable="@drawable/square" android:duration="210" />
    <item android:drawable="@drawable/square1" android:duration="210" />

</animation-list>

我的布局中有我的 ImageView

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.soundboard.sounds.MainActivity"
    android:background="@drawable/app_background">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Cowbell"
            android:id="@+id/textView"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_centerInParent="true"
            android:gravity="center"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageHost"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="beep"
            android:id="@+id/beepButton"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="@drawable/square" />

        </TableLayout>

</RelativeLayout>

然后我有我的活动

public class MainActivity extends ActionBarActivity implements View.OnClickListener {
    AnimationDrawable squareanimation;
    Button beepButton;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            beepButton = (Button) findViewById(R.id.beepButton);

            ImageView square = (ImageView) findViewById(R.id.imageHost);
            square.setBackgroundResource(R.drawable.squareanimation);
            squareanimation = (AnimationDrawable) square.getBackground();
    }


        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                if(squareanimation == null)beepButton.setText("Not working");
                return true;
            }
            return super.onTouchEvent(event);
        }

这些只是我的代码片段

每次我运行我的程序时,无论我清理或重建多少次,程序都不会改变 squareanimation 为空。如果我运行 squareanimation.start() 方法,它会使程序崩溃。

我的图像都是 128 像素 * 128 像素的 PNG 格式,如果这有任何改变的话。我所有的图像都存储在 drawable-hdpi 文件夹中。

4

1 回答 1

0

我添加了这个作为答案,所以像我这样犯同样错误的人可以找到一个简单的解决方法。我刚从 eclipse 切换到 android studio,所以我不知道它的一些功能。简而言之,当创建 animationDrawable xml 文件时 - 右键单击​​您的可绘制资源文件夹,然后单击添加新的可绘制资源文件。我愚蠢地创建了一个普通的 xml 文件,但不知何故它没有获得相同的功能。

于 2014-05-17T09:00:21.990 回答