For a particular Image say on blue background, I can get its Width and Height in android. I can display it on screen using ImageView. Even If I keep the background of Image as transparent I can display it on screen. But still if I try to find out the width or height of image programmatically , I still get the same as in previous step. What I wanted to achieve Is there any way through which I can get the exact height or width of image minus transparent background.
I want to do this because I 'm trying to implement a drag/move gesture. But now the Image size is more due to transparent background I can o开发者_开发问答nly move it to few portion only.
Probably I 'm new to whole game that's why not getting right words to express me. But hope someone will understand.
Thanks
How are you setting up the ImageView? In XML? And how are you trying to retrieve the width and height of the image? Let's say you're using XML, and something like this:
<ImageView
android:id="@+id/my_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/my_image"
/>
Your ImageView will be the same size as the image it contains. So in code, you could either check the width and height of the ImageView, or the image it contains and get the same numbers. However, if you're doing something like this:
<ImageView
android:id="@+id/my_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/my_image"
android:scaleType="centerInside"
/>
The image may be smaller than the ImageView, so to get an accurate result, you can try something like the following:
ImageView iv = (ImageView)findViewById(R.id.my_image);
Rect rect = iv.getDrawable().getBounds();
int height = rect.height();
int width = rect.width();
There might be a simpler method, but this one should work.
EDIT: If your actual image is larger than its contents, with a transparent background, you should actually crop the image down to its contents in a photo editing program.
精彩评论