android-onDraw画图

public class BrashView extends View {
    String TAG="BrashView";
    Path path=new Path();
    Paint brush=new Paint();
    Button btnRresh;
    ViewGroup.LayoutParams layoutParams;
    public BrashView(Context content) {
        super(content);
        brush.setAntiAlias(true);
        brush.setColor(Color.BLUE);
        brush.setStyle(Paint.Style.STROKE);
        brush.setStrokeJoin(Paint.Join.ROUND);
        brush.setStrokeWidth(10f);
        btnRresh=new Button(content);
        layoutParams=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        btnRresh.setLayoutParams(layoutParams);
        btnRresh.setText("点我一下");
        btnRresh.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                 path.reset();
                 postInvalidate();
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
          float x=  event.getX();
          float y= event.getY();
          switch (event.getAction())
          {
              case MotionEvent.ACTION_DOWN:
                  path.moveTo(x, y);
                  Log.e(TAG,String.format("ACTION_DOWN Poin(%f,%f)",x,y));
                  return  true;
              case MotionEvent.ACTION_MOVE:
                  path.lineTo(x,y);
                  postInvalidate();
                  Log.e(TAG, String.format("ACTION_MOVE Poin(%f,%f)", x, y));
                  return  true;
          }
        Log.e(TAG, String.format("other Poin(%f,%f)", x, y));
        return false ;//super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
       // canvas.drawPath(path,brush);
        Paint p=new Paint();
        p.setTextSize(50);
        p.setColor(Color.RED);

        canvas.drawText("圆圈:", 50, 250, p);
        canvas.drawCircle(350, 250, 50, p);
        p.setAntiAlias(true);//去掉锯齿
        canvas.drawCircle(600, 250, 50, p);;

        canvas.drawText("画线以及弧线:", 50, 350, p);
        canvas.drawLines(new float[]{400, 350, 500, 350}, p);
        p.setStyle(Paint.Style.STROKE);//设置空心
        RectF rectF=new RectF(510,300,600,400);
        canvas.drawArc(rectF, 0, 180, false, p);// 第二个参数:开始旋转的角度,逆时针开始     第三个参数:要旋转的角度    第四个参数:ture:表示花弧形 false表示画弧线

        canvas.drawText("画矩形:", 50, 450, p);
        canvas.drawRect(300, 400, 400, 500, p);
        p.setStyle(Paint.Style.FILL);
        canvas.drawRect(600, 400, 800, 500, p);

        canvas.drawText("画扇形和椭圆:", 50, 550, p);
        Shader shader=new LinearGradient(0,0,100,100,new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW,Color.LTGRAY},null,Shader.TileMode.REPEAT);
        p.setShader(shader);
        rectF=new RectF(480,480,700,700);
        canvas.drawArc(rectF,180,120,true,p);//扇形
        rectF=new RectF(710,480,900,600);
        canvas.drawOval(rectF, p);//椭圆

        canvas.drawText("三角形:", 50, 650, p);
        Path path1=new Path();
        path1.moveTo(300, 500);
        path1.lineTo(300, 700);
        path1.lineTo(450, 700);
        path1.close();
        canvas.drawPath(path1, p);
        path1.reset();
        p.setStyle(Paint.Style.STROKE);
        path1.moveTo(500, 600);
        path1.lineTo(600, 600);
        path1.lineTo(650, 650);
        path1.lineTo(600, 700);
        path1.lineTo(500, 700);
        path1.lineTo(450, 650);
        path1.close();
        canvas.drawPath(path1, p);

        canvas.drawText("圆角矩形:", 50, 750, p);
        p.reset();
        p.setColor(Color.GRAY);
        p.setStyle(Paint.Style.FILL);
        rectF=new RectF(300,710,500,810);
        canvas.drawRoundRect(rectF, 50, 50, p);

        p.setTextSize(50);
        p.setStrokeWidth(3);
        p.setColor(Color.RED);
        p.setStyle(Paint.Style.STROKE);
        canvas.drawText("画曲线:", 50, 870, p);
        path1.reset();
        path1.moveTo(400, 800);
        path1.quadTo(420, 770, 440, 1100);
        canvas.drawPath(path1, p);

        canvas.drawText("画点:", 50, 1150, p);
        canvas.drawPoint(188, 1180, p);
        canvas.drawPoints(new float[]{170, 1200, 170, 1210, 171, 1206}, p);

        canvas.drawText("贴图:", 50, 1300, p);
        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
        canvas.drawBitmap(bitmap,200,1388,p);
        Log.e(TAG, "onDraw");
       // super.onDraw(canvas);
    }
}
发布了61 篇原创文章 · 获赞 8 · 访问量 4万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览