2012-01-12 68 views
6

我正在嘗試開發一個簡單的地圖應用程序,它將在屏幕上顯示地圖。如何在Android上通過ImageView繪製線條?

當用戶在屏幕上移動光標時,我想在我的地圖上顯示2條垂直線。我曾嘗試過很多例子來獲得這個想法,但不幸的是,它並沒有成功。 我該如何做到這一點?

作爲前一個問題here我曾試過。但沒有得到答案。任何人都可以引導我?

我的main.xml是如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
    <ImageView android:id="@+id/main_imagemap" 
     android:src="@drawable/worldmap" 
     android:clickable="true" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 
    </LinearLayout> 

</LinearLayout> 

而且我的活動文件(我剛剛開始吧..)

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.ImageView; 

public class LineMapActivity extends Activity 
{ 


    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     ImageView map_image = (ImageView)findViewById(R.id.main_imagemap); 

    } 
} 

而且在該鏈接時,我也有加入MyImageView 。

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.widget.ImageView; 

public class MyImageView extends ImageView 
{ 

    public MyImageView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     // TODO Auto-generated method stub 
     Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); 
     canvas.drawLine(0, 0, 20, 20, p); 
     super.onDraw(canvas); 
    } 
} 

現在我該如何將MyImageView添加到我的應用程序?

+0

更多的細節?看看這個教程http://codemagician.wordpress.com/2010/05/06/android-google-mapview-tutorial-done-right/ – MikeIsrael 2012-01-12 07:50:21

+0

沒有我親愛的..我只是一個新的蜜蜂在andriod ..我是隻需添加drawable的圖像即可。我只是想研究如何在圖像上方畫線。就像在舊的黑莓手機 – 2012-01-12 07:58:35

+0

好吧,檢查我的答案,它應該得到你想要的結果。 – MikeIsrael 2012-01-12 08:21:06

回答

4

最後我想出了一個解決方案。這是一個簡單的程序,它在圖像上繪製一條線。

這裏是我的main.xml文件(com.ImageDraw.MyImageView是我的自定義視圖,下面的代碼):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
    <com.ImageDraw.MyImageView android:id="@+id/main_imagemap" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 
    </LinearLayout> 
</LinearLayout> 

這裏是主要活動:

import android.app.Activity; 
import android.os.Bundle; 

public class MyActivity extends Activity 
{ 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
} 

,這是我的自定義圖像視圖(MyImageView):

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

public class MyImageView extends SurfaceView implements SurfaceHolder.Callback 
{ 
    private CanvasThread canvasthread; 

    public MyImageView(Context context) { 
     super(context); 
     getHolder().addCallback(this); 
     canvasthread = new CanvasThread(getHolder(), this); 
     setFocusable(true); 
    } 

    public MyImageView(Context context, AttributeSet attrs) 
    { 
     super(context,attrs); 
     getHolder().addCallback(this); 
     canvasthread = new CanvasThread(getHolder(), this); 
     setFocusable(true); 
    } 

    protected void onDraw(Canvas canvas) { 
     Log.d("ondraw", "ondraw"); 
     Paint p = new Paint(); 
     Bitmap mapImg = BitmapFactory.decodeResource(getResources(), R.drawable.mybitmap); 
     canvas.drawColor(Color.BLACK); 
     canvas.drawBitmap(mapImg, 0, 0, null); 
     p.setColor(Color.RED); 
     canvas.drawLine(0, 0, 100, 100, p); 
    } 

    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
      int height) { 
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
     canvasthread.setRunning(true); 
     canvasthread.start(); 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
     boolean retry = true; 
     canvasthread.setRunning(false); 
     while (retry) 
     { 
      try 
      { 
       canvasthread.join(); 
       retry = false; 
      } 
      catch (InterruptedException e) { 
       // TODO: handle exception 
      } 
     } 
    } 
} 

最後這裏是我的CanvasThread:

import android.graphics.Canvas; 
import android.view.SurfaceHolder; 

public class CanvasThread extends Thread 
{ 
    private SurfaceHolder surfaceHolder; 
    private MyImageView myImageView; 
    private boolean run = false; 

    public CanvasThread(SurfaceHolder s, MyImageView m) 
    { 
     surfaceHolder = s; 
     myImageView = m; 
    } 

    public void setRunning(boolean r) 
    { 
     run = r; 
    } 

    public void run() 
    { 
     Canvas c; 
     while(run) 
     { 
      c=null; 
      try 
      { 
       c= surfaceHolder.lockCanvas(null); 
       synchronized (surfaceHolder) { 
        myImageView.onDraw(c); 
       } 
      } 
      finally 
      { 
       if(c!=null) 
       { 
        surfaceHolder.unlockCanvasAndPost(c); 
       } 
      } 
     } 
    } 
} 

您可你爲什麼不使用的MapView和覆蓋發現這tutorial

+2

特別感謝MikeIsrael,他幫了我很多...... 並且還去那個網站。 .. – 2012-01-13 10:00:20

+1

看起來不錯。如果稍後發現您希望保留原始圖像,則始終可以使圖像視圖與myimageview重疊。祝你好運! – MikeIsrael 2012-01-15 08:54:05

+0

你可以在xml中重疊一個relativelayout和align屬性,只要搜索一下你就會發現很多信息。然後,只能在頂部的圖像視圖上繪製,並且如果需要,可以將底部留作擦除。 – MikeIsrael 2012-01-16 09:59:29

1

您正在嘗試將ImageView對象轉換爲MyImageView對象時出現錯誤。

+0

然後我需要如何實現MyImageView,這是我在問題中給出的鏈接? 或者你可以顯示我可以在imageview上方畫線嗎? – 2012-01-12 08:11:35

+0

請貼上完整的日誌。 – jeet 2012-01-12 08:38:52

+0

親愛的我已經更新了我的問題。希望你能明白我的要求。我再一次描述它,我想要一個顯示圖像作爲背景的應用程序。也可以畫線。所有這些代碼都是我的實驗。不確定這些代碼的工作.. – 2012-01-12 08:45:56

1

只是修正XML,包括代替,像這樣一個ImageView的你的對象(注意,com.your.package.MyImageView應該包含您的類的包全包名稱,然後在類名)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
    <com.your.package.MyImageView android:id="@+id/main_imagemap" 
     android:src="@drawable/worldmap" 
     android:clickable="true" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 
    </LinearLayout> 

</LinearLayout> 
+0

我有我的包作爲包com.mymap; 因此我修改我的XML爲 2012-01-12 08:26:34

+0

你可以發佈logcat的錯誤? – MikeIsrael 2012-01-12 08:54:21

+0

這並沒有什麼幫助,你需要粘貼整個堆棧跟蹤來找出錯誤的位置。 – MikeIsrael 2012-01-12 09:05:42