首先找到這條直線的所有點(diǎn),然后開啟一個(gè)線程循環(huán)獲取點(diǎn),用Handle發(fā)送消息去畫線
成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的吉木乃網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
@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是根據(jù)給出的坐標(biāo)做為左上角來畫的,今天才發(fā)現(xiàn)給出的坐標(biāo)是左下角坐標(biāo)。canvas.drawText("HelloWorld!",50,110,paint);canvas.drawLine(50,110,150,110,paint);注意觀察橫線的開始位置
你好!
步驟1:在你創(chuàng)建的工程中新建一個(gè)class文件,隨便起個(gè)名字叫MyView吧,打開文件會(huì)看到:
public
class
MyView
{
}
步驟2:讓該類繼承
View類,也就是在MyView后面添幾個(gè)字母,如:
public
class
MyView
extends
View
{
}
步驟3:在上面的這個(gè)類中,寫一個(gè)方法:
public
class
MyView
extends
View
{
protected
void
onDraw(Canvas
canvas)
{
..........
}
}
步驟4:在onDrow方法中寫一條語句:
public
class
MyView
extends
View
{
protected
void
onDraw(Canvas
canvas)
{
canvas.drawLine(起點(diǎn)橫坐標(biāo),起點(diǎn)縱坐標(biāo),終點(diǎn)橫坐標(biāo),終點(diǎn)縱坐標(biāo),線的顏色);
}
}
注:看到“drawLine”了吧,這就是在畫線。
如有疑問,請(qǐng)追問。
//代碼如下
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;//?連線的點(diǎn)
@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);
//?設(shè)置在縮放動(dòng)畫過程中也顯示overlay,默認(rèn)為不繪制
mapView.setDrawOverlayWhenZooming(true);
//?RouteLine?routeLine?=
//?(RouteLine)getIntent().getSerializableExtra("routeLine");
//這里畫點(diǎn)和連接線
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);//設(shè)置一個(gè)起點(diǎn)
}
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();
//?經(jīng)度轉(zhuǎn)像素
projection.toPixels(gpoint1,?p1);
projection.toPixels(gpoint2,?p2);
projection.toPixels(gpoint3,?p3);
//第一個(gè)畫筆?畫圓
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);
//第二個(gè)畫筆?畫線
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);
// 創(chuàng)建一張空白圖片
baseBitmap = Bitmap.createBitmap(480, 640, Bitmap.Config.ARGB_8888);
// 創(chuàng)建一張畫布
canvas = new Canvas(baseBitmap);
// 畫布背景為灰色
canvas.drawColor(Color.GRAY);
// 創(chuàng)建畫筆
paint = new Paint();
// 畫筆顏色為紅色
paint.setColor(Color.RED);
// 寬度5個(gè)像素
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:
// 獲取手按下時(shí)的坐標(biāo)
startX = (int) event.getX();
startY = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
// 獲取手移動(dòng)后的坐標(biāo)
int stopX = (int) event.getX();
int stopY = (int) event.getY();
// 在開始和結(jié)束坐標(biāo)間畫一條線
canvas.drawLine(startX, startY, stopX, stopY, paint);
// 實(shí)時(shí)更新開始坐標(biāo)
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();
// 模擬一個(gè)廣播,通知系統(tǒng)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里面(當(dāng)系統(tǒng)提供的控件不能滿足你的開發(fā)需求,需要自己寫 控件,也就是自定義view)。
path類的lineTo方法:lineTo(float x, float y) ,該方法實(shí)現(xiàn)的僅僅是兩點(diǎn)連成一線的繪制線路。
path類的quadTo方法:quadTo(float x1, float y1, float x2, float y2),該方法的實(shí)現(xiàn)是當(dāng)我們不僅僅是畫一條線甚至是畫弧線時(shí)會(huì)形成平滑的曲線,該曲線又稱為"貝塞爾曲線"(Bezier curve),其中,x1,y1為控制點(diǎn)的坐標(biāo)值,x2,y2為終點(diǎn)的坐標(biāo)值)。
主要區(qū)別(效果):對(duì)比quadTo方法繪制的線,lineTo繪制的線在彎曲部分很明顯的不能形成平滑的彎曲,會(huì)出現(xiàn)明顯的兩點(diǎn)形成一線的突痕。
當(dāng)前題目:android畫線,畫線條安卓
網(wǎng)頁網(wǎng)址:http://m.rwnh.cn/article32/dsdhjsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、面包屑導(dǎo)航、企業(yè)建站、云服務(wù)器、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)