[android-developers] Re: How to save a canvas to disk
Thanks! After experiementing, seems to work. I think all I need to do is to do the view.draw(canvas) prior to have the canvas filled with what's drawn on screen. I am still exactly sure what's happening though, as basically I just do everything as it is, and create a new bitmap and a new canvas associate with it, and prior to saving, pass the new canvas to the view. About view.draw(canvas): http://developer.android.com/reference/android/view/View.html#draw(android.graphics.Canvas) Anyway, I am happy! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: How to save a canvas to disk
Hi, That is why I gave you a complete working example ( maybe try it yourself ). But not knowing your code I could only guess about when you would want to call the saveView() method I provided. I guess that you would call the saveView() method when the user requests that your KidsPaintView view should save their 'drawing' either to a file or to set as the wallpaper. I imagine that you would provide a menu option button, or some touchscreen or keyboard button to allow the user to trigger that action - when they had finished making their 'drawing'. I just used the trackball/dpad center button as an example to demonstrate triggering and saving the view. In my example I used 'SomeView' to represent your KidsPaintView and put the saveView() method in the Activity, but you could also put the saveView() method or some equivalent code inside your KidsPaintView view if you wish. You should also realize that you CAN just call draw( canvas ) on your KidsPaintView as your class extends View so it (automatically) inherits the View.draw( canvas ) method... You just need to ensure that you create and use a new canvas and a new bitmap when calling the draw( canvas) method, as I said originally and demonstrated with the code posted. Good luck ! Regards On Sep 25, 12:45 am, limtc thyech...@gmail.com wrote: Thanks! I think the part that I am confused is that I never used view.draw (canvas) code in my program so I don't know when to call it... at this moment, says I wanted to clear a screen, this is my code in the view: public void clearScreen() { gradient = false; dots.clear(); invalidate(); // this will call the onDraw and pass in the canvas } so I replace invalidate() by draw(canvas)? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: How to save a canvas to disk
Hi, I made up the following to demonstrate what I meant. In this example when you press the trackball/dpad center button the view is written to a png file and also set as the wallpaper. You can change it to save as jpeg file etc. Hope that helps. Regards / package com.testSaveView; import android.app.Activity; import android.os.Bundle; import java.io.FileOutputStream; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.Log; import android.view.KeyEvent; import android.view.View; public class testSaveView extends Activity { SomeView sv = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sv = new SomeView(this); setContentView( sv ); } @Override public boolean onKeyDown( int keyCode, KeyEvent event) { switch( event.getKeyCode() ) { case KeyEvent.KEYCODE_DPAD_CENTER: if ( sv != null ) { saveView( sv ); return true; } default: } return super.onKeyDown( keyCode, event ); } private void saveView( View view ) { Bitmap b = Bitmap.createBitmap( view.getWidth(), view.getHeight (), Bitmap.Config.ARGB_); Canvas c = new Canvas( b ); view.draw( c ); FileOutputStream fos = null; try { fos = new FileOutputStream( /sdcard/some_view_image_ + System.currentTimeMillis() + .png ); if ( fos != null ) { b.compress(Bitmap.CompressFormat.PNG, 100, fos ); fos.close(); } setWallpaper( b ); } catch( Exception e ) { Log.e(testSaveView, Exception: + e.toString() ); } } class SomeView extends View { public SomeView( Context context ) { super( context ); } public void onDraw( Canvas canvas ) { canvas.drawARGB(0x80, 0xff, 0, 0 ); Paint paint = new Paint(); paint.setColor(Color.BLUE); paint.setTextSize( 48 ); canvas.drawText(...Some view..., 10, canvas.getHeight() / 2, paint); } } } // the manifest - ?xml version=1.0 encoding=utf-8? manifest xmlns:android=http://schemas.android.com/apk/res/android; package=com.testSaveView android:versionCode=1 android:versionName=1.0 application android:icon=@drawable/icon android:label=@string/ app_name activity android:name=.testSaveView android:label=@string/app_name intent-filter action android:name=android.intent.action.MAIN / category android:name=android.intent.category.LAUNCHER / /intent-filter /activity /application uses-sdk android:minSdkVersion=3 / uses-permission android:name=android.permission.SET_WALLPAPER/ uses-permission /manifest On Sep 23, 7:12 pm, limtc thyech...@gmail.com wrote: Mmm... I don't quite understand. The link you sent is the documentation for View? If I have a new canvas, what happened to the canvas in onDraw? And when should I call view.draw(canvas)? Do you have any sample codes? Appreciated. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: How to save a canvas to disk
Thanks! I think the part that I am confused is that I never used view.draw (canvas) code in my program so I don't know when to call it... at this moment, says I wanted to clear a screen, this is my code in the view: public void clearScreen() { gradient = false; dots.clear(); invalidate(); // this will call the onDraw and pass in the canvas } so I replace invalidate() by draw(canvas)? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: How to save a canvas to disk
Hi, Have a look at - http://developer.android.com/reference/android/view/View.html#draw(android.graphics.Canvas) - create a new bitmap and a new canvas, associate the bitmap with this canvas and call view.draw( your_new_canvas ) - using your KidsPaintView view instance - then save the bitmap or use it to set the wallpaper. Regards On Sep 23, 1:03 pm, limtc thyech...@gmail.com wrote: Hi, I am doing a painting program (KIds Paint - you can find in Android Market) and I have a lot of requests to save the content on disk or to wallpaper. I have been searching around but cannot find solution. My guess is that I probably wanted to get the bitmap from the canvas, but I can't find ways to get it. Then I try to set an empty bitmap into the canvas and draw on the canvas, and save the bitmap... but I got an empty bitmap. Please help! Thanks. Here's my codes: public class KidsPaintView extends View { Bitmap bitmap = null; ... protected void onDraw(Canvas canvas) { if (bitmap == null) { bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_); canvas.setBitmap(bitmap); } ... // do painting on canvas } } Then in my main code I try to retrieve the bitmap and save it as wallpaper: Bitmap bitmap = view.bitmap; try { setWallpaper(bitmap); } catch (IOException e) { e.printStackTrace(); } But all I got is a black wallpaper. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---