2012年5月21日 星期一

[ANDROID]用手指畫畫


DrawpictureActivity.java 

package bear.Graphis.drawpicture;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class DrawpictureActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
     
        DrawView drawView = new DrawView(this);
        setContentView(drawView);
        drawView.requestFocus();
    }
}


DrawView.java

package bear.Graphis.drawpicture;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class DrawView extends View implements OnTouchListener {

 List<Point> points = new ArrayList<Point>();
    Paint paint = new Paint();
 
 public DrawView(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
  setFocusable(true);
        setFocusableInTouchMode(true);

        this.setOnTouchListener(this);

        paint.setColor(Color.WHITE);
        paint.setAntiAlias(true);
 }

 public boolean onTouch(View v, MotionEvent event) {
  // TODO Auto-generated method stub
    Point point = new Point();
         point.x = (int) event.getX();
         point.y = (int) event.getY();
         points.add(point);
         invalidate();
  return true;
 }

 @Override
 protected void onDraw(Canvas canvas) {
  // TODO Auto-generated method stub
  super.onDraw(canvas);
   for (Point point : points) {
      canvas.drawCircle(point.x, point.y, 5, paint);
 }

 }
 }





課堂上練習範例

2 則留言:

  1. 請問一下,DrawView 這個Class ,我要如何去using 他阿? 我現在無法宣告DrawView 這個class,一直不知道要如何解決~~麻煩了

    回覆刪除
    回覆
    1. 不好意思喔~因為好久沒管理部落格
      所以才這麼晚回覆,不知道你的問題是不是已經解決了?

      你的問題是在DrawpictureActivity.java 裡面無法宣告 Drawview 的class嗎?

      刪除