2013-10-31 92 views
1

我想在我的应用程序中使用gif动画。如何在android中使用gif动画?

我该怎么办呢?我已经看到this tutorial

但我没有得到任何具体的解决办法,请大家帮我,如何向我们的gif在我的代码。

Xml代码

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:layout_height="match_parent" 
<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:textColor="#ACC437" 
    android:textStyle="bold" 
    android:text="WelCome To This Blog" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<com.androidqa.AnimationView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    /> 
</LinearLayout> 

视图类

public class AnimationView extends View { 
private Movie mMovie; 
private long mMovieStart; 
private static final boolean DECODE_STREAM = true; 
private static byte[] streamToBytes(InputStream is) { 
ByteArrayOutputStream os = new ByteArrayOutputStream(1024); 
byte[] buffer = new byte[1024]; 
int len; 
try { 
    while ((len = is.read(buffer)) >= 0) { 
    os.write(buffer, 0, len); 
     } 
    } catch (java.io.IOException e) { 
    } 
     return os.toByteArray(); 
     } 
    public AnimationView(Context context,AttributeSet attrs) { 
     super(context,attrs); 
    setFocusable(true); 
    java.io.InputStream is; 
    // YOUR GIF IMAGE Here 
    is = context.getResources().openRawResource(R.drawable.th_welcome); 
    if (DECODE_STREAM) { 
    mMovie = Movie.decodeStream(is); 
     } else { 
     byte[] array = streamToBytes(is); 
    mMovie = Movie.decodeByteArray(array, 0, array.length); 
     } 
      } 
     @Override 
     public void onDraw(Canvas canvas) { 
     long now = android.os.SystemClock.uptimeMillis(); 
     if (mMovieStart == 0) { // first time 
     mMovieStart = now; 
      } 
     if (mMovie != null) { 
     int dur = mMovie.duration(); 
     if (dur == 0) { 
     dur = 3000; 
     } 
     int relTime = (int) ((now - mMovieStart) % dur); 
     Log.d("", "real time :: " +relTime); 
     mMovie.setTime(relTime); 
     mMovie.draw(canvas, getWidth() - 200, getHeight()-200); 
     invalidate(); 
      } 
      } 
       } 

请建议我要这个任何解决方案.... :)

+0

你有什么问题? – GrIsHu

+0

先生我从http://iamvijayakumar.blogspot.in/2012/06/android-animated-gif-example.html下来这个代码下降,但是当我试图导入这个,它得到了结果,我需要什么我需要(gif动画的一个例子,请帮助我... :( –

+0

什么是你得到的错误?你是什么意思的腐败? – GrIsHu

回答

1

尝试使用ImageView.For例子:

<?xml version="1.0" encoding="utf-8"?> 
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false"> 

    <item android:drawable="@drawable/image_1" android:duration="100" /> 
    <item android:drawable="@drawable/image_2" android:duration="100" /> 
    <item android:drawable="@drawable/image_3" android:duration="100" /> 
    </animation-list> 

在您的布局:

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/my_drawable" /> 

希望这会帮助你.. :)

+0

好吧,我试试这个感谢... :) –