2011-09-01 79 views
1

我開發了一個具有逐幀動畫的應用程序。我從可繪製文件夾獲取資源。所以我的apk的大小是巨大的。現在我想從服務器獲取資源,但我無法想出這樣做的想法。 我已經把我的代碼在這裏從動畫服務器獲取資源

的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" 
> 
<ImageView android:id="@+id/simple_anim" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:gravity="center" 
android:layout_centerHorizontal="true" 
/> 
<TextView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Hello World, XMLAnimation" 
/> 
<Button android:text="Button" 
android:id="@+id/button1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"></Button> 
</LinearLayout> 

frame_animation_girl.xml ::

<animation-list xmlns:android= 
"http://schemas.android.com/apk/res/android" 
android:oneshot="false"> 
     <item android:drawable="@drawable/girl0001" android:duration="20" /> 
     <item android:drawable="@drawable/girl0002" android:duration="20" /> 
     <item android:drawable="@drawable/girl0003" android:duration="20" /> 
     <item android:drawable="@drawable/girl0004" android:duration="20" /> 
     <item android:drawable="@drawable/girl0005" android:duration="20" /> 
     <item android:drawable="@drawable/girl0006" android:duration="20" /> 
    </animation-list> 

的Java文件::

 ImageView img = (ImageView) findViewById(R.id.simple_anim); 
     img.setBackgroundResource(R.drawable.frame_animation_girl); 

     MyAnimationRoutine mar = new MyAnimationRoutine(); 
     MyAnimationRoutine2 mar2 = new MyAnimationRoutine2(); 

     Timer t = new Timer(false); 
     t.schedule(mar, 100); 
     Timer t2 = new Timer(false); 
     t2.schedule(mar2, 5000); 
     Buttona = (Button) findViewById(R.id.button1); 
     final Intent animationIntent = new Intent(this, TranningIntent.class); 
     Buttona.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       startActivity(animationIntent); 
       finish(); 
      } 
     }); 
    } 

    class MyAnimationRoutine extends TimerTask { 
     MyAnimationRoutine() { 
     } 

     public void run() { 
      ImageView img = (ImageView) findViewById(R.id.simple_anim); 
      // Get the background, which has been compiled to an 
      // AnimationDrawable object. 
      AnimationDrawable frameAnimation = (AnimationDrawable) img 
        .getBackground(); 

      // Start the animation (looped playback by default). 
      frameAnimation.start(); 
     } 
    } 

    class MyAnimationRoutine2 extends TimerTask { 
     MyAnimationRoutine2() { 
     } 

     public void run() { 
      ImageView img = (ImageView) findViewById(R.id.simple_anim); 
      // Get the background, which has been compiled to an 
      // AnimationDrawable object. 
      AnimationDrawable frameAnimation = (AnimationDrawable) img 
        .getBackground(); 

      // stop the animation (looped playback by default). 
      /* frameAnimation.stop(); */ 
     } 
    } 
} 

回答

1

我認爲這取決於你想要做什麼,以及你的動畫是什麼。一些簡單的動畫(例如旋轉)可以通過動畫繪製單個drawable來實現,可能允許您存儲更少的資源。如果你正在處理非常複雜的動畫和潛在的許多資產,你總是可以通過編程方式(不是通過xml)從你的服務器返回可以被客戶端邏輯解釋爲動畫的數據。

也有在這個線程使用XML文件在運行時的一些討論: Download and replace Android resource files

0

我有::

ImageView img = (ImageView) findViewById(R.id.simple_anim); 
     animation = new AnimationDrawable(); 

      try { 
       for(int i=0;i<54;i++) 
       {  
       xyz("girl000",i); 
       }   
      animation.setOneShot(false); 
      } catch (Exception e) { 
      } 
      img.setBackgroundDrawable(animation); 
      img.post(new Starter()); 

    } 


    public void xyz(String str,int x) 
    { 
     try { 
      Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
      "http://sdfsdffff/MRESC/images/test/girl/"+"girl000"+x+".png") 
      .getContent()); 
      Drawable frame =new BitmapDrawable(bitmap); 
      animation.addFrame(frame, 50); 

     } catch (Exception e) { 

     } 

    } 
    class Starter implements Runnable { 

     public void run() { 
      animation.start();   
     } 


    } 
解決這個