我怎样才能画出两点之间的路径?

时间:2017-05-23 13:01:27

标签: java android android-studio

我的活动中有两个点的图像视图。我可以移动其中一个,我想展示两个点之间的路径。有什么建议吗?

@Override
public boolean onTouchEvent(MotionEvent event) {
    int action = event.getActionMasked();
    final float eventX = event.getRawX();
    final float eventY = event.getRawY();
    if(dotEnabled){
        dot.setX(eventX);
        dot.setY(eventY);
    }
    if(directionSet){
        direction.setX(eventX);
        direction.setY(eventY);
    }
    return false;
}

2 个答案:

答案 0 :(得分:1)

为您的绘画添加一些属性:

    Paint paint = new Paint();
    paint.setStrokeWidth(30); //here you can put any width you need
    paint.setColor(Color.BLUE);
    paint.setStyle(Paint.Style.STROKE);
    paint.setAntiAlias(true);

如果上面的代码在这部分代码下面不起作用:

 super(context);
 paint.setColor(Color.BLUE);
 this.x1 = x1;
 this.y1 = y1;
 this.x2 = x2;
 this.y2 = y2;

致电invalidate();

答案 1 :(得分:0)

最简单的方法是扩展FrameLayout,因为您已经将它用作布局中的外部ViewGroup。
要使其工作,您必须声明另外两个构造函数。在XML中声明元素时,不使用标准构造函数。所以你必须在所有这三个构造函数中设置Paint。为了减少冗余,我将其移至私有方法" initiatePaint()' 功能' declarePath()'必须被调用,当一个新的位置为' setDirection'众所周知。

InputStream rstream;
try {
    HttpResponse response = client.execute(HttpHost, method);

    rstream = Optional.ofNullable(httpResponse.getEntity())
    .map(e -> response.getContent()).orElse(null);

} catch (IOException e) {
    return BadSpot(e.getMessage()); 
}

要在布局中使用它,只需使用它而不是FrameLayout。

public class DrawLayout extends FrameLayout {
    Paint mPaint = new Paint();
    float mX1 = 0;
    float mY1 = 0;
    float mX2 = 0;
    float mY2 = 0;

    public DrawLayout(Context context) {
        super(context);
        this.initiatePaint();
    }

    public DrawLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.initiatePaint();
    }

    public DrawLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initiatePaint();
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawLine(x1, y1, x2, y2, paint);
    }

    public void declarePath(float x1, float y1, float x2, float y2) {
        this.mX1 = x1;
        this.mY1 = y1;
        this.mX2 = x2;
        this.mY2 = y2;
        this.invalidate();
    }

    private void initiatePaint() {
        this.mPaint.setColor(Color.BLUE);
        this.mPaint.setStrokeWidth(10);
        this.mPaint.setStyle(Paint.Style.STROKE);
    }
 }

在你的活动中,你必须做点什么:

<com.example.DrawLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fl"
    tools:context="it.uniroma3.sensorlog">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/mappa"
        android:id="@+id/map"/>

    <ImageView
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:src="@android:drawable/ic_notification_overlay"
        android:id="@+id/dot" />

    <ImageView
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:visibility="invisible"
        android:src="@android:drawable/ic_notification_overlay"
        android:id="@+id/setDirection" />
</DrawLayout>

}