1
什麼是在視圖上繪製像素,線條和圓的最簡單方法? 我想移動十字光標,所以沒有特別的密集。我認爲我可以擴展SurfaceView並將其添加到XML,它只會工作,但它只是顯示爲黑色,但是,當我在eclipse中查看localmap.xml的佈局視圖時,圖形按預期顯示。視圖中的簡單2D圖形
任何想法?我的onDraw永遠不會在模擬器上調用,甚至在類上調用invalidate沒有任何區別。我會繼續嘗試,但任何人都可以看到我錯過的任何東西?或者完全有更好的方法嗎?
- 弗林克
localmap.xml包含以下(在RelativeLayout的)
<com.example.android.game.LocalMapView
android:id="@+id/localmap_map"
android:layout_width="fill_parent"
android:layout_above="@id/localmap_planettext"
android:layout_below="@id/header"/>
LocalMapView.java包含以下(除其他事項外)
public class LocalMapView extends SurfaceView {
Paint mPaint = new Paint();
//Construct a LocalMapView based on inflation from XML
public LocalMapView(Context context, AttributeSet attrs) {
super(context, attrs);
// allow the map to receive the focus
setFocusable(true);
}
private void drawPixel(Canvas canvas, int x, int y, int colour) {
mPaint.setColor(colour);
if ((x >= MAP_MIN_X) && (x < MAP_MAX_X) && (y >= MAP_MIN_Y) && (y < MAP_MAX_Y)) {
canvas.drawPoint(((float)x * mScaleMapToScreenX), ((float)y * mScaleMapToScreenY), mPaint);
}
}
private void drawCircle(Canvas canvas, int x, int y, int radius, int colour) {
mPaint.setColor(colour);
canvas.drawCircle(((float)x), ((float)y), ((float)radius), mPaint);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawCircle(canvas, MAP_MAX_X/2, MAP_MAX_Y/2, 1, 0xFF00FFFF);
drawPixel(canvas, MAP_MAX_X/2, MAP_MAX_Y/2, 0xFF000000);
}
我想我一定在某個方面有點困惑。 LunarLander示例使用SurfaceView,所以我必須複製它。謝謝, - Frink – FrinkTheBrave 2010-08-15 07:11:12