如何缩放图像以填满屏幕

发布于 2024-10-02 13:34:19 字数 723 浏览 0 评论 0原文

我的问题是这样的: 我使用 android 模拟器,针对 android 2.2。 我创建了一个自定义视图,我想处理我的应用程序的背景,在这个视图中我只想显示图像,但我希望它填充整个屏幕。

我从“活动”初始化视图,如下所示: 背景视图 bw = 新背景视图(this);

问题: 在我的 HDD 上,我使用的资源图像是 500x375 像素。调用后:

Bitmap m_resourceBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.landscape_1); int nWidth = m_resourceBitmap.getWidth(); int nHeight = m_resourceBitmap.getHeight();

返回的 nWidth 和 nHeight 取决于我放置资源的位置,如果我将其放置在“drawable-mdpi”中,我会得到原始尺寸,否则我会得到其他尺寸。

另一方面,如果我通过以下方式询问视图的宽度和高度 this.getWidth(); this.getHeight();

我得到固定值,但与我期望的设备分辨率不同(我设置了 480x800 设备,得到 320x533)

,最后如果我尝试使用视图尺寸除以图像尺寸来创建比例矩阵,结果会在屏幕上显示,是一个比屏幕小得多的图像...

我的问题是: 我应该问什么问题以及从哪些对象中,以便在视图内我可以绘制资源位图以使其占据整个屏幕?

My issue is this:
I use the android emulator, targeting android 2.2.
I created a custom view which I want to handle the background of my application, in this view I only want to display an image but I want it to fill the entire screen.

I initialize the view from my "Activity" like this:
BackgroundView bw = new BackgroundView(this);

The problem:
On my HDD, the resource image I'm using is 500x375 pixels. After calling:

Bitmap m_resourceBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.landscape_1);
int nWidth = m_resourceBitmap.getWidth();
int nHeight = m_resourceBitmap.getHeight();

the returned nWidth and nHeight depend on where I placed the resource, if i place it in the "drawable-mdpi" I get the original size, otherwise I get other sizes.

On the other hand if I ask the view for it's width and height via
this.getWidth();
this.getHeight();

I get fixed values, but different from the resolution I expected from the device (I set a 480x800 device and I get 320x533)

and finally if I try to create a scale matrix using view dimensions divided by the image dimensions, the result, on screen, is an image much smaller than the screen...

My question is:
What questions should I ask and from which objects so that inside a View I can draw a resource Bitmap so that it occupies the entire screen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月野兔 2024-10-09 13:34:19

不能在布局 XML 文件中完成所有操作吗? (这是我用于菜单背景图像的示例)

<ImageView android:id="@+id/menuBackground"
        android:src="@drawable/imageResource"
        android:adjustViewBounds="true"
        android:gravity="center_vertical"
        android:scaleType="fitXY"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_x="0dp" 
        android:layout_y="0dp" />

然后,如果您需要在运行时更改它,您所要做的就是更改可绘制对象。

Cant you do it all in the layout XML file? (Here's an example that I use for my menu background image)

<ImageView android:id="@+id/menuBackground"
        android:src="@drawable/imageResource"
        android:adjustViewBounds="true"
        android:gravity="center_vertical"
        android:scaleType="fitXY"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_x="0dp" 
        android:layout_y="0dp" />

Then if you need to change it in runtime all you have to do is change the drawable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文