Imageview位置未按预期运行



I'使用全屏并锁定到纵向。清单中:

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

在布局中,我有一个图像视图和两个按钮:

<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);

在我的两个模拟器上,这都在屏幕顶部显示了预期的图像。

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

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。

为什么它们不同?

我可以向上滚动图像:

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

如何找到此值?没有这样的工作:

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

如有任何帮助,我们将不胜感激。

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

在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);

为了稍微简化代码。

最新更新