Advertisement
Guest User

Untitled

a guest
Apr 17th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1.  
  2. import android.graphics.drawable.Drawable;
  3. import android.app.Activity;
  4. import android.content.Context;
  5.  
  6.  
  7. import android.graphics.Bitmap;
  8. import android.graphics.Canvas;
  9. import android.graphics.Color;
  10. import android.graphics.Paint;
  11. import android.graphics.Path;
  12. import android.os.Bundle;
  13. import android.util.DisplayMetrics;
  14. import android.util.Log;
  15. import android.view.MotionEvent;
  16. import android.view.View;
  17.  
  18. import android.view.Window;
  19. import android.view.WindowManager;
  20. import android.widget.Button;
  21.  
  22. public class DrawBoard extends Activity {
  23. private Bitmap mBitmap;
  24. private MyView mView;
  25. private Button leave;
  26. private Button button;
  27. private Canvas mCanvas;
  28.  
  29. public void onCreate(Bundle bundle) {
  30. super.onCreate(bundle);
  31. // remove the title, and display "full screen"
  32. this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  33. this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  34. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  35. // get the windows resolution, for bitmap use
  36. DisplayMetrics dm = new DisplayMetrics();
  37. getWindowManager().getDefaultDisplay().getMetrics(dm);
  38. // create a new bitmap for paint
  39.  
  40. mBitmap = Bitmap.createBitmap(351, 351, Bitmap.Config.ARGB_8888);
  41. // mBitmap=getResources().getDrawable(R.drawable.ab123);
  42.  
  43. setContentView(R.layout.main);
  44. mView = new MyView(this);
  45. // ImageView iv = (ImageView) findViewById(R.id.imageView01);
  46. button = (Button) findViewById(R.id.button01);
  47. button.setOnClickListener(new Button.OnClickListener() {
  48. public void onClick(View v) {
  49. setContentView(mView);
  50. }
  51. });
  52.  
  53. leave = (Button) findViewById(R.id.button02);
  54. leave.setOnClickListener(new Button.OnClickListener() {
  55. public void onClick(View v) {
  56. finish();
  57. }
  58. });
  59.  
  60. }
  61.  
  62. private class MyView extends View {
  63.  
  64. private Path mPath;
  65. private Paint mBitmapPaint;
  66. private Paint mPaint, mPaint1;
  67. private float new_x, new_y;
  68. private float old_x, old_y;
  69. private Drawable PIC1;
  70.  
  71. int px = getMeasuredWidth();
  72. int py = getMeasuredWidth();
  73. int pz = getWidth();
  74. DisplayMetrics dm = new DisplayMetrics();
  75.  
  76. public MyView(Context context) {
  77. super(context);
  78.  
  79. mCanvas = new Canvas(mBitmap);
  80. mPath = new Path();
  81. mBitmapPaint = new Paint(Paint.DITHER_FLAG);
  82. mPaint = new Paint();
  83. // mPaint1= new Paint();
  84. // mPaint1.setColor(Color.BLACK);
  85. // make paint more smooth
  86. mPaint.setAntiAlias(true);
  87. mPaint.setDither(true);
  88. // (1.FILL 2.FILL_AND_STROKE 3.STROKE)
  89. mPaint.setStyle(Paint.Style.STROKE);
  90. // (1.BEVEL 2.MITER 3.ROUND) default is MITER
  91. mPaint.setStrokeJoin(Paint.Join.ROUND);
  92. // (1.BUTT 2.ROUND 3.SQUARE) default is BUIT
  93. mPaint.setStrokeCap(Paint.Cap.ROUND);
  94. // if set "0", will be a line
  95. mPaint.setStrokeWidth(4);
  96. mPaint.setColor(Color.GREEN);
  97. }
  98.  
  99. protected void onDraw(Canvas canvas) {
  100. /*
  101. * getWindowManager().getDefaultDisplay().getMetrics(dm); int width
  102. * = dm.widthPixels; int height = dm.heightPixels; PIC1 =
  103. * this.getResources().getDrawable(R.drawable.test);
  104. * PIC1.setBounds(0,351,351,height);
  105. */
  106. // set background color
  107.  
  108. canvas.drawColor(Color.WHITE);
  109.  
  110. // canvas.drawLine(0, 0, 300, 300, mPaint);
  111. // draw the specified bitmap
  112. canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
  113. // the path will be filled or framed based on the Style in the paint
  114. canvas.drawPath(mPath, mPaint);
  115. // PIC1.draw(canvas);
  116.  
  117. }
  118.  
  119. public boolean onTouchEvent(MotionEvent event) {
  120. new_x = event.getX();
  121. new_y = event.getY();
  122.  
  123. if (new_y > 351) {
  124. setContentView(R.layout.main);
  125. }
  126. if (event.getAction() == MotionEvent.ACTION_DOWN) {
  127. // save the previous path, then reset the path
  128. mPath.reset();
  129. // start position
  130. mPath.moveTo(new_x, new_y);
  131. old_x = new_x;
  132. old_y = new_y;
  133. mCanvas.save();
  134. } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
  135. float dx = Math.abs(new_x - old_x);
  136. float dy = Math.abs(new_y - old_y);
  137.  
  138. if (dx >= 4 || dy >= 4) {
  139. // set path from (old_x, old_y) to ((new_x + old_x)/2,
  140. // (new_y + old_y)/2)
  141. mPath.quadTo(old_x, old_y, (new_x + old_x) / 2,
  142. (new_y + old_y) / 2);
  143. old_x = new_x;
  144. old_y = new_y;
  145. }
  146. } else if (event.getAction() == MotionEvent.ACTION_UP) {
  147. // unknown, this line like no used
  148. mPath.lineTo(old_x, old_y);
  149. // paint the path when "ACTION_UP"
  150.  
  151. mCanvas.drawPath(mPath, mPaint);
  152. // save the paint
  153. mPath.reset();
  154. }
  155.  
  156. // update the paint, it will recall the onDraw function
  157. invalidate();
  158. return true;
  159. }
  160. }
  161. }
  162.  
  163.  
  164. //下面是XML設計
  165. <?xml version="1.0" encoding="utf-8"?>
  166. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  167. android:layout_width="fill_parent"
  168. android:layout_height="fill_parent"
  169. android:orientation="vertical" >
  170.  
  171. <LinearLayout
  172. android:id="@+id/linearLayout1"
  173. android:layout_width="246dp"
  174. android:layout_height="wrap_content" >
  175.  
  176. <Button
  177. android:id="@+id/button01"
  178. android:layout_width="wrap_content"
  179. android:layout_height="wrap_content"
  180. android:text="載入畫布" />
  181.  
  182. <Button
  183. android:id="@+id/button02"
  184. android:layout_width="wrap_content"
  185. android:layout_height="wrap_content"
  186. android:text="finish" />
  187. </LinearLayout>
  188.  
  189. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement