好的,所以我设法找到了一种方法来做到这一点。我不知道它在更弱的设备上表现如何。
布局:
... inside a RelativeLayout
...
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="none"
>
<ScrollView
android:id="@+id/act_background_scrollview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollY="70dp"
android:fillViewport="false"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="none"
>
<ImageView
android:id="@+id/act_background_wallpaper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerInside" >
</ImageView>
</ScrollView>
</HorizontalScrollView>
...
<ListView
...
>
设置背景图片:
* 1.4 常数只是为了测试,图像应该有 1/2 比例(有 / 高度),并且图像宽度应该等于屏幕宽度
public static Map<Integer, BitmapDrawable> imageMap = new HashMap<Integer, BitmapDrawable>();
protected void setBackgroundResource(int imgRes, int viewRes) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = (int)(size.x * 1.4);
int height = (int)(size.y * 1.4);
BitmapDrawable dr = imageMap.get(imgRes);
if (dr == null) {
dr = EBitmapUtils.getDrawable(getResources(), imgRes, width, height);
imageMap.put(imgRes, dr);
}
((ImageView)findViewById(R.id.act_background_wallpaper))
.setImageDrawable(dr);
}
EBitmapUtils 在哪里:
public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
int width = image.getWidth();
int height = image.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}
public static BitmapDrawable getDrawable(Resources res, int imgRes
, int width, int height){
Bitmap bm = BitmapFactory.decodeResource(res, imgRes);
Bitmap bmResized = getResizedBitmap(bm, height, width);
bm.recycle();
return new BitmapDrawable(res, bmResized);
}
并且在列表滚动时以编程方式进行背景滚动(listComponent 是一个实际包装 ListView 的自定义小部件):
backgroundScroll = (ScrollView)findViewById(R.id.act_background_scrollview);
listComponent.setCompositeOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (listComponent.getListView().getChildAt(0) == null)
return;
View c = listComponent.getListView().getChildAt(0);
int top = -c.getTop() + (listComponent.getListView().getFirstVisiblePosition() -1)
* c.getHeight();
backgroundScroll.scrollTo(0, top / 3);
}
});