Android Studio中的动画太慢

时间:2020-06-29 03:47:25

标签: java android android-studio

我想在我的Android应用中显示由127个png文件组成的动画。但是,我无法让它们像我想要的那样快地播放。如下所示,我创建了一个message Metric { bytes key = 1; double value = 2; } repeated Metric metrics = 2; 对象,并向其中添加了png文件作为框架。我打电话给AnimationDrawable,所以整个动画应该播放127毫秒(127帧x 1毫秒),但是在我的手机上,播放时间超过一秒钟。通过对代码进行性能分析,我发现animation.addFrame(frame, 1)有助于大多数内存使用,并且我尝试通过进一步调整图像大小来解决此问题。但是,动画的速度仍然远远不能达到127毫秒。最好的行动方针是什么?

byte[]

配置文件代码。调整png文件的大小会减少void createAnimation(AnimationDrawable animation, int endFrameIdx){ for (int i = 0; i < endFrameIdx; i++) { String name = idArray[i]; // idArray contains name of png files int id = getResources().getIdentifier(name, "drawable", getPackageName()); Drawable d = ContextCompat.getDrawable(this, id); Drawable compressD = resize(d); animation.addFrame(compressD, 1);} // 1 ms per frame } } private Drawable resize(Drawable image) { Bitmap b = ((BitmapDrawable)image).getBitmap(); Bitmap bitmapResized = Bitmap.createScaledBitmap(b, 90, 161, true); return new BitmapDrawable(getResources(), bitmapResized); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageanim = findViewById(R.id.imageanim); AnimationDrawable animation = new AnimationDrawable(); int stopFrameIdx = 270; // maximum # of frames to add to animation createAnimation(animation, stopFrameIdx); imageanim.setImageDrawable(animation); animation.start(); 占用的内存,但是动画播放仍然很慢。

enter image description here

1 个答案:

答案 0 :(得分:0)

在尝试优化代码并没有看到重大改进之后,我目前的最佳猜测是动画受手机显示刷新率(60 hz)的限制。尽管我很困惑,为什么android支持以较小的时间间隔显示帧的方法。