我的主要目标是让它有一个“按钮”,最初是暗的,当按下时,会像进度条一样顺时针循环地亮起来。但是我似乎无法使animationdrawable正常工作。
我的xml动画名为squareanimation,位于res目录中的drawable资源文件夹中。
我在布局中有一个ImageView。
那么我有我的活动
我的xml动画名为squareanimation,位于res目录中的drawable资源文件夹中。
<?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()方法,它会导致程序崩溃。
我的所有图像都在PNG格式中,大小为128px * 128px,如果有任何变化,所有的图片都存储在drawable-hdpi文件夹中。