我正在开发一个安卓游戏。它几乎完成了,并在 10.1 平板电脑、nexus 7、nexus 4、HTC senstation 上进行了测试,它适用于所有这些平板电脑。但是,当我在 Galaxy s3 上尝试它时,当我在帧动画 xml 中调用图片时出现 OutofMemory 错误。现在我通过调整 xhdpi 文件夹中图片的大小解决了这个问题,但结果并不令人满意。是否有任何足够的方法可以用来重新缩放动画中显示的图片以进行帧动画。
这是代码:
public class Logo extends Activity {
private ImageView iv;
Thread timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_logo);
iv = (ImageView) findViewById(R.id.imageView1);
iv.setBackgroundResource(R.anim.anime);
// thread for animating the logo and moving to next activity after
// display.
timer = new Thread() {
@Override
public void run() {
try {
sleep(7000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
//Intent i = new Intent();
//i.setClass(Logo.this, StartPoint.class);
//startActivity(i);
}
}
};
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
AnimationDrawable anime = (AnimationDrawable) iv.getBackground();
if (hasFocus) {
anime.start();
timer.start();
} else {
anime.stop();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.logo, menu);
return true;
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
finish(); // to remove this activity.
}
}
这是anime.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/a1" android:duration="60" />
<item android:drawable="@drawable/a2" android:duration="60" />
<item android:drawable="@drawable/a3" android:duration="60" />
<item android:drawable="@drawable/a4" android:duration="60" />
<item android:drawable="@drawable/a5" android:duration="60" />
<item android:drawable="@drawable/a6" android:duration="60" />
<item android:drawable="@drawable/a7" android:duration="60" />
<item android:drawable="@drawable/a8" android:duration="60" />
<item android:drawable="@drawable/a9" android:duration="60" />
<item android:drawable="@drawable/a10" android:duration="60" />
<item android:drawable="@drawable/a11" android:duration="60" />
<item android:drawable="@drawable/a12" android:duration="60" />
<item android:drawable="@drawable/a13" android:duration="60" />
<item android:drawable="@drawable/a14" android:duration="60" />
<item android:drawable="@drawable/a15" android:duration="60" />
<item android:drawable="@drawable/a16" android:duration="60" />
<item android:drawable="@drawable/a17" android:duration="60" />
<item android:drawable="@drawable/a18" android:duration="60" />
<item android:drawable="@drawable/a19" android:duration="60" />
<item android:drawable="@drawable/a20" android:duration="60" />
<item android:drawable="@drawable/a21" android:duration="60" />
<item android:drawable="@drawable/a22" android:duration="60" />
<item android:drawable="@drawable/a23" android:duration="60" />
<item android:drawable="@drawable/a24" android:duration="60" />
<item android:drawable="@drawable/a25" android:duration="60" />
<item android:drawable="@drawable/a26" android:duration="60" />
<item android:drawable="@drawable/a27" android:duration="60" />
</animation-list>
现在我看到了许多说明 bitmapFactory.options 的答案,但是我如何将这些选项应用于 xml 文件以及是否可能。有没有其他我可以使用的方法来代替调整图片大小。先感谢您。
在这里,我尝试以编程方式应用代码,但仍然遇到相同的错误:
//onCreate and thread then
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
BitmapDrawable frame1 = (BitmapDrawable)getResources().getDrawable(R.drawable.a1);
BitmapDrawable frame2 = (BitmapDrawable)getResources().getDrawable(R.drawable.a2);
BitmapDrawable frame3 = (BitmapDrawable)getResources().getDrawable(R.drawable.a3);
BitmapDrawable frame4 = (BitmapDrawable)getResources().getDrawable(R.drawable.a4);
BitmapDrawable frame5 = (BitmapDrawable)getResources().getDrawable(R.drawable.a5);
BitmapDrawable frame6 = (BitmapDrawable)getResources().getDrawable(R.drawable.a6);
BitmapDrawable frame7 = (BitmapDrawable)getResources().getDrawable(R.drawable.a7);
BitmapDrawable frame8 = (BitmapDrawable)getResources().getDrawable(R.drawable.a8);
BitmapDrawable frame9 = (BitmapDrawable)getResources().getDrawable(R.drawable.a9);
BitmapDrawable frame10 = (BitmapDrawable)getResources().getDrawable(R.drawable.a10);
BitmapDrawable frame11 = (BitmapDrawable)getResources().getDrawable(R.drawable.a11);
BitmapDrawable frame12 = (BitmapDrawable)getResources().getDrawable(R.drawable.a12);
BitmapDrawable frame13 = (BitmapDrawable)getResources().getDrawable(R.drawable.a13);
BitmapDrawable frame14 = (BitmapDrawable)getResources().getDrawable(R.drawable.a14);
BitmapDrawable frame15 = (BitmapDrawable)getResources().getDrawable(R.drawable.a15);
BitmapDrawable frame16 = (BitmapDrawable)getResources().getDrawable(R.drawable.a16);
BitmapDrawable frame17 = (BitmapDrawable)getResources().getDrawable(R.drawable.a17);
BitmapDrawable frame18 = (BitmapDrawable)getResources().getDrawable(R.drawable.a18);
BitmapDrawable frame19 = (BitmapDrawable)getResources().getDrawable(R.drawable.a19);
BitmapDrawable frame20 = (BitmapDrawable)getResources().getDrawable(R.drawable.a20);
BitmapDrawable frame21 = (BitmapDrawable)getResources().getDrawable(R.drawable.a21);
BitmapDrawable frame22 = (BitmapDrawable)getResources().getDrawable(R.drawable.a22);
BitmapDrawable frame23 = (BitmapDrawable)getResources().getDrawable(R.drawable.a23);
BitmapDrawable frame24 = (BitmapDrawable)getResources().getDrawable(R.drawable.a24);
BitmapDrawable frame25 = (BitmapDrawable)getResources().getDrawable(R.drawable.a25);
BitmapDrawable frame26 = (BitmapDrawable)getResources().getDrawable(R.drawable.a26);
BitmapDrawable frame27 = (BitmapDrawable)getResources().getDrawable(R.drawable.a27);
final AnimationDrawable anime = new AnimationDrawable();
anime.setOneShot(true);
anime.addFrame(frame1, 60);
anime.addFrame(frame2, 60);
anime.addFrame(frame3, 60);
anime.addFrame(frame4, 60);
anime.addFrame(frame5, 60);
anime.addFrame(frame6, 60);
anime.addFrame(frame7, 60);
anime.addFrame(frame8, 60);
anime.addFrame(frame9, 60);
anime.addFrame(frame10, 60);
anime.addFrame(frame11, 60);
anime.addFrame(frame12, 60);
anime.addFrame(frame13, 60);
anime.addFrame(frame14, 60);
anime.addFrame(frame15, 60);
anime.addFrame(frame16, 60);
anime.addFrame(frame17, 60);
anime.addFrame(frame18, 60);
anime.addFrame(frame19, 60);
anime.addFrame(frame20, 60);
anime.addFrame(frame21, 60);
anime.addFrame(frame22, 60);
anime.addFrame(frame23, 60);
anime.addFrame(frame24, 60);
anime.addFrame(frame25, 60);
anime.addFrame(frame26, 60);
anime.addFrame(frame27, 60);
iv.setBackgroundDrawable(anime);
if (hasFocus) {
anime.start();
timer.start();
}
else
anime.stop();
}