简体   繁体   中英

Android - scroll activity background image while scrolling listview

I want to create an activity with a listview and a background image that is automatically moved vertically when the user scrolls down the listview (the image should scroll slower than the list view), something like yahoo weather app.

I was wondering if there is an existing code/library that does this well (good code wrapping, setting the image bigger that the screen, improvements on usage so that OutOfMemory should't happen etc).

Ok, so i managed to find a way to do this. I don't know how good it performs on more weaker devices.

Layout:

... 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
    ...
> 

Setting background image: * the 1.4 constant is just for testing, the image should have a 1/2 ration (with / height), also the image width should be equal to the screen width

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

    }

Where EBitmapUtils is:

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

And background scrolling programmatically on list scroll (listComponent is a custom widget that actually wraps a 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);
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM