3

これの主な目的は、最初は暗く、押すとプログレスバーのように時計回りに点灯するアニメーションを循環する「ボタン」を持たせることです。しかし、アニメーションドローアブルがまったく機能していないようです。

res ディレクトリの描画可能なリソース フォルダーにある squareanimation という xml アニメーションがあります。

<?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 を null から変更しません。そして、squareanimation.start() メソッドを実行すると、プログラムがクラッシュします。

私の画像はすべて 128px * 128px の PNG 形式です。すべての画像は drawable-hdpi フォルダーに保存されます。

4

1 に答える 1

0

これを回答として追加したので、私のような同じ間違いをした人は簡単に修正できます。eclipse から android studio に切り替えたばかりなので、その機能の一部がわかりませんでした。簡単に言うと、animationDrawable xml ファイルを作成するときに、ドローアブル リソース フォルダーを右クリックし、[新しいドローアブル リソース ファイルの追加] をクリックします。私はばかげて通常のxmlファイルを作成しましたが、どういうわけか同じ機能が得られませんでした。

于 2014-05-17T09:00:21.990 に答える