save custom view to png or similar


Last Updated:

  1. Raffe

    Raffe New Member This Topic's Starter

    Joined:
    Nov 30, 2009
    Messages:
    4
    Likes Received:
    0
    Hello,

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

    Code (Text):
    1.  
    2. import android.content.Context;
    3. import android.graphics.Canvas;
    4. import android.graphics.Color;
    5. import android.graphics.Paint;
    6. import android.graphics.Path;
    7. import android.util.AttributeSet;
    8. import android.view.View;
    9.  
    10. public class BitmapGraf extends View {
    11.  
    12.     private Paint grafFarg;
    13.     private static Path grafPath = null;
    14.    
    15.     public BitmapGraf(Context context, AttributeSet attrs) {
    16.         super(context, attrs);
    17.        
    18.         grafFarg = new Paint();
    19.         grafFarg.setAntiAlias(true);
    20.         grafFarg.setColor(Color.RED);
    21.     }
    22.  
    23.     public static void skickaPath(Path nyPath) {
    24.         grafPath = new Path();
    25.         grafPath = nyPath;
    26.     }
    27.    
    28.     @Override
    29.     public void onDraw(Canvas canvas) {
    30.         if (grafPath != null) {
    31.             canvas.drawPath(grafPath, grafFarg);
    32.         }
    33.     }
    34. }
    35.  
    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 (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent">
    6.    
    7.     <Button android:id="@+id/ButtonOppna"
    8.         android:text="@string/button_tidigare"
    9.         android:layout_height="40dip"
    10.         android:layout_x="5dip"
    11.         android:layout_y="225dip"
    12.         android:layout_width="170dip"
    13.         />
    14.     <Button android:id="@+id/ButtonSkicka"
    15.         android:text="@string/button_skicka"
    16.         android:layout_height="40dip"
    17.         android:layout_width="100dip"
    18.         android:layout_x="375dip"
    19.         android:layout_y="225dip"
    20.         />
    21.  
    22.     <HorizontalScrollView android:id="@+id/HScrollView"
    23.         android:layout_x="5dip"
    24.         android:layout_y="10dip"
    25.         android:layout_width="470dip"
    26.         android:layout_height="210dip">
    27.        
    28.         <se.doconnet.android.andning.BitmapGraf
    29.             android:layout_width="640dip"
    30.             android:layout_height="205dip"
    31.             android:background="@drawable/graf"
    32.             />
    33.          
    34.     </HorizontalScrollView>
    35.    
    36. </AbsoluteLayout>
    37.  
    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
     

    Advertisement
  2. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    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 (Text):
    1.  
    2. Bitmap myBitmap = Bitmap.createBitmap(width, height, config)  ;
    3. Canvas myCanvas = new Canvas(b);
    4.  
    Do all your drawing to that canvas.

    Code (Text):
    1.  
    2. myCanvas.drawLine(startX, startY, stopX, stopY, paint)
    3. etc...
    4.  
    And your onDraw() method will have to draw your bitmap to the supplied canvas

    Code (Text):
    1.  
    2.     public void onDraw(Canvas canvas) {
    3.         if (grafPath != null) {
    4.  
    5.             // Draw to my own private canvas object
    6.             myCanvas.drawPath(grafPath, grafFarg);
    7.         }
    8.  
    9.         // Draw my private bitmap to the supplied canvas
    10.         canvas.drawBitmap(myBitmap, left, top, grafFarg) ;
    11.     }
    12.  
    You can then generate a JPEG/PNG from the Bitmap object you've been drawing to.

    I hope that's of some help.

    Mark
     
  3. Raffe

    Raffe New Member This Topic's Starter

    Joined:
    Nov 30, 2009
    Messages:
    4
    Likes Received:
    0
    Hi, thanks for your reply... Hopefully I can get this to work now :)
     

Share This Page

Loading...