2012-07-11 44 views
7

Tôi đang tạo một ứng dụng Android, trong đó tôi muốn phát tệp GIF trong một lần xem hình ảnh. Tôi đã thử nhiều cách, tạo một lớp tùy chỉnh nhưng chưa thành công.Phát tệp hình ảnh GIF động trong imageview

Có ai đã thực hiện điều này trước đây không?

+0

thể trùng lặp của [Android - Hiển thị Animated gif] (http://stackoverflow.com/questions/3660209/android-display-animated-gif) –

+0

tham khảo này http://stackoverflow.com/questions/ 9158310/is-it-possible-to-set-an-hoạt-gif-file-as-live-hình nền-in-android – Aerrow

Trả lời

7

Bạn có thể tìm mã mẫu trong các mẫu SDK Android. Hình ảnh gif được hiển thị trong hoạt động mở rộng View

Mã này sử dụng hình ảnh gif khác hơn ImageView

import android.app.Activity; 
import android.content.Context; 
import android.graphics.*; 
import android.graphics.drawable.*; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.*; 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.ByteArrayOutputStream; 

public class BitmapDecode extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(new SampleView(this)); 
    } 

    private static class SampleView extends View { 
     private Bitmap mBitmap; 
     private Bitmap mBitmap2; 
     private Bitmap mBitmap3; 
     private Bitmap mBitmap4; 
     private Drawable mDrawable; 

     private Movie mMovie; 
     private long mMovieStart; 

     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 SampleView(Context context) { 
      super(context); 
      setFocusable(true); 

      java.io.InputStream is; 
      is = context.getResources().openRawResource(R.drawable.beach); 

      BitmapFactory.Options opts = new BitmapFactory.Options(); 
      Bitmap bm; 

      opts.inJustDecodeBounds = true; 
      bm = BitmapFactory.decodeStream(is, null, opts); 

      // now opts.outWidth and opts.outHeight are the dimension of the 
      // bitmap, even though bm is null 

      opts.inJustDecodeBounds = false; // this will request the bm 
      opts.inSampleSize = 4;    // scaled down by 4 
      bm = BitmapFactory.decodeStream(is, null, opts); 

      mBitmap = bm; 

      // decode an image with transparency 
      is = context.getResources().openRawResource(R.drawable.frog); 
      mBitmap2 = BitmapFactory.decodeStream(is); 

      // create a deep copy of it using getPixels() into different configs 
      int w = mBitmap2.getWidth(); 
      int h = mBitmap2.getHeight(); 
      int[] pixels = new int[w*h]; 
      mBitmap2.getPixels(pixels, 0, w, 0, 0, w, h); 
      mBitmap3 = Bitmap.createBitmap(pixels, 0, w, w, h, 
              Bitmap.Config.ARGB_8888); 
      mBitmap4 = Bitmap.createBitmap(pixels, 0, w, w, h, 
              Bitmap.Config.ARGB_4444); 

      mDrawable = context.getResources().getDrawable(R.drawable.button); 
      mDrawable.setBounds(150, 20, 300, 100); 

      is = context.getResources().openRawResource(R.drawable.animated_gif); 
      if (true) { 
       mMovie = Movie.decodeStream(is); 
      } else { 
       byte[] array = streamToBytes(is); 
       mMovie = Movie.decodeByteArray(array, 0, array.length); 
      } 
     } 

     @Override protected void onDraw(Canvas canvas) { 
      canvas.drawColor(0xFFCCCCCC);    

      Paint p = new Paint(); 
      p.setAntiAlias(true); 

      canvas.drawBitmap(mBitmap, 10, 10, null); 
      canvas.drawBitmap(mBitmap2, 10, 170, null); 
      canvas.drawBitmap(mBitmap3, 110, 170, null); 
      canvas.drawBitmap(mBitmap4, 210, 170, null); 

      mDrawable.draw(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 = 1000; 
       } 
       int relTime = (int)((now - mMovieStart) % dur); 
       mMovie.setTime(relTime); 
       mMovie.draw(canvas, getWidth() - mMovie.width(), 
          getHeight() - mMovie.height()); 
       invalidate(); 
      } 
     } 
    } 
} 

Bạn có thể sử dụng những liên kết này cũng

http://weavora.com/blog/2012/02/07/android-how-to-use-animated-gif/

http://droid-blog.net/2011/10/14/tutorial-how-to-use-animated-gifs-in-android-part-1/

+1

whoaa ... để hiển thị một gif đơn giản, bạn phải viết nhiều mã này? hệ thống cần phải được thay đổi –

3

Natively ImageView không hỗ trợ hình ảnh động. Bạn có thể sử dụng hai tùy chọn để hiển thị hoạt hình file gif

  1. Sử dụng VideoView
  2. Sử dụng ImageView

Nhưng chia file gif thành nhiều phần và sau đó áp dụng hình ảnh động với nó. Hãy thử link-play-gif-animation này, bạn sẽ nhận được kết quả mong muốn.

+1

Bạn có thể cho biết làm thế nào nó có thể được thực hiện bằng cách sử dụng VideoView ?? –

+0

@SHAKIRSHABBIR http://stackoverflow.com/a/26712205/945247 – Leon

5

Bạn có thể hiển thị Gif trong android mà không sử dụng bất kỳ thư viện của bên thứ ba nào. Đây là cách tôi đã làm điều đó.

Tạo chế độ xem web.

<WebView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageWebView" /> 

Hầu hết các ví dụ cho thấy cách mở tệp từ thư mục nội dung nhưng tại đây tôi chỉ cho bạn cách mở tệp từ thẻ SD/bộ nhớ.

WebView webView = (WebView) findViewById(R.id.imageWebView); 
    String data = "<body> <img src = \""+ filePath+"\"/></body>"; 
    // 'filePath' is the path of your .GIF file on SD card. 
    webView.loadDataWithBaseURL("file:///android_asset/",data,"text/html","UTF-8",null); 
Các vấn đề liên quan