android 屏幕上面画线

作业如下:在android屏幕上面任意画线                            2011-12-1 ,从网上看到了,参考了,修改了,成功了,给大家分享下

package feng.f121.drawline;//本人创建的包名,每人有每人的不同的包

import java.security.PublicKey;

import android.R.integer;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

 

//新建一个类继承View

public class Drawl extends View{

 private int mov_x;//声明起点坐标
 private int mov_y;
 private Paint paint;//声明画笔
 private Canvas canvas;//画布
 private Bitmap bitmap;//位图
 private int blcolor;
 public Drawl(Context context) {
  super(context);
  paint=new Paint(Paint.DITHER_FLAG);//创建一个画笔
  bitmap = Bitmap.createBitmap(480, 854, Bitmap.Config.ARGB_8888); //设置位图的宽高
  canvas=new Canvas();
  canvas.setBitmap(bitmap);
  
  paint.setStyle(Style.STROKE);//设置非填充
  paint.setStrokeWidth(5);//笔宽5像素
  paint.setColor(Color.RED);//设置为红笔
  paint.setAntiAlias(true);//锯齿不显示
   
 }

//画位图
 @Override
 protected void onDraw(Canvas canvas) {
//  super.onDraw(canvas);
  canvas.drawBitmap(bitmap,0,0,null);
 }
 //触摸事件
 @Override
 public boolean onTouchEvent(MotionEvent event) {
  if (event.getAction()==MotionEvent.ACTION_MOVE) {//如果拖动
   canvas.drawLine(mov_x, mov_y, event.getX(), event.getY(), paint);//画线
   invalidate();
  }
  if (event.getAction()==MotionEvent.ACTION_DOWN) {//如果点击
   mov_x=(int) event.getX();
   mov_y=(int) event.getY();
   canvas.drawPoint(mov_x, mov_y, paint);//画点
   invalidate();

  }
  mov_x=(int) event.getX();
  mov_y=(int) event.getY();
  return true;
 }
 
 
}

 

在Activity中

public class DrawLine extends Activity {
 private Drawl bDrawl;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bDrawl=new Drawl(this);
       setContentView(bDrawl);//将view视图放到Activity中显示
    }

你可能感兴趣的:(android,屏幕,画线)