Question

How can I extract a Bitmap object out of my onDraw() routine in my CustomView?

Here is my code:

public class DrawView extends View {
    private Paint paint = new Paint();
    private Point point;
    private LinkedList<Point> listaPontos;
    private static Context context;

    class Point {

        public Point(float x, float y) {
            this.x = x;
            this.y = y;
        }

        float x = 0;
        float y = 0;
    }

    public DrawView(Context context) {
        super(context);
        this.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
        this.context = context;
        paint.setColor(Color.YELLOW);
        this.listaPontos = new LinkedList<Point>();
    }

    @Override
    public void onDraw(Canvas canvas) {

        if(listaPontos.size() != 0){
            for(Point point : listaPontos){
                canvas.drawCircle(point.x,  point.y, 25, paint);    
            }
        }
        calculateAmount(canvas);        
    }

    private void calculateAmount(Canvas canvas) {
        LinkedList<Integer> colors = new LinkedList<Integer>();
        for(int i = 0 ; i != canvas.getWidth(); i++)
        {
            for(int j = 0; j != canvas.getHeight(); j++){

                int color = BITMAP.getPixel(i,j);  //How can I get the bitmap generated on onDraw ?

                colors.add(color);
            }
        }

        int yellow = 0;
        int white = 0;

        for(Integer cor : colors) {

            if(cor == Color.WHITE) {
                white++;
            }
            if(cor == Color.YELLOW) {
                yellow++;
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                    listaPontos.add(new Point(event.getX(), event.getY()));
                    break;
        }
        invalidate();
        return true;
    }
}

Thanks alot in advance ;)

EDIT: The bitmap thing is to calculate each pixel color, how can I add a background image to my DrawView ? I tested this.setBackgroundResource(R.drawable.a); at constructor but didn't work, thanks again ;)

Was it helpful?

Solution

There is no way to extract a Bitmap out of a Canvas. At least not directly.

However, it is possible to draw on a Bitmap with the Canvas and then use the Bitmap.

Bitmap mDrawBitmap;
Canvas mBitmapCanvas;
Paint drawPaint = new Paint();

@Override
public void onDraw(Canvas canvas) {

    drawPaint.setColor(Color.RED);

    if (mDrawBitmap == null) {
        mDrawBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        mBitmapCanvas = new Canvas(mDrawBitmap);
    }

    // clear previously drawn stuff
    mBitmapCanvas.drawColor(Color.WHITE);

    // draw on the btimapCanvas
    mBitmapCanvas.drawStuff(...);
    //... and more

    // after drawing with the bitmapcanvas,
    //all drawn information is stored in the Bitmap    


    // draw everything to the screen
    canvas.drawBitmap(mDrawBitmap, 0, 0, drawPaint);
}

After the onDraw() method has finished, all drawn information will be drawn on the screen (by calling canvas.drawBitmap(...), and also be stored in your Bitmap object (because all draw operations have been done on the Canvas that was created with the Bitmap).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top