asdasd

package com.jleo;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;

public class MapView extends SurfaceView implements Callback {

	private LunarThread thread;
	public boolean running;
	private SurfaceHolder holder;
	private Resources mRes;
	private Position[] ps;
	private float zoom;
	private float offsetX;
	private float offsetY;
	private float lastX;
	private float lastY;

	public MapView(Context context, AttributeSet attrs) {
		super(context, attrs);
		thread = new LunarThread();

		holder = getHolder();
		holder.addCallback(this);

		mRes = context.getResources();
	}

	public boolean onTouchEvent(MotionEvent event) {
		int eventaction = event.getAction();

		switch (eventaction) {

		case MotionEvent.ACTION_MOVE:

			float endX = event.getX();
			float endY = event.getY();
			float lastX = this.lastX;
			float lastY = this.lastY;
			if (lastX == 0 && lastY == 0) {
				lastX = endX;
				lastY = endY;
			}
			this.lastX = endX;
			this.lastY = endY;
			offsetX += (endX - lastX)/zoom;
			offsetY += (endY - lastY)/zoom;
			break;
		case MotionEvent.ACTION_UP:
			this.lastX = 0;
			this.lastY = 0;
			break;
		case MotionEvent.ACTION_DOWN:
		this.lastX =  event.getX();
		this.lastY =  event.getY();
		break;
	}
		this.invalidate();
		return true;
	}
	
	class LunarThread extends Thread {
		private Bitmap androidIcon;
		private Canvas c;
		@Override
		public void run() {
			androidIcon = BitmapFactory.decodeResource(mRes, R.drawable.android);
			// bg = BitmapFactory.decodeResource(mRes, R.drawable.cor);
			while (true) {
				try {
					c = holder.lockCanvas(null);
					if (c != null)
						synchronized (holder) {
							doDraw(c);
						}
				} finally {
					if (c != null) {
						holder.unlockCanvasAndPost(c);
					}
				}
			}
		}

		public void doDraw(Canvas c) {
			// c.drawBitmap(bg, 0,0, null);
			c.drawColor(Color.WHITE);
			Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
			paint.setColor(Color.BLACK);

			// draw coordinate
			c.drawLine(0, c.getHeight() / 2, c.getWidth(), c.getHeight() / 2,
					paint);
			c.drawLine(c.getWidth() / 2, 0, c.getWidth() / 2, c.getHeight(),
					paint);
			float z = zoom;
			float ofx = offsetX;
			float ofy = offsetY;
			for (int i = 0; i < ps.length; i++) {
				Position p = ps[i];
				float x = ((float) p.x * c.getWidth() / 30000*z
						+ c.getWidth() / 2 + ofx*z) -androidIcon.getWidth()/2;
				float y = ((float) p.y * c.getHeight()
						/ 30000*z + c.getHeight() / 2 + ofy*z)-androidIcon.getHeight()/2;
				c.drawBitmap(androidIcon, x, y, paint);
			}
		}
	}

	public Thread getThread() {
		return thread;
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		running = true;
		ps = NormTest.generateMap();
		thread.start();
		zoom = 1;
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		running = false;
	}

	public void zoomIn() {
		zoom = zoom / 2;

	}

	public void zoomOut() {
		zoom = zoom * 2;
	}
}

你可能感兴趣的:(thread,C++,c,android,C#)