【发布时间】:2011-10-30 20:50:37
【问题描述】:
我想在屏幕上显示图像,但图像边缘会大于屏幕本身的尺寸。 IE 如果最大屏幕宽度为 X 则我希望图像的宽度为 X +100,最大屏幕高度为 Y 则图像高度为 Y +100。
我希望该选项能够将图像向右/向左移动,并且图像仍将显示在整个屏幕上。
【问题讨论】:
我想在屏幕上显示图像,但图像边缘会大于屏幕本身的尺寸。 IE 如果最大屏幕宽度为 X 则我希望图像的宽度为 X +100,最大屏幕高度为 Y 则图像高度为 Y +100。
我希望该选项能够将图像向右/向左移动,并且图像仍将显示在整个屏幕上。
【问题讨论】:
为此,我使用RelativeLayout 和requestLayout 函数:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
并在代码中生成合适的图片:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
// example code with bigger dimensions than the screen
Bitmap b = Bitmap.createBitmap(width + 100, height + 100, Bitmap.Config.ARGB_8888);
// draw smth on the bitmap
Canvas c = new Canvas(b);
Paint p = new Paint();
p.setColor(Color.RED);
c.drawCircle(b.getWidth() / 2,
b.getHeight() / 2,
Math.min(b.getWidth(), b.getHeight()) / 2,
p);
// set the image
ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setImageBitmap(b);
// call this method to change imageview size
iv.requestLayout();
【讨论】:
d = getResource().getDrawable(R.drawable.image) 和iv.setImageDrawable(d)。