Gif動図バックグラウンド取得適応画面


バックグラウンドでgifのコントロールを取得し、自分で何かを追加して完全にしました.
			float saclex = (float) Width / (float) mMovie.width();
			float sacley = (float) Height / (float) mMovie.height();
			float sameRate = saclex < sacley ? saclex : sacley;
			canvas.scale(sameRate, sameRate);
			float x = ((canvas.getWidth() / sameRate - mMovie.width()) / 2);

主にスケールを計算する場所canvasはスケールを除いてなぜか分かりません....自分の必要に応じてメモしてください.このコントロールはitemで使用しています.widthは親コントロールの幅で、具体的な使用は少し変更する可能性がありますが、方法は同じです.
完全なコード
package com.example.gifview;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.WindowManager;

public class GifOnlineView extends View {
	private Movie mMovie;
	// private Bitmap mBmp;
	private long mPlayMovieTime;
	private String DOWNLOAD_ADDR;
	private Context context;

	private int Width, viewWidth;

	private int Height, viewHeight;

	public GifOnlineView(Context context) {
		super(context);
		this.context = context;
	}

	public GifOnlineView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void geturl(String url, int w, int h) {
		DOWNLOAD_ADDR = url;
		Width = w;
		Height = h;
		// WindowManager wm = (WindowManager) getContext().getSystemService(
		// Context.WINDOW_SERVICE);
		// int width = wm.getDefaultDisplay().getWidth();
		// int height = wm.getDefaultDisplay().getHeight();
		// System.out.println("width:"+width+"    height:"+height);

		readGifFormNet();//  gif 
	}

	private void readGifFormNet() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				httpTest();
				mHandler.sendEmptyMessage(0);
			}
		}).start();
	}

	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				invalidate();
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	private void httpTest() {
		try {
			URL url = new URL(DOWNLOAD_ADDR);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			// connection.setRequestMethod("GET");
			int size = connection.getContentLength();
			InputStream in = connection.getInputStream();
			byte[] array = streamToBytes(in);

			mMovie = Movie.decodeByteArray(array, 0, array.length);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	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();
	}

	@SuppressLint({ "DrawAllocation", "NewApi" })
	@Override
	protected void onDraw(Canvas canvas) {
		Paint p = new Paint();
		p.setAntiAlias(true);
		setLayerType(LAYER_TYPE_SOFTWARE, p);
		DisplayMetrics metrics = new DisplayMetrics();
		metrics = getResources().getDisplayMetrics();
		if (mMovie != null) {
			long now = android.os.SystemClock.uptimeMillis();
			if (mPlayMovieTime == 0) { // first time
				mPlayMovieTime = now;
			}
			int dur = mMovie.duration();
			if (dur == 0) {
				dur = 1000;
			}
			int relTime = (int) ((now - mPlayMovieTime) % dur);
			//  

			float saclex = (float) Width / (float) mMovie.width();
			float sacley = (float) Height / (float) mMovie.height();
			float sameRate = saclex < sacley ? saclex : sacley;
			canvas.scale(sameRate, sameRate);
			float x = ((canvas.getWidth() / sameRate - mMovie.width()) / 2);
//			System.out.println("aaaaa Width " + Width);
//			System.out.println("aaaaa  " + sameRate);
//			System.out.println("aaaaa gif " + mMovie.width());
//			System.out.println("aaaaa gif " + mMovie.width() * sameRate);
//			System.out.println("aaaaa  gif " + x);
//			System.out
//					.println("aaaaa  gif "
//							+ (((float) (Width / sameRate) - (float) (mMovie
//									.width())) / 2));
//			System.out.println("aaaaa pinmgu" + metrics.density);
//			System.out.println("aaaaa canvas" + canvas.getWidth());

			// canvas.translate( mMovie.width() * sameRate, mMovie.height() *
			// sameRate);

			mMovie.setTime(relTime);
			mMovie.draw(canvas, x, 0);
			canvas.restore();
			invalidate();
		}
	}
}