扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
首先找到这条直线的所有点,然后开启一个线程循环获取点,用Handle发送消息去画线
成都创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站设计、做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的吉木乃网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
@Override
public void run() {
try {
for(int i = 0 ; i myPointList.size(); i++)
{
MyPoint myPoint = myPointList1.get(i);
Message message = Message.obtain();
message.what = 2;
message.obj = myPoint;
mHandler.sendMessage(message);
Thread.sleep(100);
}
} catch (Exception e) {
e.printStackTrace();
}
}
原来一直以为canvas.drawText是根据给出的坐标做为左上角来画的,今天才发现给出的坐标是左下角坐标。canvas.drawText("HelloWorld!",50,110,paint);canvas.drawLine(50,110,150,110,paint);注意观察横线的开始位置
你好!
步骤1:在你创建的工程中新建一个class文件,随便起个名字叫MyView吧,打开文件会看到:
public
class
MyView
{
}
步骤2:让该类继承
View类,也就是在MyView后面添几个字母,如:
public
class
MyView
extends
View
{
}
步骤3:在上面的这个类中,写一个方法:
public
class
MyView
extends
View
{
protected
void
onDraw(Canvas
canvas)
{
..........
}
}
步骤4:在onDrow方法中写一条语句:
public
class
MyView
extends
View
{
protected
void
onDraw(Canvas
canvas)
{
canvas.drawLine(起点横坐标,起点纵坐标,终点横坐标,终点纵坐标,线的颜色);
}
}
注:看到“drawLine”了吧,这就是在画线。
如有疑问,请追问。
//代码如下
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import android.os.Bundle;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.Overlay;
import com.baidu.mapapi.Projection;
public class BaiduMapActivity extends MapActivity {
private Context mContext;
private MapView mapView;
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private GeoPoint gpoint1, gpoint2, gpoint3;// 连线的点
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.baidumap_layout);
BaseApplication baseApp = (BaseApplication) this.getApplication();
if (baseApp.mBMapManage == null) {
baseApp.mBMapManage = new BMapManager(mContext);
baseApp.mBMapManage.init(baseApp.mStrKey,
new BaseApplication.MyGeneralListener());
}
baseApp.mBMapManage.start();
super.initMapActivity(baseApp.mBMapManage);// 初始化map sdk
mapView = (MapView) findViewById(R.id.bmapView);
mapView.setBuiltInZoomControls(true);
// 设置在缩放动画过程中也显示overlay,默认为不绘制
mapView.setDrawOverlayWhenZooming(true);
// RouteLine routeLine =
// (RouteLine)getIntent().getSerializableExtra("routeLine");
//这里画点和连接线
MyOverlay myOverlay = new MyOverlay();
mapView.getOverlays().add(myOverlay);
MapController mapController = mapView.getController();
mapController.zoomIn();
gpoint1 = new GeoPoint((int) (2259316 * 10),
(int) (11396279 * 10));
gpoint2 = new GeoPoint((int) (2259245 * 10),
(int) (11396226 * 10));
gpoint3 = new GeoPoint((int) (2259121 * 10),
(int) (11396066 * 10));
mapController.animateTo(gpoint1);//设置一个起点
}
class MyOverlay extends Overlay {
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
Projection projection = mapView.getProjection();
Point p1 = new Point();
Point p2 = new Point();
Point p3 = new Point();
// 经度转像素
projection.toPixels(gpoint1, p1);
projection.toPixels(gpoint2, p2);
projection.toPixels(gpoint3, p3);
//第一个画笔 画圆
Paint fillPaint = new Paint();
fillPaint.setColor(Color.BLUE);
fillPaint.setAntiAlias(true);
fillPaint.setStyle(Style.FILL);
// 将图画到上层
canvas.drawCircle(p1.x, p1.y, 5.0f, fillPaint);
canvas.drawCircle(p2.x, p2.y, 5.0f, fillPaint);
canvas.drawCircle(p3.x, p3.y, 5.0f, fillPaint);
//第二个画笔 画线
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setDither(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(4);
//连接
Path path = new Path();
path.moveTo(p1.x, p1.y);
path.lineTo(p2.x, p2.y);
path.lineTo(p3.x, p3.y);
//画出路径
canvas.drawPath(path, paint);
}
}
}
public class MainActivity extends Activity {
private ImageView iv;
private Bitmap baseBitmap;
private Canvas canvas;
private Paint paint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.iv = (ImageView) this.findViewById(R.id.iv);
// 创建一张空白图片
baseBitmap = Bitmap.createBitmap(480, 640, Bitmap.Config.ARGB_8888);
// 创建一张画布
canvas = new Canvas(baseBitmap);
// 画布背景为灰色
canvas.drawColor(Color.GRAY);
// 创建画笔
paint = new Paint();
// 画笔颜色为红色
paint.setColor(Color.RED);
// 宽度5个像素
paint.setStrokeWidth(5);
// 先将灰色背景画上
canvas.drawBitmap(baseBitmap, new Matrix(), paint);
iv.setImageBitmap(baseBitmap);
iv.setOnTouchListener(new OnTouchListener() {
int startX;
int startY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 获取手按下时的坐标
startX = (int) event.getX();
startY = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
// 获取手移动后的坐标
int stopX = (int) event.getX();
int stopY = (int) event.getY();
// 在开始和结束坐标间画一条线
canvas.drawLine(startX, startY, stopX, stopY, paint);
// 实时更新开始坐标
startX = (int) event.getX();
startY = (int) event.getY();
iv.setImageBitmap(baseBitmap);
break;
}
return true;
}
});
}
public void save(View view) {
try {
File file = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
OutputStream stream = new FileOutputStream(file);
baseBitmap.compress(CompressFormat.JPEG, 100, stream);
stream.close();
// 模拟一个广播,通知系统sdcard被挂载
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
intent.setData(Uri.fromFile(Environment
.getExternalStorageDirectory()));
sendBroadcast(intent);
Toast.makeText(this, "保存图片成功", 0).show();
} catch (Exception e) {
Toast.makeText(this, "保存图片失败", 0).show();
e.printStackTrace();
}
}
}
Android中的Path类一般用在自定义view里面(当系统提供的控件不能满足你的开发需求,需要自己写 控件,也就是自定义view)。
path类的lineTo方法:lineTo(float x, float y) ,该方法实现的仅仅是两点连成一线的绘制线路。
path类的quadTo方法:quadTo(float x1, float y1, float x2, float y2),该方法的实现是当我们不仅仅是画一条线甚至是画弧线时会形成平滑的曲线,该曲线又称为"贝塞尔曲线"(Bezier curve),其中,x1,y1为控制点的坐标值,x2,y2为终点的坐标值)。
主要区别(效果):对比quadTo方法绘制的线,lineTo绘制的线在弯曲部分很明显的不能形成平滑的弯曲,会出现明显的两点形成一线的突痕。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流