[Go Back]
撰写小框架部分
// Scribbler.java
package com.example.scribble_ex04;
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;
IScribbleColor m_sv;
private Paint paint= null;
public static long initial_time;
private long draw_time;
@SuppressLint("UseValueOf") public Scribbler(SurfaceHolder holder, IScribbleColor sv){
mHolder = holder;
m_sv = sv;
paint = new Paint();
poList = new ArrayList<dwPoint>();
colorList = m_sv.getScribbleColors();
}
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);
}
}}
// IScribbleColor.java
package com.example.scribble_ex04;
import java.util.ArrayList;
public interface IScribbleColor {
public ArrayList<Integer> getScribbleColors();
}
// IScribble.java
package com.example.scribble_ex04;
public interface IScribble {
void begin_record();
void record_down(int x, int y, int cx);
void record_move(int x, int y, int cx);
void clear();
void play();
}
// ScriProxy.java
package com.example.scribble_ex04;
import android.view.SurfaceHolder;
public class ScriProxy implements IScribble{
private Scribbler scr;
public ScriProxy(SurfaceHolder holder, IScribbleColor sv){
scr = new Scribbler(holder, sv);
}
@Override public void begin_record() { scr.begin_record(); }
@Override public void record_down(int x, int y, int cx) {
scr.record_down(x, y, cx);
}
@Override public void record_move(int x, int y, int cx) {
scr.record_move(x, y, cx);
}
@Override public void clear() { scr.clear(); }
@Override public void play() { scr.play(); }
}
// SvStub.java
package com.example.scribble_ex04;
import android.content.Context;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
abstract class SvStub extends SurfaceView implements SurfaceHolder.Callback,
IScribbleColor{
protected SurfaceHolder mHolder;
private IScribble proxy;
private int color_idx;
SvStub(Context context) throws InterruptedException {
super(context);
color_idx = 0;
mHolder = getHolder();
mHolder.addCallback(this);
proxy = new ScriProxy(mHolder, this);
}
public void surfaceCreated(SurfaceHolder holder) {
proxy.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){
proxy.record_down((int)event.getX(), (int)event.getY(), color_idx);
}
else if(action == MotionEvent.ACTION_MOVE){
proxy.record_move((int)event.getX(), (int)event.getY(), color_idx);
}
return true;
}
public void change_color(int c){
color_idx = c;
}
public void clear(){
proxy.clear();
}
public void play(){
proxy.play();
}
}
撰写App部分
// mySV.java
package com.example.scribble_ex04;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
public class mySV extends SvStub{
private ArrayList<Integer> colorList;
mySV(Context context) throws InterruptedException {
super(context);
}
@SuppressLint("UseValueOf") @Override
public ArrayList<Integer> getScribbleColors() {
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));
return colorList;
}
public ArrayList<Integer> getColorList(){
return this.colorList;
}
}
// MainActivity.java
package com.example.scribble_ex04;
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) //New
mv.clear();
else if(v == ibtn2) //Play
mv.play();
else if(v == ibtn3) //Exit
finish();
}
}