Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.graphics.drawable.Drawable;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Path;
- import android.os.Bundle;
- import android.util.DisplayMetrics;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.Button;
- public class DrawBoard extends Activity {
- private Bitmap mBitmap;
- private MyView mView;
- private Button leave;
- private Button button;
- private Canvas mCanvas;
- public void onCreate(Bundle bundle) {
- super.onCreate(bundle);
- // remove the title, and display "full screen"
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- // get the windows resolution, for bitmap use
- DisplayMetrics dm = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);
- // create a new bitmap for paint
- mBitmap = Bitmap.createBitmap(351, 351, Bitmap.Config.ARGB_8888);
- // mBitmap=getResources().getDrawable(R.drawable.ab123);
- setContentView(R.layout.main);
- mView = new MyView(this);
- // ImageView iv = (ImageView) findViewById(R.id.imageView01);
- button = (Button) findViewById(R.id.button01);
- button.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- setContentView(mView);
- }
- });
- leave = (Button) findViewById(R.id.button02);
- leave.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- finish();
- }
- });
- }
- private class MyView extends View {
- private Path mPath;
- private Paint mBitmapPaint;
- private Paint mPaint, mPaint1;
- private float new_x, new_y;
- private float old_x, old_y;
- private Drawable PIC1;
- int px = getMeasuredWidth();
- int py = getMeasuredWidth();
- int pz = getWidth();
- DisplayMetrics dm = new DisplayMetrics();
- public MyView(Context context) {
- super(context);
- mCanvas = new Canvas(mBitmap);
- mPath = new Path();
- mBitmapPaint = new Paint(Paint.DITHER_FLAG);
- mPaint = new Paint();
- // mPaint1= new Paint();
- // mPaint1.setColor(Color.BLACK);
- // make paint more smooth
- mPaint.setAntiAlias(true);
- mPaint.setDither(true);
- // (1.FILL 2.FILL_AND_STROKE 3.STROKE)
- mPaint.setStyle(Paint.Style.STROKE);
- // (1.BEVEL 2.MITER 3.ROUND) default is MITER
- mPaint.setStrokeJoin(Paint.Join.ROUND);
- // (1.BUTT 2.ROUND 3.SQUARE) default is BUIT
- mPaint.setStrokeCap(Paint.Cap.ROUND);
- // if set "0", will be a line
- mPaint.setStrokeWidth(4);
- mPaint.setColor(Color.GREEN);
- }
- protected void onDraw(Canvas canvas) {
- /*
- * getWindowManager().getDefaultDisplay().getMetrics(dm); int width
- * = dm.widthPixels; int height = dm.heightPixels; PIC1 =
- * this.getResources().getDrawable(R.drawable.test);
- * PIC1.setBounds(0,351,351,height);
- */
- // set background color
- canvas.drawColor(Color.WHITE);
- // canvas.drawLine(0, 0, 300, 300, mPaint);
- // draw the specified bitmap
- canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
- // the path will be filled or framed based on the Style in the paint
- canvas.drawPath(mPath, mPaint);
- // PIC1.draw(canvas);
- }
- public boolean onTouchEvent(MotionEvent event) {
- new_x = event.getX();
- new_y = event.getY();
- if (new_y > 351) {
- setContentView(R.layout.main);
- }
- if (event.getAction() == MotionEvent.ACTION_DOWN) {
- // save the previous path, then reset the path
- mPath.reset();
- // start position
- mPath.moveTo(new_x, new_y);
- old_x = new_x;
- old_y = new_y;
- mCanvas.save();
- } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
- float dx = Math.abs(new_x - old_x);
- float dy = Math.abs(new_y - old_y);
- if (dx >= 4 || dy >= 4) {
- // set path from (old_x, old_y) to ((new_x + old_x)/2,
- // (new_y + old_y)/2)
- mPath.quadTo(old_x, old_y, (new_x + old_x) / 2,
- (new_y + old_y) / 2);
- old_x = new_x;
- old_y = new_y;
- }
- } else if (event.getAction() == MotionEvent.ACTION_UP) {
- // unknown, this line like no used
- mPath.lineTo(old_x, old_y);
- // paint the path when "ACTION_UP"
- mCanvas.drawPath(mPath, mPaint);
- // save the paint
- mPath.reset();
- }
- // update the paint, it will recall the onDraw function
- invalidate();
- return true;
- }
- }
- }
- //下面是XML設計
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <LinearLayout
- android:id="@+id/linearLayout1"
- android:layout_width="246dp"
- android:layout_height="wrap_content" >
- <Button
- android:id="@+id/button01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="載入畫布" />
- <Button
- android:id="@+id/button02"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="finish" />
- </LinearLayout>
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement