750架透明的PNG动画的ImageView在23fps高 [英] 750 frame transparent PNG animation in ImageView at 23fps

查看:142
本文介绍了750架透明的PNG动画的ImageView在23fps高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动有两个ImageViews设置为全屏幕320×480。

I have an Activity with two ImageViews set to full screen 320 x 480.

我要播放动画,但用完VM之前无法获得超过50使用AnimateDrawable和一个XML数组玩了。

I need to play an animation but couldn't get more than 50 to play using AnimateDrawable and an xml array before running out of VM.

我想转换PNG图像与一个循环,这样我可以在沿途做点其他的东西。
例如振动在帧120中,180安培; 250等,并在框400再踢动画。

I'm trying to switch PNGs with a Loop so I can do other stuff at points along the way. For example vibrate at frames 120, 180 & 250 etc. and play another animation at frame 400.

这code以下的工作,但在ImageView的需要180之间 - 280ms到模拟器更新。所以,我需要设置的Thread.Sleep大于280-300否则我开始丢失帧。我需要一种更快的方式来更新ImageView的。是否有一个更快的部件可用?

This code below works but the ImageView takes between 180 - 280ms to update on the emulator. So I need to set thread.sleep greater than 280-300 otherwise I start missing frames. I need a quicker way to update the imageview. Is there a quicker widget available?

对于一些帧的图像并不需要改变或者是空白的,而这样的动画持续746帧我只真正需要他们的245。因此,我已删除从可绘制文件夹,并在他们的code跳过不改变或空白帧未找到时。这减少了尺寸下降到9MB总数的所有图像。

The image for some frames do not need to change or are blank so while the animation lasts for 746 frames I only actually need 245 of them. So I have deleted the non changing or blank frames from the drawables folder and the code skips over them when not found. This has reduced the size down to 9mb total for all the images.

这是我的第一个Android应用程序很抱歉,如果我不这样做是正确的方式。我将如何改善任何意见。 : - )

This is my first Android app so sorry if I am not doing it the right way. I will take any advice on how to improve. :-)

任何人都可以想出一个更好的方式来做到这一点?帮帮我!! (这个相同的逻辑工作正常在iPhone上)

Can anyone think of a better way to do this? HELP!! (This same logic works fine on the 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");     
         }
   };

}

推荐答案

现在我已经能够得到的帧速率可达减少每个图像的尺寸(在动画中使用)下降到接近20帧每秒最小的垂直条以适合对象。

I have now been able to get the frame rate up to close to 20 frames a second by reducing the size of each image (used in the animation) down to the smallest vertical strip to fit the object.

我阅读有关使用Spritesheets游戏动画,他们四处移动单个图像,并通过一个小窗口中查看所需要的帧后,想出了这个主意。精灵不是practicle我的目的,太多的框架精灵表是135MB。

I came up with this idea after reading about using Spritesheets for game animation where they move a single image around and view the needed frame through a small window. Sprites were not practicle for my purpose, too many frames Sprite sheet would be 135mb.

图像现在只有480×150(31-54KB之间的每一个),而不是一个完整的画面是。 ImageView的高度设置为FILL_PARENT和ImageView的宽度为WRAP_CONTENT所以还是以不同的屏幕大小自动缩放。

The images are now only 480 x 150 (between 31-54KB each) instead of a full screen. ImageView height is set to fill_parent and ImageView width set to wrap_content so it still scales automatically to different screen sizes.

我发现,现在的文件加载更快更小,现在已经接近可接受的动画帧速率。希望这是帮助别人谁是试图同样的事情。

I found that now the files are much smaller it loads quicker and now is close to an acceptable animation frame rate. Hope this is of help to anyone else who is trying the same thing.

这篇关于750架透明的PNG动画的ImageView在23fps高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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