以下代码是我用来在图像上徒手绘制的代码
import android.content.Context;
import android.graphics.*;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
import android.widget.Toast;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.davemorrissey.labs.subscaleview.TempDemo;
import com.davemorrissey.labs.subscaleview.sample.R;
import java.util.ArrayList;
import java.util.List;
public class FreehandView extends SubsamplingScaleImageView implements OnTouchListener {
private PointF vPrevious;
private PointF vStart;
private boolean drawing = false;
public static TextView textViewObj1;
public static TextView textViewObj2;
public static TextView textViewObj3;
public static TextView textViewObj4;
public static TextView textViewObj5;
private int strokeWidth;
private List<PointF> sPoints;
public FreehandView(Context context, AttributeSet attr) {
super(context, attr);
initialise();
}
public FreehandView(Context context) {
this(context, null);
}
private void initialise() {
setOnTouchListener(this);
float density = getResources().getDisplayMetrics().densityDpi;
strokeWidth = (int)(density/60f);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (sPoints != null && !drawing) {
return super.onTouchEvent(event);
}
boolean consumed = false;
int touchCount = event.getPointerCount();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_1_DOWN:
drawing=true;
initialise();
vStart = new PointF(event.getX(), event.getY());
vPrevious = new PointF(event.getX(), event.getY());
break;
case MotionEvent.ACTION_POINTER_2_DOWN:
// Abort any current drawing, user is zooming
vStart = null;
vPrevious = null;
break;
case MotionEvent.ACTION_MOVE:
drawing=true;
PointF sCurrentF = viewToSourceCoord(event.getX(), event.getY());
PointF sCurrent = new PointF(sCurrentF.x, sCurrentF.y);
PointF sStart = vStart == null ? null : new PointF(viewToSourceCoord(vStart).x, viewToSourceCoord(vStart).y);
if (touchCount == 1 && vStart != null) {
float vDX = Math.abs(event.getX() - vPrevious.x);
float vDY = Math.abs(event.getY() - vPrevious.y);
if (vDX >= strokeWidth * 5 || vDY >= strokeWidth * 5) {
if (sPoints == null) {
sPoints = new ArrayList<PointF>();
sPoints.add(sStart);
}
sPoints.add(sCurrent);
vPrevious.x = event.getX();
vPrevious.y = event.getY();
drawing = true;
}
consumed = true;
invalidate();
} else if (touchCount == 1) {
// Consume all one touch drags to prevent odd panning effects handled by the superclass.
consumed = true;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
}
// Use parent to handle pinch and two-finger pan.
return consumed || super.onTouchEvent(event);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Don't draw anything before image is ready.
if (!isImageReady()) {
return;
}
Paint paint = new Paint();
paint.setAntiAlias(true);
if (sPoints != null && sPoints.size() >= 2) {
Path vPath = new Path();
PointF vPrev = sourceToViewCoord(sPoints.get(0).x, sPoints.get(0).y);
vPath.moveTo(vPrev.x, vPrev.y);
for (int i = 1; i < sPoints.size(); i++) {
PointF vPoint = sourceToViewCoord(sPoints.get(i).x, sPoints.get(i).y);
vPath.quadTo(vPrev.x, vPrev.y, (vPoint.x + vPrev.x) / 2, (vPoint.y + vPrev.y) / 2);
vPrev = vPoint;
}
paint.setStyle(Style.STROKE);
paint.setStrokeCap(Cap.ROUND);
paint.setStrokeWidth(strokeWidth * 2);
paint.setColor(Color.BLACK);
canvas.drawPath(vPath, paint);
paint.setStrokeWidth(strokeWidth);
paint.setColor(Color.argb(255, 0, 0, 0));
canvas.drawPath(vPath, paint);
}
}
public void reset() {
Toast.makeText(getContext(), TempDemo.text5, Toast.LENGTH_LONG).show();
if (drawing == false)
{
//Toast.makeText(getContext(), "false", Toast.LENGTH_LONG).show();
drawing=true;
}
if (drawing == true)
{
//Toast.makeText(getContext(), "true", Toast.LENGTH_LONG).show();
drawing=false;
}
//this.sPoints = null;
//invalidate();
}
}
从我触摸手指到释放手指时,这将画出一条连续线 然后我按下我的手指agin并继续绘制,直到我再次释放我的手指
问题是,当我在图像上的新位置触摸我的手指时,它会从我上次停止的位置画一条线
我希望能够在我松开手指并从我放手指的地方再次开始画画时停止绘画,但不要在旧停止和新开始之间画一条线
我认为它与movetto有关但这是我的第一个带绘图的应用程序,我可以找到的所有教程都有连续的绘图示例
有什么想法吗?
你一如既往地感谢
标记
答案 0 :(得分:0)
您使用名为sPoints的点列表作为一行。相反,你可以使用一系列的行:
List<List<PointF>> listOfLines = new ArrayList<List<PointF>>();
使用MotionEvent.ACTION_DOWN,您可以开始一个新行,并使用MotionEvent.ACTION_UP结束它。在OnDraw()中,您可以单独绘制所有可用的行。
鲁道夫