Android简单画板的实现

画板主要功能的实现需要3步:

1、创建画板;

2、添加画笔及绘制;

3、清除画板。

主活动部分代码:

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;




public class MainActivity extends Activity {

private Button btn;
private MyView view;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.btn);
        view = (MyView) findViewById(R.id.draw);
        btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
view.clear();
}
});
    }


}

自定义view部分代码:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;




public class MyView extends SurfaceView implements Callback,OnTouchListener{


private Paint p = new Paint();
private Path path = new Path();

public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
p.setColor(Color.RED);
p.setTextSize(10);
p.setAntiAlias(true);
p.setStyle(Style.STROKE);
setOnTouchListener(this);

}
public void draw(){
Canvas canvas = getHolder().lockCanvas();
canvas.drawColor(Color.WHITE);
canvas.drawPath(path, p);
getHolder().unlockCanvasAndPost(canvas);
}


public void clear(){
path.reset();
draw();
}


@Override
public void surfaceCreated(SurfaceHolder holder) {
draw();
}


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub

}


@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub

}
@Override
public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(event.getX(), event.getY());
draw();
break;


case MotionEvent.ACTION_MOVE:
path.lineTo(event.getX(), event.getY());
draw();
break;
}

return true;
}


}

界面部分代码:

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.jikexueyuan.drawingboard.MainActivity" >


            android:id="@+id/draw"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />


            android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="清理画布" />



总结:在绘制画布部分要注意设置画笔的颜色,大小还有画笔的风格。清理画布方法内部要加入绘制方法,方便清理后的下次绘制。还可以在此基础上添加画圆画方等功能。

你可能感兴趣的:(android)