Android之涂鸦(简单的画图)

package com.android.draw;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class ActivityMain extends Activity implements OnTouchListener {

	private Canvas canvas;
	private Paint paint;
	private Bitmap bitmap;
	private Bitmap bmp = null;
	private int width;
	private int height;
	private ImageView imageView;

	private float downX;
	private float downY;
	private float upX;
	private float upY;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.main);
		imageView = (ImageView) findViewById(R.id.imageview);
	}

	public void DrawBackground() {
		Display display = getWindowManager().getDefaultDisplay();
		width = display.getWidth();
		height = display.getHeight();
		bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		canvas = new Canvas(bitmap);
		paint = new Paint();
		paint.setColor(Color.WHITE);
		canvas.drawRect(0, 0, width, height, paint);
	}

	public void DrawBackground1() {
		Display display = getWindowManager().getDefaultDisplay();
		width = display.getWidth();
		height = display.getHeight();
		bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		canvas = new Canvas(bitmap);
		paint = new Paint();
		paint.setColor(Color.WHITE);
		canvas.drawBitmap(bmp, 0, 0, paint);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {

		int action = event.getAction();
		switch (action) {
		case MotionEvent.ACTION_UP:// 弹起
			upX = event.getX();// 获得弹起时的X坐标
			upY = event.getY();// 获得弹起时的Y坐标
			canvas.drawPoint(upX, upY, paint);
			imageView.invalidate();
			break;
		case MotionEvent.ACTION_DOWN:// 按下
			downX = event.getX();// 获得按下时的X坐标
			downY = event.getY();// 获得按下时的Y坐标
			break;
		case MotionEvent.ACTION_MOVE:// 移动
			float tempX = event.getX();// 获得移动时的X坐标
			float tempY = event.getY();// 获得移动时的Y坐标
			canvas.drawLine(downX, downY, tempX, tempY, paint);
			downX = tempX;
			downY = tempY;
			imageView.invalidate();
			break;
		}
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		menu.add(0, Menu.FIRST, 0, "选择图片");
		menu.add(0, Menu.FIRST + 1, 0, "绘制图片");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case Menu.FIRST:
			Intent intent = new Intent(Intent.ACTION_PICK);
			intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);// 跳转到外部存储卡图库中
			startActivityForResult(intent, 1);// 有结果跳转
			break;
		case Menu.FIRST + 1:
			DrawBackground();
			paint.setColor(Color.RED);
			paint.setStrokeWidth(5);
			imageView.setImageBitmap(bitmap);
			imageView.setOnTouchListener(this);
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case 1:
			Uri uri = data.getData();
			try {
				bmp = BitmapFactory.decodeStream(getContentResolver()
						.openInputStream(uri));// 从外部存储卡上获得图片
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			if (bmp != null) {
				Display display = getWindowManager().getDefaultDisplay();
				width = display.getWidth();
				height = display.getHeight();
				Bitmap canvasBmp = Bitmap.createBitmap(width, height,
						Bitmap.Config.ARGB_8888);

				canvas = new Canvas(canvasBmp);
				paint = new Paint();
				canvas.drawRect(0, 0, width, height, paint);

				Matrix matrix = new Matrix();// 图形矩阵
				canvas.drawBitmap(bmp, matrix, paint);
				paint.reset();
				paint.setColor(Color.RED);
				imageView.setImageBitmap(canvasBmp);
				imageView.setOnTouchListener(this);
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值