android canvas 教程,Android Canvas drawLine

问题

I have a custom layout to draw a line based on touch input. I have it drawing the line but when the user touches the screen, the line disapeers and it draws a new line. What I want it to do is to draw a new line and leave the previous line there.

Here is my code:

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.view.View;

public class DrawView extends View {

Paint paint = new Paint();

float startX;

float startY;

float stopX;

float stopY;

public DrawView(Context context, AttributeSet attrs) {

super(context, attrs);

paint.setAntiAlias(true);

paint.setStrokeWidth(6f);

paint.setColor(Color.BLACK);

paint.setStyle(Paint.Style.STROKE);

paint.setStrokeJoin(Paint.Join.ROUND);

}

@Override

protected void onDraw(Canvas canvas) {

canvas.drawLine(startX, startY, stopX, stopY, paint);

}

@Override

public boolean onTouchEvent(MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

startX = event.getX();

startY = event.getY();

return true;

case MotionEvent.ACTION_MOVE:

stopX = event.getX();

stopY = event.getY();

break;

case MotionEvent.ACTION_UP:

stopX = event.getX();

stopY = event.getY();

break;

default:

return false;

}

Invalidate();

return true;

}

}

回答1:

You need to store all the lines instead of just the last one.

The following code is completely untested, but hopefully gives you the general idea.

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.view.View;

import java.util.ArrayList;

class Line {

float startX, startY, stopX, stopY;

public Line(float startX, float startY, float stopX, float stopY) {

this.startX = startX;

this.startY = startY;

this.stopX = stopX;

this.stopY = stopY;

}

public Line(float startX, float startY) { // for convenience

this(startX, startY, startX, startY);

}

}

public class DrawView extends View {

Paint paint = new Paint();

ArrayList lines = new ArrayList();

public DrawView(Context context, AttributeSet attrs) {

super(context, attrs);

paint.setAntiAlias(true);

paint.setStrokeWidth(6f);

paint.setColor(Color.BLACK);

paint.setStyle(Paint.Style.STROKE);

paint.setStrokeJoin(Paint.Join.ROUND);

}

@Override

protected void onDraw(Canvas canvas) {

for (Line l : lines) {

canvas.drawLine(l.startX, l.startY, l.stopX, l.stopY, paint);

}

}

@Override

public boolean onTouchEvent(MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {

lines.add(new Line(event.getX(), event.getY()));

return true;

}

else if ((event.getAction() == MotionEvent.ACTION_MOVE ||

event.getAction() == MotionEvent.ACTION_UP) &&

lines.size() > 0) {

Line current = lines.get(lines.size() - 1);

current.stopX = event.getX();

current.stopY = event.getY();

Invalidate();

return true;

}

else {

return false;

}

}

}

来源:https://stackoverflow.com/questions/16748146/android-canvas-drawline

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值