如何在两点之间画直线?
How to Draw straignt Line between two Points?
我正在尝试在我的代码中使用手指从 first
点移动到 second
,不管怎样,线应该是直的,如果first
点没有到达second
点,线应该在ACTION_UP.
上消失
我的灵感来自:
https://play.google.com/store/apps/details?id=nf.co.sumwu.dropdot&hl=en
我正在尝试通过 CustomView 的 onDraw 方法来实现它
这是我的尝试:
public class MyGFX extends View{
// setup initial color
private final int paintColor = Color.BLACK;
// defines paint and canvas
private Paint drawPaint;
Point first , second;
private Path path = new Path();
public MyGFX(Context context,AttributeSet attrs) {
super(context, attrs);
setupPaint(); // same as before
first = new Point(100,100);
second = new Point(200,100);
}
// Get x and y and append them to the path
public boolean onTouchEvent(MotionEvent event) {
float pointX = event.getX();
float pointY = event.getY();
// Checks for the event that occurs
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
boolean insideCircle =
(2*((int)pointX - (first.x + 2))) + (2*((int)pointY - (first.y + 2))) <= (2 * 2);
if(insideCircle){
// Starts a new line in the path
path.moveTo(first.x,first.y);
Toast.makeText(getContext(), "path.moveto is called \n first.x = "
+first.x+" first.y = "+first.y+" px ="+pointX+" py ="+pointY,
Toast.LENGTH_SHORT).show();
}
break;
case MotionEvent.ACTION_MOVE:
boolean insideCircle2 =
(2*(pointX - (second.x + 2))) + (2*(pointY - (second.y + 2))) <= (2 * 2);
if(insideCircle2){
// Starts a new line in the path
path.moveTo(pointX, pointY);
}
// Draws line between last point and this point
path.lineTo(pointX, pointY);
postInvalidate(); // Indicate view should be redrawn
break;
case MotionEvent.ACTION_UP:
path.close();
break;
default:
return false;
}
return true; // Indicate we've consumed the touch
}
// Setup paint with color and stroke styles
private void setupPaint() {
drawPaint = new Paint();
drawPaint.setColor(paintColor);
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(5);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
}
// Draws the path created during the touch events
@Override
protected void onDraw(Canvas canvas) {
canvas.drawCircle(first.x, first.y, 2, drawPaint);
canvas.drawCircle(second.x, second.y, 2, drawPaint);
if(!path.isEmpty())
canvas.drawPath(path, drawPaint);
}
}
drawLine(float startX, float startY, float stopX, float stopY, Paint paint)
所以
canvas.drawLine(10.0, 5.0, 11.0, 12.6,...);
试试这个 .....................
layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<play.decisio.com.myapplication.ConnectDotsView
android:layout_width="match_parent"
android:id="@+id/dot"
android:layout_height="match_parent" />
</RelativeLayout>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view=(ConnectDotsView)findViewById(R.id.dot);
List<Point> point=new ArrayList<Point>();
for(int i=0;i<5;i++){
point.add(new Point(i*100+100, i*100+10));
}
view.setPoints(point);
}
我正在尝试在我的代码中使用手指从 first
点移动到 second
,不管怎样,线应该是直的,如果first
点没有到达second
点,线应该在ACTION_UP.
我的灵感来自: https://play.google.com/store/apps/details?id=nf.co.sumwu.dropdot&hl=en
我正在尝试通过 CustomView 的 onDraw 方法来实现它
这是我的尝试:
public class MyGFX extends View{
// setup initial color
private final int paintColor = Color.BLACK;
// defines paint and canvas
private Paint drawPaint;
Point first , second;
private Path path = new Path();
public MyGFX(Context context,AttributeSet attrs) {
super(context, attrs);
setupPaint(); // same as before
first = new Point(100,100);
second = new Point(200,100);
}
// Get x and y and append them to the path
public boolean onTouchEvent(MotionEvent event) {
float pointX = event.getX();
float pointY = event.getY();
// Checks for the event that occurs
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
boolean insideCircle =
(2*((int)pointX - (first.x + 2))) + (2*((int)pointY - (first.y + 2))) <= (2 * 2);
if(insideCircle){
// Starts a new line in the path
path.moveTo(first.x,first.y);
Toast.makeText(getContext(), "path.moveto is called \n first.x = "
+first.x+" first.y = "+first.y+" px ="+pointX+" py ="+pointY,
Toast.LENGTH_SHORT).show();
}
break;
case MotionEvent.ACTION_MOVE:
boolean insideCircle2 =
(2*(pointX - (second.x + 2))) + (2*(pointY - (second.y + 2))) <= (2 * 2);
if(insideCircle2){
// Starts a new line in the path
path.moveTo(pointX, pointY);
}
// Draws line between last point and this point
path.lineTo(pointX, pointY);
postInvalidate(); // Indicate view should be redrawn
break;
case MotionEvent.ACTION_UP:
path.close();
break;
default:
return false;
}
return true; // Indicate we've consumed the touch
}
// Setup paint with color and stroke styles
private void setupPaint() {
drawPaint = new Paint();
drawPaint.setColor(paintColor);
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(5);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
}
// Draws the path created during the touch events
@Override
protected void onDraw(Canvas canvas) {
canvas.drawCircle(first.x, first.y, 2, drawPaint);
canvas.drawCircle(second.x, second.y, 2, drawPaint);
if(!path.isEmpty())
canvas.drawPath(path, drawPaint);
}
}
drawLine(float startX, float startY, float stopX, float stopY, Paint paint)
所以
canvas.drawLine(10.0, 5.0, 11.0, 12.6,...);
试试这个 .....................
layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<play.decisio.com.myapplication.ConnectDotsView
android:layout_width="match_parent"
android:id="@+id/dot"
android:layout_height="match_parent" />
</RelativeLayout>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view=(ConnectDotsView)findViewById(R.id.dot);
List<Point> point=new ArrayList<Point>();
for(int i=0;i<5;i++){
point.add(new Point(i*100+100, i*100+10));
}
view.setPoints(point);
}