0

我正在尝试从 a 中的点画一条线float[]。我知道这些点到达了drawLines()函数,因为我在这里记录它们:

Draw Points: : 154.18182 : 784.8889 : 215.27272 : 677.3333 : 215.27272 : 677.3333 : 337.45453 : 462.22217 : 337.45453 : 462.22217 : 276.36365 : 569.7778 : 276.36365 : 569.7778 : 398.54544 : 354.66663 : 398.54544 : 354.66663 : 154.18182 : 784.8889 : 154.18182 : 784.8889 : 337.45453 : 462.22217:337.45453:462.22217:520.7273:139.55554:520.7273:139.555554:581.8182:32.0:32.0:32.0:581.8182:32.0

*注意:不要注意点的重复——第一行的终点就是第二行的起点。drawLines 函数需要四个连续的点,因此需要重复点。

我的widthheight有效integersWidth = 672 Height = 968

我的onDraw功能:

请注意,我已经尝试了一些现在被注释掉的东西。我的背景是灰色的,所以这Color.RED条线在绘制时应该是可见的。

@Override
public void onDraw(Canvas canvas){
    //setWillNotDraw(true);

    Log.d(TAG, "DRAW DAMNIT!!!");
    Log.d(TAG, "Width = " + (int) width + " Height = " + (int)height);

    paint = new Paint();
    paint.setStyle(Style.STROKE);
    paint.setStrokeWidth(4);
    paint.setColor(Color.RED);
    //paint.setAntiAlias(true);
    //paint.setShadowLayer(4, 2, 2, 0x81000000);

    Bitmap bitmap = Bitmap.createBitmap((int)width, (int)height, Bitmap.Config.ARGB_8888);
    canvas = new Canvas(bitmap);

    String drawPointsGo = "";
    float[] drawPoints = new float[points.size()];
    for(int i=0; i<points.size(); i++){
        Float f = points.get(i);
        drawPoints[i] = (float) (f != null ? f : 0.0);
        drawPointsGo = drawPointsGo + " : " + drawPoints[i];
    }
    Log.d(TAG, "Draw Points: " + drawPointsGo);

    canvas.drawLines(drawPoints, paint);
}

我的 XML:

<za.co.widge.test.linegraph.LineGraphView 
    android:id="@+id/linechart"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:background="#444"
    android:padding="16dp"
    android:layout_weight="1"
    />

结果: 手机屏幕上出现灰色块,但没有画出红线。

为什么我的 (custom View)上什么也没画canvas

4

1 回答 1

1

删除此行:

Bitmap bitmap = Bitmap.createBitmap((int)width, (int)height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);

您正在使用屏幕外(支持位图)覆盖传递给 onDraw 方法的画布实例,因此屏幕上没有任何内容。

于 2013-06-20T08:44:24.077 回答