• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps save custom view to png or similar

Raffe

Lurker
Hello,

I'm working on an android application where I've created a custom view like this:

Code:
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

public class BitmapGraf extends View {

    private Paint grafFarg;
    private static Path grafPath = null;
    
    public BitmapGraf(Context context, AttributeSet attrs) {
        super(context, attrs);
        
        grafFarg = new Paint();
        grafFarg.setAntiAlias(true);
        grafFarg.setColor(Color.RED);
    }

    public static void skickaPath(Path nyPath) {
        grafPath = new Path();
        grafPath = nyPath;
    }
    
    @Override
    public void onDraw(Canvas canvas) {
        if (grafPath != null) {
            canvas.drawPath(grafPath, grafFarg);
        }
    }
}
It's basicly just draws a path on the screen which i get thru the constructor. It's a graph of some values I get from an XML.

I insert the view inside a HorizontalScrollView in my layout like this:

Code:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <Button android:id="@+id/ButtonOppna"
        android:text="@string/button_tidigare"
        android:layout_height="40dip"
        android:layout_x="5dip"
        android:layout_y="225dip"
        android:layout_width="170dip"
        />
    <Button android:id="@+id/ButtonSkicka"
        android:text="@string/button_skicka"
        android:layout_height="40dip"
        android:layout_width="100dip"
        android:layout_x="375dip"
        android:layout_y="225dip"
        />

    <HorizontalScrollView android:id="@+id/HScrollView"
        android:layout_x="5dip"
        android:layout_y="10dip"
        android:layout_width="470dip"
        android:layout_height="210dip">
        
        <se.doconnet.android.andning.BitmapGraf
            android:layout_width="640dip"
            android:layout_height="205dip"
            android:background="@drawable/graf"
            />
         
    </HorizontalScrollView>
    
</AbsoluteLayout>
Yeah I know I shouldn't be using AbsoluteLayout but I might change that later. ;)

Now I want to be able to save this BitmapGraf with the drawn path and the background and all that to a png or jpg or anything but I can't wrap my head around it.
I've looked on several sites on how to convert views to bitmaps but I can't get it to work...

Anyone who can help me please!? I'm somewhat desperate.

/Raffe
 
Hi Raffe,

I've looked around and can't find anything about generating a bitmap from a View.

But I have a suggestion. You could work the other way around: create a Bitmap first, and then from that generate a Canvas which you draw to.

In your constructor:

Code:
Bitmap myBitmap = Bitmap.createBitmap(width, height, config)  ;
Canvas myCanvas = new Canvas(b);

Do all your drawing to that canvas.

Code:
myCanvas.drawLine(startX, startY, stopX, stopY, paint) 
etc...

And your onDraw() method will have to draw your bitmap to the supplied canvas

Code:
    public void onDraw(Canvas canvas) {
        if (grafPath != null) {

            // Draw to my own private canvas object
            myCanvas.drawPath(grafPath, grafFarg);
        }

        // Draw my private bitmap to the supplied canvas
        canvas.drawBitmap(myBitmap, left, top, grafFarg) ;
    }

You can then generate a JPEG/PNG from the Bitmap object you've been drawing to.

I hope that's of some help.

Mark
 
Back
Top Bottom