Imageview位置不符合预期

时间:2012-01-14 07:12:45

标签: android

我使用全屏并锁定到肖像。在清单中:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
android:screenOrientation="portrait"

在布局中我有一个imageview和两个按钮:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"/>
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />
<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />
</LinearLayout>

一开始我将自己的图片加载到imageview中:

imageView = (ImageView) findViewById(R.id.imageView1); //from .xml 
Drawable newPhoneImage;
newPhoneImage = getResources().getDrawable(R.drawable.phone_on);
imageView.setImageDrawable(newPhoneImage);

在我的两个模拟器上,它会按预期在屏幕顶部显示图像。

然后我绘制一个位图并替换Image:

sBitmap = Bitmap.createBitmap(557, 580,Config.ARGB_8888);
//draw the bimap image
BitmapDrawable nimage;
nimage = new BitmapDrawable(sBitmap);
imageView.setImageDrawable(nimage); 

在大型仿真器(WVGA800 400x800)上,图像位于顶部,符合预期。

在小型模拟器(QVGA 240x320)和手机(240x320)上,图像从顶部向下35像素。

为什么他们不同?

我可以向上滚动图像:

imageView.scrollTo(0, 35); //this will put image at top

我怎样才能找到这个值?这样的事情都不会起作用:

int offset = imageView.getTop();// it just = height of image

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

这是因为你的图像对于屏幕来说太大了。

在XML中设置android:adjustViewBounds="true"可能有所帮助。

此外,您可以尝试获取屏幕宽度并使用它:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
Bitmap.createBitmap(width, width/557*580,Config.ARGB_8888);

创建正确尺寸的图像。尝试同时设置这两件事。

另一方面,您可以:

imageView = (ImageView) findViewById(R.id.imageView1); //from .xml 
imageView.setImageResource(R.drawable.phone_on);

稍微简化一下代码。