2010-05-28 255 views
3

我正在嘗試查找有關如何更改畫布座標系的信息。我有一些矢量數據要使用圓形和直線等繪製到畫布上,但數據的座標系與畫布座標系不匹配。Android畫布座標系

有沒有辦法我使用到屏幕的單位單元映射?

我畫到沒有佔用整個顯示器的ImageView的。

如果我必須做我自己的計算每個繪圖調用之前,如何找到我的ImageView的寬度和高度?

我試過的getWidth()和getHeight()調用似乎是返回整個畫布大小,而不是ImageView的大小沒有幫助。

我看到一些東西矩陣,是東西,會爲我工作嗎?

我試過使用「public void scale(float sx,float sy)」,但它的作用更像是一個像素級縮放,而不是通過擴展每個像素的矢量縮放函數。這意味着如果尺寸增加以適應屏幕,線條厚度也會增加。


更新:

經過一番研究,我開始認爲有沒有辦法改變座標系到別的東西。我需要將所有座標映射到屏幕的像素座標,並通過修改每個矢量來完成。 getWidth()和getHeight()現在似乎對我更好。我可以說出了什麼問題,但我懷疑我不能在構造函數中使用這些方法。

+0

是的,getWidth()和getHeight()在構造函數中不起作用。 – Mitch 2010-06-13 00:48:11

回答

0

我知道在Android上執行自定義矢量圖形的唯一方法是將所有內容都繪製成圖像文件,然後將其放入ImageView。我不確定我是否確切地理解您的要求,但如果唯一的問題是縮放,ImageView會將使用android:scaleType="center"作爲ImageView的屬性的任何圖像縮放爲其自己的尺寸。

至於改變的座標系統,我懷疑是可能的(雖然我還沒有研究它)。不過,編寫一個將數據系統映射到標準Android座標系的函數可能相當簡單。

2

感謝您的回覆。我幾乎沒有放棄以我認爲應該的方式工作。當然,我認爲事情應該發生的不是他們如何發生。 :)

這裏的基本上它是如何工作的,但似乎在某些情況下,像素爲關,而圈似乎丟失部分時,事情在一些邊界條件,我有土地沒有搞清楚。就我個人而言,我認爲這是不能接受的內部應用程序代碼,應該在Android庫中......如果你爲谷歌工作,眨眼,微調。 :)

private class LinearMapCanvas 
{ 
    private final Canvas canvas_; // hold a wrapper to the actual canvas. 

    // scaling and translating values: 
    private double scale_; 

    private int translateX_; 
    private int translateY_; 

// linear mapping from my coordinate system to the display's: 
    private double mapX(final double x) 
    { 
    final double result = translateX_ + scale_*x; 
    return result; 
    } 

    private double mapY(final double y) 
    { 
     final double result = translateY_ - scale_*y; 
     return result; 
    } 

    public LinearMapCanvas(final Canvas canvas) 
    { 
     canvas_ = canvas; 

// Find the extents of your drawing coordinates: 
     final double minX = extentArray_[0]; 
    final double minY = extentArray_[1]; 
    final double maxX = extentArray_[2]; 
    final double maxY = extentArray_[3]; 

// deltas: 
    final double dx = maxX - minX; 
    final double dy = maxY - minY; 

// The display's available pixels, accounting for margins and pen stroke width: 
    final int width = width_ - strokeWidth_ - 2*margin_; 
    final int height = height_ - strokeWidth_ - 2*margin_; 

    final double scaleX = width/dx; 
    final double scaleY = height/dy; 

    scale_ = Math.min(scaleX , scaleY); // Pick the worst case, so the drawing fits 

// Translate so the image is centered: 
     translateX_ = (int)((width_ - (int)(scale_*dx))/2.0 - scale_*minX); 
     translateY_ = (int)((height_ - (int)(scale_*dy))/2.0 + scale_*maxY); 
    } 

// wrappers around the canvas functions you use. These are only two of many that would need to be wrapped. Annoying and error prone, but beats any alternative I can find. 
    public void drawCircle(final float cx, final float cy, final float radius, final Paint paint) 
    { 
    canvas_.drawCircle((float)mapX(cx), (float)mapY(cy), (float)(scale_*radius), paint); 
    } 

    public void drawLine(final float startX, final float startY, final float stopX, final float stopY, final Paint paint) 
    { 
    canvas_.drawLine((float)mapX(startX), (float)mapY(startY), (float)mapX(stopX), (float)mapY(stopY), paint); 
    } 
... 
} 
+0

請原諒我的代碼格式。在我發佈之前,代碼看起來很棒,但是在StackOverflow似乎做了一些瘋狂的事情之後。 – Mitch 2010-06-13 00:49:41

0

可以使用畫布矩陣的preScale()方法擴展畫布座標到你自己的單位。請注意,這也會縮放Paint的筆畫寬度,這可能不是您想要的。