【发布时间】:2012-11-11 18:16:39
【问题描述】:
我有两个 ImageView 设置为全屏 320 x 480 的 Activity。
我需要播放动画,但在用完 VM 之前,使用 AnimateDrawable 和 xml 数组播放的动画不能超过 50 个。
我正在尝试使用循环切换 PNG,这样我就可以在沿途的各个点做其他事情。 例如在第 120、180 和 250 帧等处振动,并在第 400 帧播放另一个动画。
以下代码有效,但 ImageView 在模拟器上更新需要 180 - 280 毫秒。所以我需要将 thread.sleep 设置为大于 280-300,否则我会开始丢失帧。我需要一种更快的方法来更新 imageview。有更快的小部件可用吗?
某些帧的图像不需要更改或为空白,因此当动画持续 746 帧时,我实际上只需要其中的 245 帧。所以我从drawables文件夹中删除了不变的或空白的框架,当找不到时代码会跳过它们。这已将所有图像的总大小减少到 9mb。
这是我的第一个 Android 应用程序,如果我没有以正确的方式进行操作,非常抱歉。我会就如何改进提出任何建议。 :-)
谁能想到更好的方法来做到这一点?帮助!! (同样的逻辑在 iPhone 上运行良好)
activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="@drawable/imgbackground"
android:visibility="visible" />
<ImageView
android:id="@+id/animLayer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:visibility="visible" />
</RelativeLayout>
MainActivity.java
package com.test.pngloop
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final String TAG = null;
public int iFrameCount = 0;
public ImageView myIV;
public String myString;
long startTime;
long endTime;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//define the IV here so it's done only once
myIV = (ImageView) findViewById(R.id.animLayer);
}
@Override
protected void onStart()
{
super.onStart();
Thread thr1 = new Thread(r1);
thr1.start();
}
Runnable r1 = new Runnable()
{
public void run()
{
while (iFrameCount< 747)
{
runOnUiThread(new Runnable()
{
public void run()
{
iFrameCount++;
String image="image" + iFrameCount;
int resID = getResources().getIdentifier(image, "drawable", getPackageName());
if (resID != 0) //if there is no image/change for this frame skip it
{
myString="iFrameCount: " + iFrameCount;
Log.d(TAG, myString);
//speed the same between setImageResource & setBackgroundResource
//myIV.setImageResource(resID);
//myIV.postInvalidate();
startTime = System.currentTimeMillis();
myIV.setBackgroundResource(resID);
endTime = System.currentTimeMillis();
System.out.println("setBackground took: " + (endTime - startTime) + " milliseconds");
}
else
{ //we can skip frames 1-119, 209-251, 272-322, 416-472 & 554-745 (as no change from previous frame)
myString="File skipped: " + iFrameCount;
Log.d(TAG, myString);
}
}
});
try
{
Thread.sleep(300);
}
catch (InterruptedException iex) {}
}
Log.d(TAG, "Finished playing all frames");
}
};
}
【问题讨论】:
-
更新: 我已经能够在实际的 Android 设备(LG Optimus L5 480x320 屏幕)上将速度提高到大约 13fps(70ms 循环)。我只能从模拟器中获得 5fps(200ms 循环)而不会丢失帧。有没有人找到更快播放大型动画的解决方案?如何将 Animation.drawables 拆分成小块并一个接一个地播放?有人成功了吗?
-
更新 2: 我现在已经能够通过将每个 iamge 的大小减小到最小的水平条来将帧速率提高到接近每秒 20 帧适合对象。图像现在只有 480 x 150 而不是全屏。 ImageView 高度设置为 fill_parent 和 ImageView 宽度设置为 wrap_content 所以我仍然可以缩放。我想文件更小加载更快我从其他帖子中得到了关于使用精灵进行游戏动画的想法。 .希望这对其他人有帮助。
标签: android eclipse animation png imageview