Android的画布的drawLine [英] Android Canvas drawLine
本文介绍了Android的画布的drawLine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个自定义的布局来绘制基于触摸输入一行。我有画线,但是当用户触摸屏幕时,该行disapeers,并绘制一个新的生产线。我想它做的是汲取新行,并留下previous行那里。 这是我的code:
进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.util.AttributeSet;
进口android.view.MotionEvent;
进口android.view.View;
公共类drawView函数扩展视图{
涂料粉刷=新的油漆();
浮动STARTX;
浮动startY;
浮动stopX;
浮动stopY;
公共drawView函数(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
paint.setAntiAlias(真正的);
paint.setStrokeWidth(1207米);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
}
@覆盖
保护无效的OnDraw(帆布油画){
canvas.drawLine(STARTX,startY,stopX,stopY,油漆);
}
@覆盖
公共布尔的onTouchEvent(MotionEvent事件){
开关(event.getAction()){
案例MotionEvent.ACTION_DOWN:
STARTX = event.getX();
startY = event.getY();
返回true;
案例MotionEvent.ACTION_MOVE:
stopX = event.getX();
stopY = event.getY();
打破;
案例MotionEvent.ACTION_UP:
stopX = event.getX();
stopY = event.getY();
打破;
默认:
返回false;
}
无效();
返回true;
}
}
解决方案
您需要存储所有的行,而不只是最后一个。
下面code是完全未经测试,但希望给你的总体思路。
进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.util.AttributeSet;
进口android.view.MotionEvent;
进口android.view.View;
进口的java.util.ArrayList;
班线{
浮STARTX,startY,stopX,stopY;
公共线路(STARTX浮动,浮动startY,浮stopX,浮stopY){
this.startX = STARTX;
this.startY = startY;
this.stopX = stopX;
this.stopY = stopY;
}
公共线路(STARTX浮动,浮动startY){//为了方便
这个(STARTX,startY,STARTX,startY);
}
}
公共类drawView函数扩展视图{
涂料粉刷=新的油漆();
ArrayList的<线>线=新的ArrayList<线>();
公共drawView函数(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
paint.setAntiAlias(真正的);
paint.setStrokeWidth(1207米);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
}
@覆盖
保护无效的OnDraw(帆布油画){
对(线L:行){
canvas.drawLine(l.startX,l.startY,l.stopX,l.stopY,油漆);
}
}
@覆盖
公共布尔的onTouchEvent(MotionEvent事件){
如果(event.getAction()== MotionEvent.ACTION_DOWN){
lines.add(新线(event.getX(),event.getY()));
返回true;
}
否则,如果((event.getAction()== MotionEvent.ACTION_MOVE ||
event.getAction()== MotionEvent.ACTION_UP)及&安培;
lines.size()> 0){
线电流= lines.get(lines.size() - 1);
current.stopX = event.getX();
current.stopY = event.getY();
无效();
返回true;
}
其他 {
返回false;
}
}
}
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;
}
}
解决方案
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<Line> lines = new ArrayList<Line>();
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;
}
}
}
这篇关于Android的画布的drawLine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文