0

嗨,我是 android 开发的初学者。我正在开发一种类似油漆的应用程序。我已经在画布上画了一些点。现在我想通过手指在这些点之间画线。我不知道我该怎么做。我没有使用谷歌地图。

 package com.example.point;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class DrawView extends View implements OnTouchListener {
    private static final String TAG = "DrawView";

    List<Point> points = new ArrayList<Point>();
    List<Point> points1 = new ArrayList<Point>();
    List<Point> points2 = new ArrayList<Point>();
    Paint paint = new Paint();
PointF firstpoint=new PointF(100,100);
PointF secondpoint=new PointF(200,200);
Path path=new Path();
    public DrawView(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);

        this.setOnTouchListener(this);

        paint.setColor(Color.BLACK);

        paint.setAntiAlias(true);
    }

    @Override
    public void onDraw(Canvas canvas) {


        paint.setStrokeWidth(3);
        canvas.drawPoint(100, 100, paint);
        canvas.drawPoint(200, 200, paint);
        canvas.drawPoint(200, 400, paint);
        Point p=new Point();
        Point p1=new Point();
        //p1=points.get(1);
        for(int i=points.size()-2;i<points.size();i++)
        {

            p=points.get(i);
            //if(p1.x==100 && p1.y==100)
        //canvas.drawLine(100, 100, p.x, p.y, paint);
    //  path.moveTo(100, 100);
        //path.lineTo(200, 200);
        //canvas.drawPath(path, paint);
        }




    }

    public boolean onTouch(View view, MotionEvent event) {

        /*if(event.getAction()==MotionEvent.ACTION_MOVE)
        {
         Point point1 = new Point();          
            point1.x = event.getX();
            point1.y = event.getY();
            points.add(point1);
            invalidate();
            Log.d(TAG, "point: " + point1);


        }*/
        //if(event.getX()==100 && event.getY()==100)
        //{
            if(event.getAction()==MotionEvent.ACTION_MOVE)
            {
            Point point1 = new Point();          
            point1.x = event.getX();
            point1.y = event.getY();
            points.add(point1);

            invalidate();
            Log.d(TAG, "point: " + point1);
            //}

        }
        //return false;
        return true;



        /*switch (event.getAction()) {
         case MotionEvent.ACTION_DOWN:
             Point point = new Point();          
             point.x = event.getX();
             point.y = event.getY();
             points.add(point);
             invalidate();
             Log.d(TAG, "point: " + point);

             return true;

         case MotionEvent.ACTION_MOVE:
             Point point1 = new Point();          
             point1.x = event.getX();
             point1.y = event.getY();
             points1.add(point1);
             invalidate();
             Log.d(TAG, "point: " + point1);

             return true;

         case MotionEvent.ACTION_UP:

             Point point2 = new Point();          
             point2.x = event.getX();
             point2.y = event.getY();
             points2.add(point2);
             invalidate();
             Log.d(TAG, "point: " + point2);

             return true;
         }
     return false;

        //        return true;*/
    }
}

class Point {
    float x, y;

    @Override
    public String toString() {
        return x + ", " + y;
    }
}

我想用手指在这些点之间画一条直线。如果我的手指远离那个特定点,然后我向后移动我的手指,那么画的线就会被抹去。即仅在这些特定点之间绘制的线。

4

3 回答 3

0
public class HelloGoogleMaps extends  MapActivity  {
    /** Called when the activity is first created. */

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MapController mMapController;
        MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mMapController = mapView.getController();
        mMapController.setZoom(18);
      // Two points in Mexico about 1km apart
        GeoPoint point1 = new GeoPoint(19240000,-99120000);
        GeoPoint point2 = new GeoPoint(19241000,-99121000);
        mMapController.setCenter(point2);
        // Pass the geopoints to the overlay class
        MapOverlay mapOvlay = new MapOverlay(point1, point2);
        mapView.getOverlays().add(mapOvlay);
    }

    public class MapOverlay extends com.google.android.maps.Overlay {

      private GeoPoint mGpt1;
      private GeoPoint mGpt2;

      protected MapOverlay(GeoPoint gp1, GeoPoint gp2 ) {
         mGpt1 = gp1;
         mGpt2 = gp2;
      }
      @Override
      public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
            long when) {
         super.draw(canvas, mapView, shadow);
         Paint paint;
         paint = new Paint();
         paint.setColor(Color.RED);
         paint.setAntiAlias(true);
         paint.setStyle(Style.STROKE);
         paint.setStrokeWidth(2);
         Point pt1 = new Point();
         Point pt2 = new Point();
         Projection projection = mapView.getProjection();
         projection.toPixels(mGpt1, pt1);
         projection.toPixels(mGpt2, pt2);
         canvas.drawLine(pt1.x, pt1.y, pt2.x, pt2.y, paint);
         return true;
      }
   }
   @Override
   protected boolean isRouteDisplayed() {
      // TODO Auto-generated method stub
      return false;
   }
}

它在我的应用程序中运行代码......一切顺利

于 2012-08-07T11:07:31.553 回答
0

只是在这里提醒一下,通常最好是在您尝试过某些事情但没有成功之后才来这里询问。通过这种方式,您希望获得诸如“您尝试过什么......?”之类的评论。和类似的东西。

关于你的问题。http://developer.android.com/reference/android/graphics/Canvas.html是一个很好的起点。Android 文档非常有用。如您所见,您有一个drawLine()方法。这应该做你问的。祝你好运!

于 2012-08-07T10:51:36.767 回答
0

您的画布是否位于视图内?如果是这样,这是一个起点,假设您正在询问使用手指运动在点之间画线......

查找以下方法以获取帮助:

public boolean onTouchEvent(MotionEvent event)

检测手指运动。您需要区分刚刚发生的动作动作类型和动作动作“索引”。然后,如果运动是移动运动(例如手指在点之间移动),那么您可以调用drawPathdrawLine来绘制线条。

于 2012-08-07T11:18:10.687 回答