Android:从静止图像制作动画

Mud*_*san 14 animation android image

我有一系列静态图像,总共有超过500个图像呈现在drawable目录中.我需要制作一个动画(每秒加载大约20个图像).我希望它能够顺利运行并且没有内存异常.

我想这样做,2至3秒(40到60张图像)的图像应该加载到内存中并显示,然后它们应该关闭(释放内存),然后加载下2到3秒的图像.此技术可以防止内存不足异常.它只是一个想法,我不知道它是否是一个好主意.请引导我一些更好的想法与一些代码一起...如果我的想法更好,可以工作,那么请告诉我一些帮助代码来做到这一点.

在阅读回复并按照您的建议进行操作后,我编写了一些代码,如下所示:

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/llMain">

<ViewFlipper android:id="@+id/imageflipper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<ImageView android:id="@+id/ImageView01"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"        
        android:scaleType="centerInside"        
        android:layout_gravity="center" />
    </ViewFlipper>
    </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这是我做动画的代码:

public class Animation extends Activity {
ViewFlipper flipper;
int myIndex = 216;
private final Handler handler = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    flipper=(ViewFlipper)findViewById(R.id.imageflipper); 
    doTheAutoRefresh();
        //displayData();
}

private void doTheAutoRefresh() {
    handler.postDelayed(new Runnable() {
        public void run(){
            displayData(); // this is where you put your refresh code
            doTheAutoRefresh();
             }
         }, 30);

}

private void displayData()
{
    Resources r = getResources();
    if(myIndex > 230){
        myIndex = 216;
        ImageView myImg = (ImageView)findViewById(R.id.ImageView01);
        myImg.setImageResource(r.getIdentifier("drum0" + myIndex, "drawable", "com.vt.animation"));

        myIndex += 1;
        flipper.showNext();
    }
    else{
        ImageView myImg = (ImageView)findViewById(R.id.ImageView01);

        myImg.setImageResource(r.getIdentifier("drum0" + myIndex, "drawable", "com.vt.animation"));

        myIndex += 1;
        flipper.showNext();
    }
}
Run Code Online (Sandbox Code Playgroud)

}

但它很慢.我已经将刷新时间设置为30毫秒,但实际上它的刷新时间不是太快,而是刷新时间大约为1秒.有什么建议让它快速感觉像真正的动画?

谢谢,

oli*_*erg 36

使用FrameAnimation,例如res/drawable/movie.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/frame1" android:duration="50" />
    <item android:drawable="@drawable/frame2" android:duration="50" />
    <item android:drawable="@drawable/frame3" android:duration="50" />
    etc...
</animation-list>
Run Code Online (Sandbox Code Playgroud)

然后在Java中:

imageView.setBackgroundResource(R.drawable.movie);
AnimationDrawable anim = (AnimationDrawable) imageView.getBackground();
anim.start();
Run Code Online (Sandbox Code Playgroud)

  • 这导致内存不足错误,因为它看起来像是将它们全部加载到内存中. (5认同)

Mud*_*san 20

好.经过这么多天后,我遇到的最大问题和最简单的解决方案.我永远不会想到它会那么容易......:D

我已经使用处理程序和计时器来实现只有一个图像视图,没有鳍状肢,没有动画师没有别的......这是我的解决方案:

----- main.xml文件-----

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background">

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageView1">
</ImageView>
Run Code Online (Sandbox Code Playgroud)

这就是我做的方式:

public class MainActivity extends Activity {
private ImageView _imagView;
private Timer _timer;
private int _index;
private MyHandler handler;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    handler= new MyHandler();
    _imagView=(ImageView) findViewById(R.id.imageView1);

    _index=0;
    _timer= new Timer();
    _timer.schedule(new TickClass(), 500, 200);
}

private class TickClass extends TimerTask
{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        handler.sendEmptyMessage(_index);
        _index++;
    }
}

private class MyHandler extends Handler
{
    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        super.handleMessage(msg);

        try {
                Bitmap bmp= BitmapFactory.decodeStream(MainActivity.this.getAssets().open("drum_"+_index+".png"));
                _imagView.setImageBitmap(bmp);

                Log.v("Loaing Image: ",_index+"");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.v("Exception in Handler ",e.getMessage());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}

注意: 我已将所有图片放到资产目录中.

它尽可能简单,没什么大不了的......

我希望对那些想要这样的人有所帮助:)