can anybody tell me how to refresh or
reset a bitmap or canvas to draw
another image on it?
actually i have a map binary file which contains bitmap tiles in bytes now problem is i have a buffer image with buffer image i get screen image to display. so when i move my screen image i need to refresh my buffer image and draw new tiles to display on screen.
here is code to display image.
bitmap = Bitmap.createBitmap(screenWidth * (int)mapState.getiBufferMult(), screenHeight * (int)mapState.getiBufferMult(), Config.RGB_565);
canvas = new Canvas(bitmap);
image = new ImageView(this);
this is not full source but i hope you guys can have idea.
thanks a lot
If you generate the bitmap that you want, you can say image.setImageBitmap(yourBitmap);
The Bitmap class also offers methods to modify an existing bitmap or replace just part of it.
If you are using the same bitmap and modifying it, call invalidate () on the ImageView when you have the bitmap ready.
Related
I have some SVGs in my assets folder and I need to dynamically set them in my widget (on an ImageView).
I am using this library: http://code.google.com/p/svg-android/
This library returns a Picture or a PictureDrawable.
The only methods I can see to use on RemoteViews are setImageViewBitmap which obviously takes a bitmap.
I tried looking for code to convert a Drawable to a Bitmap like this:
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
currentBitmap = bitmap;
But the bitmap is too small. When I create the bitmap in Illustrator I set the artboard size to 65 which is what comes through on the intrinsic width/height.
My widgets can be resized so the ImageView sizes are variable. Even if I set the width and height statically to some large number like this...
Bitmap bitmap = Bitmap.createBitmap(300, 300, Config.ARGB_8888);
then the resulting bitmap just has a bunch of whitespace below and to the right of a tiny image.
I guess I need to somehow draw the picture at a scaled up value as well as creating the Bitmap at size 300. Ideally I could figure out the size of the ImageView at runtime and set the proper sized Bitmap if I knew that. Is this the best approach and how would I do this? Perhaps there is a better approach I don't even know about?
I've not used android-svg but if it's using vanilla PictureDrawables, then it should be just a matter of not using the intrinsic bounds.
Try the following:
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(300, 300, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
pictureDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
pictureDrawable.draw(canvas); // do not access the Picture directly, that defeats the purpose
currentBitmap = bitmap;
In short, use the Drawable, not its Picture and set the Drawable's bounds to be the full canvas.
Have you tried createScaledBitmap?
createScaledBitmap()
I have tried svg-android and it has not worked for me for this very reason. Not to mention its severely limited feature set.
The point of using vector graphics is that I can generate images of any appropriate size to fit the UI View size. Which means the generation method must accept the size requirements at run-time, and not always use width,height declared in <svg> tag.
Hence I used the native implementation: libsvg-android, which exactly does that.
It directly renders to a canvas with given size:
long objId = SvgRaster.svgAndroidCreate();
SvgRaster.svgAndroidParseBuffer(objId, readString(mInputStream, "UTF-8"));
SvgRaster.svgAndroidSetAntialiasing(objId, true);
SvgRaster.svgAndroidRenderToArea(objId, mCanvas, 0, 0, mWidth, mHeight);
I ended up modifying the underlying Artboard size to be 300. None of the scaling up methods worked. So the final code I used was that which I originally posted:
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
currentBitmap = bitmap;
Once I had a 300x300 Bitmap I was able to set it into the RemoteViews using setImageViewBitmap .
This kind of defeated the purpose of using SVGs in the first place (at least as far as using them in widgets was concerned).
My problem was not the same as User117. Perhaps he did not name the outer layer 'bounds' as was required in the library. Either way, I managed to get the library working, albeit by having to modify the SVG Artboard size.
Hopefully with the increase in screen resolution Android will introduce SVGs as part of the platform soon.
I know - the title may sounds strange. Let me explain:
I created an image to show you, what I'm talking about:
I got an image (Bitmap (1)), that as the size of 150w/200h.
Now I need to make the bitmap bigger ((2) 400w/400h), but the original image must have the same size. So that the image is embedded in white background.
I think one way to solve it is this:
* create a big bitmap
* create a canvas for it
* draw the original bitmap on the canvas
* draw the canvas
* generate a bitmap of the canvas
The problem for me is, that it must be done in a background thread without drawing a view.
I hope you understand me.
You can use the code bellow to achive it. Where smallBitmap is your original image and bigBitmap is the final image:
Bitmap bigBitmap = Bitmap.createBitmap(width, height , Bitmap.Config.ARGB_8888);
canvas = new Canvas(bigBitmap);
canvas.drawBitmap(smallBitmap, left, top, new Paint());
Regards.
This should do the trick.
Create a thread and in that thread object:
Create a new bitmap.
Create a canvas based on that bitmap.
Draw your bitmap to that canvas
and voila!
I hope this helps.
I am trying to create a bitmap from a View that is not displayed yet to set it as a texture with OpenGL in android.
The problem is that when I create my bitmap the layout parameters of my View have 0 value cause the onLayout() function has not been called yet.
Is there a way to "simulate" a layout or do a "background layout" to get the right layout params ?
I was thinking about a Frame layout having two views, the background one would be used to create my bitmap as the layout would have been done.
Thanks.
You indeed need to layout your View before drawing it into a Bitmap. Simply call
myView.measure(...);
then
myView.layout(0, 0, myView.getMeasuredWidth(), myView.getMeasuredHeight());
I have posted an example on how to do this in one of the following presentations: http://www.curious-creature.org/2010/12/02/android-graphics-animations-and-tips-tricks/
So, some edits after the question was clarified :)
I suggest you create the bitmap independently of the view, then scale it to the same size as the view once the view is created. Create an arbitrary size bitmap that allows you to render what you want to it ..
// This in some class that renders your bitmap independently, and can be
// queried to get the bitmap ...
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_4444);
// render something to your bitmap here
Then after your view is created, take the precreated bitmap and rescale it to the right size (I've not actually checked this by compiling it -- might be errors):
Rect original = new Rect(0, 0, 100, 100);
RectF destination = new RectF(0.0, 0.0, (float)myView.getWidth(), (float)myView.getHeight());
Canvas canvas = new Canvas();
canvas.drawBitmap(myRenderingClass.b, original, destination, null);
myView.draw(canvas);
I want to create a bitmap / image which has many images like "Collage" which has more then one images in a single picture.
I have stored all my images in a grid view but now i want to create a single image from all those images. And even i want to make few images click able
so what can be the road map to do this ? any sort of help / example will be helpful.
reference image
Bitmap pic1 = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);
Bitmap pic2 = BitmapFactory.decodeResource(getResources(), R.drawable.pic2);
Bitmap bg= BitmapFactory.decodeResource(getResources(), R.drawable.background);
Bitmap out1 = Bitmap.createBitmap(bg) ;
Canvas comboImage = new Canvas(out1);
comboImage.drawBitmap(pic1, 10f, 20f, null);
comboImage.drawBitmap(pic2, 30f, 40f, null);
out1 will have pic1 & pic2, with a background image bg.
To create a single image from multiple images look at using Canvas. You can put bitmaps (and drawables) on a canvas. You can commit the changes and then push then to a single bitmap that you can then use. As far as making certain sections clickable after making it one single image, I will leave this up to someone else to explain, I am not worked directly with the ontouch() functions.
Is it possible to manipulate images using canvas? How do we get the image onto the canvas?
#Override
protected void onDraw(Canvas canvas) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Bitmap mBitmap = bitmap.copy(bitmap.getConfig(), true);
canvas = new Canvas(mBitmap);
Matrix matrix = new Matrix();
canvas.drawBitmap(mBitmap, matrix, mPaint);
}
I'm unable to see the image on the screen. canvas.drawBitmap() shouldn't be necessary since I'm using the constructor and passing mBitmap.
Yes you can, but you don't "get the image onto the Canvas," a Canvas is just an interface to draw onto a Bitmap. To do so, simply create a Canvas and give a reference to your mutable Bitmap:
Canvas c = new Canvas(myBitmap);
Very easy :)
You shouldn't do this in your onDraw method. Try to create an ImageView that should display the image and then set the bitmap via setImageBitmap(bitmap).
In general you are doing somethings wrong in your code.
Loading an image every time something should be drawn to the screen will slow down your application a lot. Try to load the image only once and then just edit and set it to the imageview.
You are assigning a new Canvas to the parameter you got. But this won't change anything on the screen. Think of it like somebody puts a paper in front of you and says:"Please write your message on the paper". Now you take another blank paper and write on it. But the other person only knows about his paper and will use this paper to read your message.