1. 程式人生 > >Android實現圖片切分與拼合

Android實現圖片切分與拼合

直接上程式碼:

package com.example.uitest;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
	ImageView mImage;
	ImageView mImage2;
	private int X_SPLIT = 4;
	private int Y_SPLIT = 4;
	Bitmap pre = null;
	List<ImagePiece> pieces = null;
	int pieceWidth;
	int pieceHeight;
	int mCurMax;
	int mCurIndex;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button btn = (Button) findViewById(R.id.button1);
		mImage = (ImageView) findViewById(R.id.image);
		mImage2 = (ImageView) findViewById(R.id.image2);
		btn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent();
				i.setClass(MainActivity.this, SecondActivity.class);
				MainActivity.this.startActivity(i);
			}
		});
		Resources res = getResources();
		final Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.tt);

		pieces = new ArrayList<ImagePiece>(X_SPLIT * Y_SPLIT);
		pieces = ImageSplitter.split(bmp, X_SPLIT, Y_SPLIT);
		int width = bmp.getWidth();
		int height = bmp.getHeight();
		pieceWidth = width / X_SPLIT;
		pieceHeight = height / Y_SPLIT;
		mCurMax = X_SPLIT * Y_SPLIT;
		mCurIndex = 1;
		for (int i = 0; i < Y_SPLIT; i++) {
			for (int j = 0; j < X_SPLIT; j++) {
				ImagePiece piece = new ImagePiece();
				piece.index = j + i * X_SPLIT;
				int xValue = j * pieceWidth;
				int yValue = i * pieceHeight;
				piece.bitmap = Bitmap.createBitmap(bmp, xValue, yValue,
						pieceWidth, pieceHeight);
				pieces.add(piece);
			}
		}

		pre = pieces.get(0).bitmap;

		mImage.setBackgroundResource(R.drawable.tt);
		timer.schedule(task, 2000, 2000); // 1s後執行task,經過1s再次執行
		mImage2.setBackgroundDrawable(new BitmapDrawable(pre));
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 1 && mCurIndex < X_SPLIT * Y_SPLIT) {
				// tvShow.setText(Integer.toString(i++));
				int i = mCurIndex / X_SPLIT;//1
				int j = mCurIndex % Y_SPLIT;//0
				Bitmap second = pieces.get(mCurIndex).bitmap;
				Bitmap result = Bitmap.createBitmap(pieceWidth * Y_SPLIT,
						pieceHeight * (i + 1), Config.ARGB_8888);
				Canvas canvas = new Canvas(result);
				canvas.drawBitmap(pre, 0, 0, null);
				//mImage.setBackgroundDrawable(new BitmapDrawable(result));
				canvas.drawBitmap(second, pieceWidth * j, pieceHeight * i, null);
				mImage2.setBackgroundDrawable(new BitmapDrawable(result));
				pre = result;
				mCurIndex++;
				super.handleMessage(msg);
			}
			super.handleMessage(msg);
		};
	};
	Timer timer = new Timer();
	TimerTask task = new TimerTask() {

		@Override
		public void run() {
			// 需要做的事:傳送訊息
			Message message = new Message();
			message.what = 1;
			handler.sendMessage(message);
		}
	};
}