Android:从静止图像制作动画

时间:2011-04-10 10:20:22

标签: android image animation

我有一系列静止图像,总共有超过500张图像呈现在可绘制目录中。我需要制作一个动画(每秒加载大约20个图像)。我希望它能够顺利运行并且没有Out of Memory Exception。

我想这样做,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>

这是我做动画的代码:

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秒。有什么建议让它快速感觉像是真正的动画吗?

谢谢,

4 个答案:

答案 0 :(得分: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>

然后在Java中:

imageView.setBackgroundResource(R.drawable.movie);
AnimationDrawable anim = (AnimationDrawable) imageView.getBackground();
anim.start();

答案 1 :(得分:19)

行。经过这么多天后,我遇到的最大问题和最简单的解决方案。我永远不会想到它会那么容易......: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>

这就是我做的方式:

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

}

注意:我已将所有图片放入资产目录。

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

我希望它能帮助那些想要这样做的人:)

答案 2 :(得分:2)

加载几张图片非常昂贵。

我认为加载包含某个动画的所有动作的单个图像会更好。动画片。

private Bitmap animation = BitmapFactory.decodeResource(getResources(), R.drawable.myPng);

想法是遍历位图。

答案 3 :(得分:1)

好吧,我正在使用viewFlipper,在视图之间切换。关于这个问题的好处是你可以看到之前的图片在新图片滑入时滑出。

内部图像显示方法:

    if (direction == NEXT) {
        viewFlipper.setInAnimation(slideLeftIn);
        viewFlipper.setOutAnimation(slideLeftOut);

        if (currImg < max)
            currImg++;
        if (currImg == max)
            currImg = 0;

        if (currentView == 0) {
            currentView = 1;
            ImageView iv = (ImageView) findViewById(R.id.ImageView02);
            iv.setImageResource(images[currImg]);
        } else if (currentView == 1) {
            currentView = 2;
            ImageView iv = (ImageView) findViewById(R.id.ImageView03);
            iv.setImageResource(images[currImg]);
        } else {
            currentView = 0;
            ImageView iv = (ImageView) findViewById(R.id.ImageView01);
            iv.setImageResource(images[currImg]);
        }
        viewFlipper.showNext();
    }
    else if (direction == PREV) {
        viewFlipper.setInAnimation(slideRightIn);
        viewFlipper.setOutAnimation(slideRightOut);

        if (currImg > 0)
            currImg--;
        else if (currImg <= 0)
            currImg = (max-1);

        if (currentView == 0) {
            currentView = 2;
            ImageView iv = (ImageView) findViewById(R.id.ImageView03);
            iv.setImageResource(images[currImg]);
        } else if (currentView == 2) {
            currentView = 1;
            ImageView iv = (ImageView) findViewById(R.id.ImageView02);
            iv.setImageResource(images[currImg]);
        } else {
            currentView = 0;
            ImageView iv = (ImageView) findViewById(R.id.ImageView01);
            iv.setImageResource(images[currImg]);
        }
        viewFlipper.showPrevious();

在XML文件中:

        <ViewFlipper android:id="@+id/imageflipper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ImageView android:id="@+id/ImageView01"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"        
            android:scaleType="centerInside"        
            android:layout_gravity="center" />

        <ImageView android:id="@+id/ImageView02"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"        
            android:scaleType="centerInside"        
            android:layout_gravity="center" />

        <ImageView android:id="@+id/ImageView03"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"        
            android:scaleType="centerInside"        
            android:layout_gravity="center" />

        </ViewFlipper>