Size of Image in Android

可紊 提交于 2019-12-24 17:19:08

问题


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 only 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


回答1:


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.



来源:https://stackoverflow.com/questions/4367961/size-of-image-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!