我想在按钮点击时更改ImageView中的图像。我也在这个imageView上做动画。对于更改图像,我在imageView上使用setImageDrawable()
方法。问题是,有时这种方法不适用于每个图像。有时它会更改图像并为ImageView设置新图像,但有时它不会更改图像并且不会显示任何内容。如果我调试我的代码然后它显示所有图像。我不知道为什么会发生这种情况,因为代码也没有抛出任何错误。我是否因为内存而面临这个问题?
这是我的代码:
public class AnimalsActivity extends Activity {
private Animation animalTranslate;
private ImageView animalImg;
private static int cnt = 0;
private ImageButton btnNext, btnPrev;
};
private int[] animalImages = { R.drawable.antelope, R.drawable.bull,
R.drawable.camel, R.drawable.deer, R.drawable.animal_elephant,
R.drawable.fox, R.drawable.giraffe, R.drawable.animal_horse,
R.drawable.iguana, R.drawable.jaguar, R.drawable.koala,
R.drawable.animal_lion, R.drawable.monkey, R.drawable.nyala,
R.drawable.orca, R.drawable.panda, R.drawable.quoll,
R.drawable.animal_rabbit, R.drawable.snake, R.drawable.tiger,
R.drawable.urial, R.drawable.vole, R.drawable.wolf,
R.drawable.xerus, R.drawable.yak, R.drawable.animal_zebra };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.animal_screen);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
btnNext = (ImageButton) findViewById(R.id.btn_animalNext);
btnPrev = (ImageButton) findViewById(R.id.btn_animalPrev);
animalImg = (ImageView) findViewById(R.id.animalImage);
StartAnimations(cnt);
btnNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cnt++;
if (cnt > 25) {
cnt = 0;
}
StartAnimations(cnt);
}
});
btnPrev.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cnt--;
if (cnt < 0) {
cnt = 25;
}
StartAnimations(cnt);
}
});
}
private void StartAnimations(int count) {
animalImg.setImageResource(animalImages[cnt]);
animalTranslate = AnimationUtils.loadAnimation(AnimalsActivity.this,
R.drawable.letters_translate);
animalImg.startAnimation(animalTranslate);
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
cnt = 0;
Intent readActivity = new Intent(AnimalsActivity.this,
HomeScreenActivity.class);
readActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(readActivity);
}
}
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lyt_animal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/forest"
android:orientation="vertical" >
<ImageView
android:id="@+id/animalImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/txt_animal"
android:layout_centerHorizontal="true"
android:scaleType="fitXY" />
<ImageButton
android:id="@+id/btn_animalPrev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@drawable/animal_prev" />
<ImageButton
android:id="@+id/btn_animalNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/animal_next" />
</RelativeLayout>
答案 0 :(得分:0)
从onCreate开始动画不是一个好方法。 Read this, It may help you.
答案 1 :(得分:-3)
我编辑了我的代码,现在它正在运行。我想,错误是由于内存不足,因为我在模拟器中测试了我的应用程序。我在设备中测试了应用程序并且它运行良好。