我想创建一个带有列表视图和背景图像的活动,当用户向下滚动列表视图时,该活动会自动垂直移动(图像应该比列表视图滚动得慢),就像雅虎天气应用程序一样。
我想知道是否有一个现有的代码/库可以很好地完成这项工作(良好的代码包装,将图像设置得大于屏幕,改进使用情况以便OutOfMemory应该发生等)。
答案 0 :(得分:0)
好的,所以我设法找到了这样做的方法。我不知道它对更弱的设备有多好。
布局:
... 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);
}
});