Android:从静止图像制作动画 [英] Android: make animation from still images

查看:20
本文介绍了Android:从静止图像制作动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 drawable 目录中展示了一系列静止图像,总共有 500 多张图像.我需要制作动画(每秒加载大约 20 个图像).我希望它运行顺利,没有内存不足异常.

I've a series of still images and total of more than 500 images presented in drawable directory. I need to make an animation (load about 20 images per second). I want it to run smoothly and with no Out Of Memory Exception.

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

I've idea to do this that images for 2 to 3 seconds (40 to 60 images) should load in memory and displayed and then they should disposed off (release the memory) and then images for next 2 to 3 seconds should load. This technique can prevent Out Of Memory Exception. Its just an idea, I dont know whether its a good idea or not. Please guide me some better idea with some code to go with... If my idea is much better and can work then please tell me some helping code to do that.

阅读回复并按照您的建议做后,我写了一些这样的代码:

After reading replies and doing as you suggest, I've written some code like this:

  <?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>

这是我制作动画的代码:

and here is my code for doing animation:

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();
    }
}

}

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

but its very slow. I've set up the refresh time to 30 milliseconds but actually its not refreshing too fast rather its refresh time is about 1 second. Any suggestion to make it fast to feel like real animation?

谢谢,

推荐答案

好的.这么多天后我遇到的最大问题和最简单的解决方案.我从没想到会这么容易做到... :D

OK. The biggest problem and the easiest solution I got to go with after so many days. I would never expect that it would be so easy to do... :D

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

I've used both handler and timer to achieve with just an image view, no flipper, no animator nothing else at all... Here is my solution:

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

----- main.xml file -----

<?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>

这是我的做法:

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());
        }
    }
}

}

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

Note: I've placed all my images to asset directory.

就这么简单,没什么大不了的...

Its so simple as it can, nothing big to do...

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

I hope it'll be helpful for someone looking to go like this :)

这篇关于Android:从静止图像制作动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆