[GoBack]

 

撰写Scribbler.java

// Scribbler.java

package com.example.hoo;

import java.util.ArrayList;

import java.util.Iterator;

import android.annotation.SuppressLint;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.view.SurfaceHolder;

 

public class Scribbler {

        private ArrayList<dwPoint> poList;

        private ArrayList<Integer> colorList;

        private SurfaceHolder mHolder;

        private Paint paint= null;

        public static long initial_time;

        private long draw_time;

         

 @SuppressLint("UseValueOf") public Scribbler(SurfaceHolder holder){

          mHolder = holder;

          paint = new Paint();

          poList = new ArrayList<dwPoint>();

          colorList = new ArrayList<Integer>();

          //-----------------------------------

          colorList.add(new Integer(Color.WHITE));

          colorList.add(new Integer(Color.RED));

          colorList.add(new Integer(Color.GREEN));

          colorList.add(new Integer(Color.YELLOW));

  }

  public ArrayList<Integer> getColorList(){   return colorList;  }

  public void clear(){

                Canvas canvas = mHolder.lockCanvas();

               canvas.drawColor(Color.BLACK);

               mHolder.unlockCanvasAndPost(canvas);

           begin_record();

  }

  public void begin_record(){ 

                initial_time  = System.currentTimeMillis();

             poList.clear();  

  }

  public void record_down(int x, int y, int c_idx){

    dwPoint po = new dwPoint(x, y, 0, colorList.get(c_idx).intValue());

             poList.add(po);

  }

  public void record_move(int x, int y, int c_idx) { 

         dwPoint po = new dwPoint(x, y, 1, colorList.get(c_idx).intValue());

                 poList.add(po); // add a new point

                 this.quick_draw();

  }

  public void play(){

           subThread th = new subThread(this);

           th.start();

  } 

  public void draw() {

            long curr_time;        

            long base_time =0;          

            int k = 0;

           dwPoint po; 

 

            Iterator<dwPoint> it = poList.iterator();

           while (it.hasNext() ){

               po = it.next();

              draw_time = po.m_timeSpan;

               if(k == 0)  

                    base_time = System.currentTimeMillis();

            k++;

            //---------   waiting ----------------------------------

            do { curr_time =  System.currentTimeMillis() - base_time;     

                 } 

            while (curr_time < draw_time);

            //-------------------------------------------

             this.paint_to(k);

           }

     }

    public void paint_to(int k) {

               Canvas canvas = mHolder.lockCanvas();

               canvas.drawColor(Color.BLACK);

               dwPoint po;    

            int lastX = 0;

            int lastY = 0;

           

            Iterator<dwPoint> it = poList.iterator();

            for (int i = 0; i<k; i++) {

               po = it.next();

           if(po.m_type == 0 )  { 

                    lastX = po.m_x;  lastY = po.m_y;

                }

           else { 

                   paint.setColor(po.m_color);

                   paint.setStrokeWidth(2);

                   canvas.drawLine(lastX, lastY, po.m_x, po.m_y, paint);

                   lastX = po.m_x;   

                   lastY = po.m_y; 

                   }

         }

            mHolder.unlockCanvasAndPost(canvas);

     }  

    public void quick_draw() {

               int size = poList.size();

               paint_to(size);

     }

//---------------------------------------------

 class subThread extends Thread {

         private Scribbler scr;

         subThread(Scribbler s){

                 scr = s;

         }

         public void run() {

             scr.draw();

        } 

       }

//---------------------------------------------

    class dwPoint {

      public int m_x, m_y, m_type;

       public long m_timeSpan;

       public int m_color;

       public dwPoint()

                    {}

     public dwPoint(int x, int y, int ty, int cc) {

           m_x = x;            m_y = y;

           m_type = ty;                m_color = cc;

          m_timeSpan = (long)(System.currentTimeMillis() - Scribbler.initial_time);

     }

  } 

}

 

撰写SvStub.java

 

// SvStub.java

package com.example.hoo;

import android.content.Context;

import android.view.MotionEvent;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

 

abstract class SvStub extends SurfaceView implements SurfaceHolder.Callback {

    protected SurfaceHolder mHolder;

    private int color_idx;

     

    SvStub(Context context) throws InterruptedException {

        super(context);

        color_idx = 0;

        mHolder = getHolder();

        mHolder.addCallback(this);

     }

   public void surfaceCreated(SurfaceHolder holder) {

           begin_record();

     }

   public void surfaceDestroyed(SurfaceHolder holder) {}

   public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {}

   @Override

   public boolean onTouchEvent(MotionEvent event) {

                int action = event.getAction();

                if(action == MotionEvent.ACTION_DOWN){

                         record_down((int)event.getX(), (int)event.getY(), color_idx);

               }

                else if(action == MotionEvent.ACTION_MOVE){

                         record_move((int)event.getX(), (int)event.getY(), color_idx);

               }

                return true;

  }

  public void change_color(int c){  color_idx = c;  }

  public abstract void begin_record();

  public abstract void record_down(int x, int y, int cx);

  public abstract void record_move(int x, int y, int cx);

}

 

撰写mySV.java

 

// mySV.java

package com.example.hoo;

import java.util.ArrayList;

import android.content.Context;

 

public class mySV extends SvStub {

   private Scribbler scr;

   Context ctx;

               

   mySV(Context context) throws InterruptedException {

          super(context);

          ctx = context;

          scr = new Scribbler(mHolder);

        }

@Override public void begin_record() {

            scr.begin_record();

}

@Override public void record_down(int x, int y, int color_idx) {

            scr.record_down(x, y, color_idx);

}

@Override public void record_move(int x, int y, int color_idx) {

            scr.record_move(x, y, color_idx);

}

public void clear(){   scr.clear();   }

public void play(){   scr.play();   }

public ArrayList<Integer> getColorList(){

                    return scr.getColorList();

  }

}

 

撰写MainActivity.java

 

// MainActivity.java

package com.example.hoo;

import java.util.ArrayList;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.WindowManager;

import android.widget.Button;

import android.widget.LinearLayout;

 

public class MainActivity extends Activity implements OnClickListener {

        private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;

        private mySV mv = null;

        private Button[] btn;

        private Button ibtn, ibtn2, ibtn3;

        private ArrayList<Integer> colorList;

         

    @Override protected void onCreate(Bundle icicle) {

        super.onCreate(icicle);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

                               WindowManager.LayoutParams.FLAG_FULLSCREEN);

        try {   mv = new mySV(this);

                } catch (InterruptedException e) {  e.printStackTrace();                }

        btn = new Button[4];

                //----------------------------------

        LinearLayout layout = new LinearLayout(this);

        layout.setOrientation(LinearLayout.VERTICAL);

        //----------------------------------------------

        LinearLayout layout_r1 = new LinearLayout(this);

        layout_r1.setOrientation(LinearLayout.HORIZONTAL);

        LinearLayout.LayoutParams param1 =

               new LinearLayout.LayoutParams(120, 70);

        param1.leftMargin = 5;        param1.topMargin = 5;

        ibtn = new Button(this);        ibtn.setOnClickListener(this);

        ibtn.setTextSize(10);           ibtn.setText("New");

        layout_r1.addView(ibtn, param1);

        ibtn2 = new Button(this);     ibtn2.setOnClickListener(this);

        ibtn2.setTextSize(10);        ibtn2.setText("Play");

        layout_r1.addView(ibtn2, param1);

       ibtn3 = new Button(this);     ibtn3.setOnClickListener(this);

        ibtn3.setTextSize(10);        ibtn3.setText("Exit");

        layout_r1.addView(ibtn3, param1);

        LinearLayout.LayoutParams pa = new LinearLayout.LayoutParams(WC, WC);

        pa.topMargin = 4;

        layout.addView(layout_r1, pa);

        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(365, 360);

        param.topMargin = 5;     param.leftMargin = 5;

        layout.addView(mv, param);

        LinearLayout layout_r2 = new LinearLayout(this);

        layout_r2.setOrientation(LinearLayout.HORIZONTAL);

        LinearLayout.LayoutParams param2

               = new LinearLayout.LayoutParams(60, 45);

        param2.leftMargin = 5;        btn[0] = new Button(this);

        btn[0].setOnClickListener(this);

        layout_r2.addView(btn[0], param2);

        btn[1] = new Button(this);      btn[1].setOnClickListener(this);

        layout_r2.addView(btn[1], param2);

        btn[2] = new Button(this);      btn[2].setOnClickListener(this);

        layout_r2.addView(btn[2], param2);

        btn[3] = new Button(this);      btn[3].setOnClickListener(this);

        //-------------------------------------------- 

        colorList = mv.getColorList();

        int size = colorList.size();

        for(int k=0; k<size; k++)

               btn[k].setBackgroundColor(colorList.get(k).intValue());

        layout_r2.addView(btn[3], param2);

        layout.addView(layout_r2, pa); 

        setContentView(layout);

    }

        public void onClick(View v) {

                if(v == btn[0] )        mv.change_color(0);

                else if(v == btn[1])    mv.change_color(1);

                else if(v == btn[2])    mv.change_color(2);

                else if(v == btn[3])    mv.change_color(3);

                else if(v == ibtn)       mv.clear();

                else if(v == ibtn2)     mv.play();

                else if(v == ibtn3)  finish();

        }

}