2013-06-18 91 views
0

我想從float[]中的點中劃出一條線。我知道這點得到了drawLines()功能,因爲我在這裏記錄他們:爲什麼我的(自定義視圖)畫布上沒有繪製線條?

畫點: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.55554 581.8182 32.0 581.8182 32.0 398.54544 354.66663 398.54544 :354.66663:154.18182:784.8889

* 注意:不支付注意重複點 - 第一行的終點是第二行的起點。 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" 
    /> 

結果: 灰色塊出現在手機屏幕上,但沒有紅線繪製。

爲什麼沒有畫我的(自定義Viewcanvas

回答

1

刪除此行:

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

您將要覆蓋傳遞給你的onDraw方法與屏外(位圖支持)一個帆布實例,所以沒有什麼是在屏幕上的情節。

相關問題