`

如何让Android支持GIF图片

 
阅读更多

public class SampleView extends View {
 
private Movie mMovie;
 
private Bitmap mBitmap;
 
private Canvas mCanvas;
 
private long mMovieStart;
 
   private  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);
 
  
 
InputStream is;
 
  
 
is = context.getResources().openRawResource(R.drawable.aa_gif);
 
  
 
if (true) {
 
             mMovie = Movie.decodeStream(is);
 
         } else {
 
             byte[] array = streamToBytes(is);
 
             mMovie = Movie.decodeByteArray(array, 0, array.length);
 
         }
 
 
int w = mMovie.width();
 
         int h = mMovie.height();
 
         Log.i("-------width-------", w+"");
 
         Log.i("-------height-------", h+"");
 
       // int[] pixels = new int[w*h];
 
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
 
         mCanvas = new Canvas(mBitmap);
 
  
 
         }
 
  
    @Override
 
    protected void onDraw(Canvas canvas) {
 
        canvas.drawColor(0xFFCCCCCC);            
 
  
 
        Paint p = new Paint();
 
        p.setAntiAlias(true);
 
 
        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);
 
            mCanvas.drawColor(0xFFCCCCCC);
 
            mMovie.draw(mCanvas, 0, 0);
 
            Bitmap bitmap = Bitmap.createScaledBitmap(mBitmap, 300, 300, false);
 
  
            mBitmap.createScaledBitmap(mBitmap, 200, 300, false);
 
            canvas.drawBitmap(bitmap, 100, 200, null);
 
            invalidate();
 
        }
 
    }
 
}
public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SampleView sampleView = new SampleView(this);
        setContentView(sampleView);
    }
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics