嗨,我是android开发的初学者。我正在开发类似涂料的涂料。我已经在画布上画了一些点。现在我想通过fingure在这些点之间画线。我不知道我怎么能做到这一点。我没有使用谷歌地图。
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;
}
}
我想用fingure在这些点之间划一条直线。如果我的fingure远离那个特定的点,之后我移回我的fingure比绘制的线将被删除。即仅在这些特定点之间绘制的线。
答案 0 :(得分:0)
在SO中只是一个人站在这里,通常最好只有在你尝试了一些事情并且没有成功之后来这里问问。这样你就可以得到诸如“你有什么尝试......?”之类的评论。和那样的东西。
关于你的问题。 http://developer.android.com/reference/android/graphics/Canvas.html是一个很好的起点。 Android文档非常有用。 如您所见,您有drawLine()方法。哪个应该按照你的要求做。 祝你好运!
答案 1 :(得分: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;
}
}
它在我的应用程序中运行代码..所有最佳
答案 2 :(得分:0)