在Android的图像尺寸 [英] Size of Image in Android

查看:168
本文介绍了在Android的图像尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关特定的图像蓝色背景上说,我可以得到它的宽度和高度的Andr​​oid。我可以用ImageView的在屏幕上显示。即使我把图片的背景为透明,我可以在屏幕上显示。但仍然,如果我尝试以编程方式找出图像的宽度或高度,我仍然得到相同previous一步。我想实现的是有没有什么办法,通过它我可以得到图像减去透明背景的确切高度或宽度。

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.

也许我是新来整场比赛,这就是为什么没有得到合适的词来恩preSS我。但希望有人会明白的。

Probably I 'm new to whole game that's why not getting right words to express me. But hope someone will understand.

感谢

推荐答案

你是如何建立的ImageView?在XML?和你是如何试图检索图像的宽度和高度?比方说,你正在使用XML,而像这样:

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"
    />

您的ImageView将大小,因为它包含的图像相同。所以在code,则既可以检查ImageView的,或它包含的图像的宽度和高度,并获得相同的号码。但是,如果你正在做这样的事情:

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"
    />

图片可能会比ImageView的小,因此要获得准确的结果,你可以尝试类似如下:

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.

编辑:如果您的实际图像大于它的内容时,用透明背景,你实际上应该裁剪图像至其在照片编辑程序的内容。

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.

这篇关于在Android的图像尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆