Drawing application with resize when orientation is changed - android

I'm sorry to disturb, but at this point, i'm really stuck.
I'm trying to make an activity to draw on a picture. But i've no idea how to handle the rotation of the device.
If i change the bitmap with createScaledBitmap, that works but the draws are not in the right place. (The bitmap is bigger and added space)
I need to stretch the bitmap for the new size (in a way that draws are on the right place on the picture).
Everytime my configuration change, i do that :
snapshotCanvas.mTempBitmap = Bitmap.createScaledBitmap(snapshotCanvas.mOriginalBitmap.copy(snapshotCanvas.mOriginalBitmap.getConfig(), true), drawView1.getWidth(), drawView1.getHeight(), false);
snapshotCanvas.mCanvas.setBitmap(snapshotCanvas.mTempBitmap);
snapshotCanvas.mCanvas.scale((float) snapshotCanvas.mCanvas.getWidth() / drawView.getWidth(),
(float) snapshotCanvas.mCanvas.getHeight() / drawView.getHeight());
I tried many things but i got terribly lost.
Thanks in advance.

Related

How to make the area of camera preview be half of SurfaceView in ContinuousCaptureActivity

I want to make the area of camera preview be half of SurfaceView, so I modify the code of ContinuousCaptureActivity.
The detail is as follows:
Use GLES20.glViewport(0, 0, viewWidth / 2, viewHeight / 2); to replace https://github.com/google/grafika/blob/master/src/com/android/grafika/ContinuousCaptureActivity.java#L436
But the result is strange, see the picture below.
I really can not understand it and what is the right way to do it?
Who can give me some advices?
My first thought would be that you need a glClear() in there (with the viewport set to cover the entire surface) since you're no longer filling the entire surface with the blit. Otherwise you get uninitialized data, and on a tile-based architecture things can get strange.
Fiddling with the viewport isn't really the right way to go. Just draw a smaller rectangle. Use Sprite2d and change the X/Y scale factors. See TextureFromCameraActivity for an example -- you can scale the rect, zoom in, rotate, etc.

Cutting out the centre region of a Bitmap and shrinking, in Android

I'm trying to dynamically create images in android by taking an existing Bitmap and removing the centre of it in order to make a "cropped" version. The resulting image's height would naturally be smaller than the original, something like the attached example.
I've got a rough way of doing this by creating two new Bitmaps from the original, one containing the top of the image above the crop section (e.g. the android's head in the example) and the other containing the remaining image below the crop section (the android's feet) using the Bitmap.createBitmap(source, x, y, width, height) method, then drawing both of these bitmaps onto a canvas of a size equal to the original image minus the removed space.
This feels a bit clunky, and as I could be calling this method several times a second, it seems wasteful to create two bitmaps each time.
I was wondering if there was a more efficient way of doing this. Something like drawing the original Bitmap onto a canvas using a Path with it's Paint's xfermode set to a
new PorterDuffXfermode(Mode.DST_OUT) in order to cut out the portion of the image I wish to delete. But this seems to clear that area and not shrink the image down i.e. it leaves a big empty gap in the Android's middle.
Any suggestions greatly appreciated!
Why do you create two bitmaps? You only need to create one bitmap and then do canvas.drawBitmap() twice.
Bitmap bmpOriginal;
Bitmap bmpDerived = Bitmap.create(...);
Canvas canvas = new Canvas(bmpDerived);
canvas.drawBitmap(bmpOriginal, rectTopSrc, rectTopDst, null);
canvas.drawBitmap(bmpOriginal, rectBottomSrc, rectBottomDst, null);
Done.

Image/Bitmap placement?

Have any of you seen the simulator bong apps in android? How would you make that app? For example I have tried doing it using xml layouts. I divided the picture up and placed it in order and in segments so that when a particular picture is pressed a function will occur. Problem is that on different phones the size of the collective pictures either extends off the screen or is compressed.
I have also tried using surfaceViews with bitmaps so that each individual picture can take the size of the screen and adjust them accordingly. I am having trouble making the pictures not plop on top of eachother and instead be placed in order going down the screen. I can get the first 2 just fine designating the Rect dst with the starting height being where the other ended but the third and fourth aren't showing up on the screen. For example I used
Rect dst = new Rect(0, 0, mWidth, mHeight/7 );
For the first image which is on the top. Then for the second image
Rect dst = new Rect(0, mHeight/7, mWidth, mHeight/3 );
(mHeight and mWidth is the canvas width and height)
Not sure how to go about placing the next 2 pictures beneath them and eachother. I tried
Rect dst = new Rect(0, mHeight/7+(mHeight/3), mWidth, mHeight/7);
That didn't work though. It honestly looks really simple to do and I'm sure it is, but I just cant seem to get the images to work the way I want. Is there a way to leave the image as a whole and specify when a certain spot is selected it does something (that would work on every phone the same way).
Any help with this would be greatly appreciated. Any other ideas for how to get the result I want are also very much appreciated. Thanks for taking time to read this at all.
Well you could divide the screen into regions like left,top,width,height.
Eg.
Region 1 (0,50,100,100)
Region 2 (100,50,100,100).
And add a OnTouchListener and use that to get touch coords and check which region it belongs to. I'm not sure if it a good way but it is doable.

android (Advanced)Saving a Bitmap Canvas with DrawText but textsize calculation needed

I have a Canvas that i draw text on.
(this is quite advanced i think hope you can follow me)
See attached image below.
The functionality is that I can tap on the screen to change the textsize.
Tap left screen = smaller Font.
Tap right Screen = bigger Font.
I can then also move the text on the screen.
When textsize is ok and i have moved the text where i want it,
Then I want to save it back to the original Bitmap.
I use options.inSampleSize = 4; to initially load the Bitmap
The ImageView that have the Bitmap is of course smaller then the original Image.
Some kind of calculation is needed.
This tends to be quite difficult to do.
I have the options.inSampleSize = 4 Bitmaps Ratio.
It's 0.59, 0.69 something depending on Landscape or portrait.
Im playing around with that to somehow change the new BitmapsetTextSize
to look the same as the ImageView smaller Bitmap.
What could i do here?
I have a feeling that since one never know what size an image have.
I have to somehow scale/constrain the Loaded Bitmap Ratio to a fixed Ratio.
Then i need to using percentage or something to transfer the text location
to the bigger image.
I can kind of do that when it comes to initial
(red small ball on picture) location. Hence, the starting point of the text.
But i dont know how long the text is so im stuck so so speak and asking for advice
One way i tried was to divide paint.getTextSize() with the Ratio something like 0.59. That looked like a solution at first. But the image ratio is not fixed and the Font size is not fixed something else is needed.
Here are two pictures showing the problem.
On phone Bitmap:
The saved new Bitmap:
I'm not 100% clear that I understand what you mean, but here's a go. It sounds like you were close to the right approach. Instead of using a fixed ratio, you need to calculate the ratio that the image is scaled by to fit in the view on the phone, then you can scale the text by the inverse ratio. So in steps:
Measure the width of the original image (height would do just as well, but we just need one dimension)
Measure the width of the scaled image
Calculate ratio (ratio = original / scaled)
Let the user type their text
You can then get the text size using something like: float paintSize = paint.getTextSize();
For rendering on the final image, use paint.setTextSize(paintSize / ratio);.

Dynamically create / draw images to put in android view

I'm not sure I'm doing this the "right" way, so I'm open to other options as well. Here's what I'm trying to accomplish:
I want a view which contains a graph. The graph should be dynamically created by the app itself. The graph should be zoom-able, and will probably start out larger than the screen (800x600 or so)
I'm planning on starting out simple, just a scatter plot. Eventually, I want a scatter plot with a fit line and error bars with axis that stay on the screen while the graph is zoomed ... so that probably means three images overlaid with zoom functions tied together.
I've already built a view that can take a drawable, can use focused pinch-zoom and drag, can auto-scale images, can switch images dynamically, and takes images larger than the screen. Tying the images together shouldn't be an issue.
I can't, however, figure out how to dynamically draw simple images.
For instance: Do I get a BitMap object and draw on it pixel by pixel? I wanted to work with some of the ShapeDrawables, but it seems they can only draw a shape onto a canvas ... how then do I get a bitmap of all those shapes into my view? Or alternately, do I have to dynamically redraw /all/ of the image I want to portray in the "onDraw" routine of my view every time it moves or zooms?
I think the "perfect" solution would be to use the ShapeDrawable (or something like it to draw lines and label them) to draw the axis with the onDraw method of the view ... keep them current and at the right level ... then overlay a pre-produced image of the data points / fit curve / etc that can be zoomed and moved. That should be possible with white set to an alpha on the graph image.
PS: The graph image shouldn't actually /change/ while on the view. It's just zooming and being dragged. The axis will probably actually change with movement. So pre-producing the graph before (or immediately upon) entering the view would be optimal. But I've also noticed that scaling works really well with vector images ... which also sounds appropriate (rather than a bitmap?).
So I'm looking for some general guidance. Tried reading up on the BitMap, ShapeDrawable, Drawable, etc classes and just can't seem to find the right fit. That makes me think I'm barking up the wrong tree and someone with some more experience can point me in the right direction. Hopefully I didn't waste my time building the zoom-able view I put together yesterday :).
First off, it is never a waste of time writing code if you learned something from it. :-)
There is unfortunately still no support for drawing vector images in Android. So bitmap is what you get.
I think the bit you are missing is that you can create a Canvas any time you want to draw on a bitmap. You don't have to wait for onDraw to give you one.
So at some point (from onCreate, when data changes etc), create your own Bitmap of whatever size you want.
Here is some psuedo code (not tested)
Bitmap mGraph;
void init() {
// look at Bitmap.Config to determine config type
mGraph = new Bitmap(width, height, config);
Canvas c = new Canvas(mybits);
// use Canvas draw routines to draw your graph
}
// Then in onDraw you can draw to the on screen Canvas from your bitmap.
protected void onDraw(Canvas canvas) {
Rect dstRect = new Rect(0,0,viewWidth, viewHeight);
Rect sourceRect = new Rect();
// do something creative here to pick the source rect from your graph bitmap
// based on zoom and pan
sourceRect.set(10,10,100,100);
// draw to the screen
canvas.drawBitmap(mGraph, sourceRect, dstRect, graphPaint);
}
Hope that helps a bit.

Categories

Resources