主函数:
- import java.util.Date;
- import android.R.integer;
- import android.app.Activity;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.os.Bundle;
- import android.view.View.OnClickListener;
- import android.widget.LinearLayout;
- public class AndroidtestActivity04 extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);//必须有
- setContentView(R.layout.main);
- LinearLayout layout =(LinearLayout) findViewById(R.id.root);//在根界面创建一个线性布局管理器
- // final line l=new line(this);//实例一个画线实例
- // layout.addView(l);//画线实例添加进线性布局
- final ball b=new ball(this);
- // b.setMinimumWidth(layout.getWidth());
- // b.setMinimumHeight(layout.getHeight());
- layout.addView(b);
- }
- }
画一个圆ball.java类:
- import android.R.color;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.view.MotionEvent;
- import android.view.View;
- public class ball extends View{
- public float bx=50;
- public float by=50;
- public Paint p;
- public Canvas canvas;
- public ball(Context context) {
- super(context);
- p=new Paint();//定义一个画笔
- p.setColor(Color.GREEN);//设置画笔的颜色
- canvas =new Canvas();
- }
- //ondraw方法画一个圆
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- canvas.drawCircle(bx, by, 20, p);//画一个圆:x:50,y:50,半径:50的圆
- }
- //设置ball的触摸监听事件
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- // if (event.getAction()==MotionEvent.ACTION_DOWN) {//鼠标按下
- // bx=event.getX();
- // by=event.getY();
- // invalidate();//重绘圆
- // }
- // if (event.getAction()==MotionEvent.ACTION_UP) {//鼠标抬起
- //
- // }
- // if (event.getAction()==MotionEvent.ACTION_MOVE) {//鼠标移动
- // bx=event.getX();
- // by=event.getY();
- // invalidate();//重绘圆
- // }
- bx=event.getX();
- by=event.getY();
- invalidate();//重绘圆
- return true;
- }
- }
画线line.java类:鼠标点击画线
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.view.MotionEvent;
- import android.view.View;
- class line extends View {
- public Canvas canvas;
- public Paint p;
- private Bitmap bitmap;
- float x,y;
- int bgColor;
- public line(Context context) {
- super(context);
- //设置颜色
- bgColor = Color.WHITE;
- bitmap = Bitmap.createBitmap(480, 854, Bitmap.Config.ARGB_8888);
- //建立画布
- canvas=new Canvas();
- //设置位图
- canvas.setBitmap(bitmap);
- //设置图层
- p = new Paint(Paint.DITHER_FLAG);
- //可以更平滑
- p.setAntiAlias(true);
- p.setColor(Color.RED);
- //样式
- p.setStrokeCap(Paint.Cap.ROUND);
- //线条宽
- p.setStrokeWidth(8);
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- if (event.getAction() == MotionEvent.ACTION_MOVE) {
- //画线
- canvas.drawLine(x, y, event.getX(), event.getY(), p);
- invalidate();
- }
- if (event.getAction() == MotionEvent.ACTION_DOWN) {
- x = event.getX();
- y = event.getY();
- //画一个点
- canvas.drawPoint(x, y, p);
- invalidate();
- }
- if (event.getAction() == MotionEvent.ACTION_UP) {
- }
- //记录当前的鼠标坐标
- x = event.getX();
- y = event.getY();
- return true;
- }
- @Override
- public void onDraw(Canvas c) {
- c.drawBitmap(bitmap, 0, 0, null);
- }
- }