gpt4 book ai didi

android - View 中简单的简单二维图形

转载 作者:行者123 更新时间:2023-11-29 22:32:37 25 4
gpt4 key购买 nike

在 View 上绘制像素、线条和圆圈的最简单方法是什么?我想四处移动十字光标,所以没有什么特别密集的。

我想我可以扩展 SurfaceView 并将它添加到 XML,它会正常工作,但它只是显示为黑色,但是,当我在 eclipse 中查看 localmap.xml 的布局 View 时,图形显示如预期。

有什么想法吗?我的 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);
}

最佳答案

使用 SurfaceView,您不需要在 onDraw() 中进行绘图。您必须从底层表面抓取一 block Canvas 并在其中绘制。在我看来,您并不真正知道为什么要使用 SurfaceView。只需使用普通 View ,onDraw() 就可以正常工作。

关于android - View 中简单的简单二维图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3484779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com