【问题标题】:Android - scroll activity background image while scrolling listviewAndroid - 滚动列表视图时滚动活动背景图像
【发布时间】:2014-06-30 20:48:07
【问题描述】:

我想创建一个带有列表视图和背景图像的活动,当用户向下滚动列表视图(图像滚动速度应该比列表视图慢)时,它会自动垂直移动,类似于雅虎天气应用程序。

我想知道是否有一个现有的代码/库可以很好地做到这一点(良好的代码包装、将图像设置为大于屏幕、改进使用以防止 OutOfMemory 发生等)。

【问题讨论】:

  • 您正在寻找的是 Parallax,谷歌搜索出现了几个选项,其中一个是 github.com/nirhart/ParallaxScroll
  • 不适合我。这是一个很好的库,但我不知道如何将它与活动背景图像一起使用。

标签: android listview scroll background-image


【解决方案1】:

好的,所以我设法找到了一种方法来做到这一点。我不知道它在更弱的设备上表现如何。

布局:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多