Dynamically create / draw images to put in android view

后端 未结 1 1950
灰色年华
灰色年华 2021-01-20 18:34

I\'m not sure I\'m doing this the \"right\" way, so I\'m open to other options as well. Here\'s what I\'m trying to accomplish:

I want a view which contains a graph.

相关标签:
1条回答
  • 2021-01-20 19:33

    First off, it is never a waste of time writing code if you learned something from it. :-)

    There is unfortunately still no support for drawing vector images in Android. So bitmap is what you get.

    I think the bit you are missing is that you can create a Canvas any time you want to draw on a bitmap. You don't have to wait for onDraw to give you one.

    So at some point (from onCreate, when data changes etc), create your own Bitmap of whatever size you want.

    Here is some psuedo code (not tested)

    Bitmap mGraph;
    
    void init() {
        // look at Bitmap.Config to determine config type
        mGraph = new Bitmap(width, height, config);
        Canvas c = new Canvas(mybits);
        // use Canvas draw routines to draw your graph
    }
    
    // Then in onDraw you can draw to the on screen Canvas from your bitmap.
    protected void onDraw(Canvas canvas) {
        Rect dstRect = new Rect(0,0,viewWidth, viewHeight);
        Rect sourceRect = new Rect();
        // do something creative here to pick the source rect from your graph bitmap
        // based on zoom and pan 
        sourceRect.set(10,10,100,100);
    
        // draw to the screen
        canvas.drawBitmap(mGraph, sourceRect, dstRect, graphPaint);
    }
    

    Hope that helps a bit.

    0 讨论(0)
提交回复
热议问题