How to get width and height of displayed image in android? - android

This question may seem trivial and been asked many times, but I couldn't find an answer that will work for me.
I have an ImageView and GLSurfaceView that is drawn on top of ImageView when I push a button.
For my ImageView I pass a Bitmap and scale it down in order to avoid an OutOfMemoryError exception. My ImageView is aligned to other elements in my UI, so it stretches the image to fill width/height (this is what I want).
When I change from ImageView to GLSurfaceView I get black regions around my image that used to be transparent in my ImageView, because GLSurfaceView makes a hole in UI and works differently than usual elements it draws background of my image to black and I don't need it, because I have custom background.
I came down with a solution to set LayoutParams for GLSurfaceView programmatically and I use Bitmap width and height, but I end up with a smaller image. I need the exact width and height of the image displayed - not the ImageView because it has transparent regions.
PS: I'm not an OpenGL expert and if you have a solution that works for OpenGL please share it.
a)
_______________
| |
| |
|---------------| <-
| transparent |
|---------------| <- Entire
| | ImageView
| Image | need
| | only this
|---------------| <-
| transparent |
|---------------| <-
| |
---------------
b)
_______________
| |
| |
|---------------| <-
| black |
|---------------|
| |
| Image | GLSurfaceView
| |
|---------------|
| black |
|---------------| <-
| |
---------------
My ImageView and GLSurfaceView:
<android.opengl.GLSurfaceView
android:id="#+id/glsvImageHolder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/horScrollImage"
android:layout_marginBottom="5dp"
android:layout_marginTop="50dp"
android:visibility="invisible" />
<ImageView
android:id="#+id/ivImageHolder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/horScrollImage"
android:layout_marginBottom="5dp"
android:layout_marginTop="50dp"
android:visibility="visible" />

float widthPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM,
pxToMm(ivImageHolder.getWidth(), context),
getResources().getDisplayMetrics());
float heightPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM,
pxToMm(ivImageHolder.getHeight(), context),
getResources().getDisplayMetrics());
ViewGroup.LayoutParams layoutParams= glsvImageHolder.getLayoutParams();
layoutParams.width= (int) widthPx;
layoutParams.height= (int) heightPx;
glsvImageHolder.setLayoutParams(layoutParams);
Pixel to MM
public static float pxToMm(final float px, final Context context)
{
final DisplayMetrics dm = context.getResources().getDisplayMetrics();
return px / TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 1, dm);
}
Make GLSurfaceView' s width and height : wrap_content.

Related

Create an image from two other

My problem is that i have one ImageView with my main image (fill the screen) and then i create another one ImageView which is smaller one, dragable and the image has transparent background.
I would like then after the positioning of the smaller ImageView to be able to create one image from this two as i see it on screen. My recent tries i have manage something like this but i have problems with the second image scaling,transparent background and positioning.
[update]
Here is my code. Using this i achieve retaining transparent background but the positioning of the image is wrong i cannot get the correct current position of the position of the second image view. Also, the second image scaling/quality is really bad.
Bitmap cameraImage = ((BitmapDrawable) photoView.getDrawable()).getBitmap();
Bitmap myIcon = ((BitmapDrawable) ( temp).getDrawable()).getBitmap();
myIcon= myIcon.copy(Bitmap.Config.ARGB_8888,true);
cameraImage =cameraImage.copy(Bitmap.Config.ARGB_8888,true);
Canvas canvas = new Canvas(cameraImage);
Rect r=canvas.getClipBounds();
r.left=0;
r.top=0;
r.bottom=r.bottom/2;
r.right=r.right/2;
canvas.drawBitmap(myIcon,null,r,null);
photoView.setImageBitmap(cameraImage);
[update]
The frame is my base Image View and the stars is the second Image View. When the second Image View has a final position lets say the position on the example, I would like to make a bitmap containing these two images. (maintaining transparent background to the second one)
-----------------
| |
| *** |
| *** |
| *** |
| |
| |
| |
| |
-----------------
Thanks,
Adamos
I used this code snippet which takes the screenshot from the parent view of the two images views.
parentView.buildDrawingCache();
Bitmap cache = Bitmap.createBitmap(parentView.getDrawingCache());
photoView.setImageBitmap(cache);
parentView.destroyDrawingCache();
Thanks #hypd09.

Android: while screen in landscape, recalculate the size of image

I am new to android and currently have project on hand. I did search some solution from Google and StackOverflow. I really have no clue how to do it.
In the project i did specify the size of holder image for display well during listview rendering image. Besides that i did calculate the size of item in listview in order to expand the listview in scrollView. But when it comes to landscape, i want the size that i specified to be recalculate, so the child of listview (such as textView) can expand. And the image I specified in portrait can expand (full screen) in landscape as well.
Please help! What steps i need to take for re-calculating it? or is there any other solution?
Below is the example of my layout, the size of image in landscape is same as in portrait ,i.e., as i specified it. So what are the functions that i can call once i change the view mode? Thanks.
___________________
|...................| ________________________________
|. .| | .................... |
|.Image Full Screen.| | . . |
|. .| | . Image could not . |
|...................| | . full screen . |
| | | .................... |
| Portrait mode | | |
| | | Land scape mode |
| | | |
| | |________________________________|
|___________________|

VideoView not working for video with rotation on LG Android phones

With LG MS840, if I take a video in lanscape orientation, then display it in a VideoView, it looks same to what it used to be.
However if a take a video in portait orientation, then display it in VideoView, the video's width and height will be swapped and the image will be scaled to fit that swapped size.
For example, if an image in the video I took is:
-------
| A|
| |
| |
| |
|B C|
-------
The image in the recorded file still is:
------------
| A C |
| |
| B |
------------
Since when the video size is reported the rotation has been considered, what I saw is:
-------
|A C|
| |
| |
| |
| B|
-------
Any clue how this could be fixed?
VideoView does not support rotation of video even if composition matrix is set correctly and rotation attribute is used.
What you can do is to use TextureView and set its attribute rotation="90" (for example). It then will rotate the frames but the aspect ratio is something that you need to handle your self. In order to do so you can use textTureView.setScaleX((screenHeight * 1.0f) / screenWidth)
More details are here: Rotating an android VideoView

custom indeterminate progress drawable scaling

Trying to use a custom indeterminate drawable that will use vertical scaling. It comes out looking strange, it seems as though the bottom row of pixels are the only ones scaled, so it comes out looking like this.
I want it to be vertically scaled so it looks like a traditional candy-stripe indeterminate progress bar. The reason I need it to be scalable is that the area it will be covering will be weighted, so I can't have a static size.
Setting the minimum height seems to change nothing. Changing the drawables to 9-patch screws up the tiling (it turns into one png covering the whole area instead of horizontally tiling. In the end, I need horizontal tiling, and vertical scaling.
Anyone have an idea for me?
EDIT:
Reading up, I see in ProgressBar, that they wrap all your bitmaps like so:
final Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();
if (mSampleTile == null) {
mSampleTile = tileBitmap;
}
final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
final BitmapShader bitmapShader = new BitmapShader(tileBitmap,
Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
shapeDrawable.getPaint().setShader(bitmapShader);
return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT,
ClipDrawable.HORIZONTAL) : shapeDrawable;
In otherwords, the force your bitmap to tile with Clamp, instead of respecting your original tile setting. I am sure there is a reason for this that I don't know. The only think I can think of is to write my own progress bar class that does my own tiling.
probably you should use pattern. see documentation drawable-resourse please look to the
"XML Bitmap" section of the document. there is a tileMode="repeat" i think this will solve you problem.
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#[package:]drawable/drawable_resource"
android:antialias=["true" | "false"]
android:dither=["true" | "false"]
android:filter=["true" | "false"]
android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
"fill_vertical" | "center_horizontal" | "fill_horizontal" |
"center" | "fill" | "clip_vertical" | "clip_horizontal"]
android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />

Combining SurfaceView with other Views such as TextView and Buttons

I'm totally new to android programming (just did some tutorials/read the dev guides etc.) and as every newbie I want to do a useless game :-).
Currently I'm struggling with the layout of different views. Similar to the example I've made a class which extends a SurfaceView and put that into a FrameLayout. Around this SurfaceView I want to have other View's like Buttons & TextViews.
Something like this:
-----------------------------------------------
| TextView | SurfaceView | TextView |
| | | |
------------ ------------
| | | TextView |
| | | |
| | ------------
| | | TextView |
| | | |
| | ------------
| | | |
| | | |
-----------------------------------------------
| Button Button |
-----------------------------------------------
I've managed to do something like this with a FrameLayout and RelativeLayouts (sticking the TextViews at the edges of the screen) but I'd like to better control the size of the SurfaceView as it should be a multiple in width and height of the object(s) I'll be drawing in it. I've tried setting layout_width and layout_height to some dp values but when I start painting at 0,0 it's still at the very top-left corner (where the TextView is..).
So, what's the best practice to achieve a layout as above? Using what layout? Or should I better draw my text inside the draw() function of my SurfaceView instead?
Layouts are the right way to combine OpengGL content with buttons, labels and other type of view objects. You can combine layouts inside other layouts... so you may build your screen step by step combining Linear Layouts or whatever you prefer.
For example, you can use a Relative layout to setup the buttons (b1, b2) and the rest of the screen:
+-----------+
| Zone 1 |
+-----+-----+
| b1 | b2 |
+-----+-----+
Then inside Zone 1, you can use a Horizontal Linear Layout for the 3 main columns
+----+----+----+
| c1 | c2 | c3 |
+----+----+----+
Inside of c2 you can place the SurfaceView, and in c1 the text label
Inside c3 will be a new Vertical Linear layout to display the text labels.
+----+
| t1 |
+----+
| t2 |
+----+
| t3 |
+----+

Categories

Resources