how to scroll listview background with item

前端 未结 2 800
情歌与酒
情歌与酒 2021-01-14 01:32

I set a image as Listview background, if I want to scroll it with the item, what can I do?

for example: 1 is the background, if I scroll Listview down, it will chan

2条回答
  •  春和景丽
    2021-01-14 02:11

    In your Activity's XML file define the listview like this ::

    (Define the property in this xml file as per your requirement)

    
    

    Create one Class named MyCustomListView ::

        public class MyCustomListView extends ListView
        {
    
           private Bitmap background;
    
        public MyCustomListView(Context context, AttributeSet attrs) 
        {
            super(context, attrs);
            background = BitmapFactory.decodeResource(getResources(), R.drawable.yourImageName);
        }
    
        @Override
        protected void dispatchDraw(Canvas canvas) 
        {
            int count = getChildCount();
            int top = count > 0 ? getChildAt(0).getTop() : 0;
            int backgroundWidth = background.getWidth();
            int backgroundHeight = background.getHeight();
            int width = getWidth();
            int height = getHeight();
    
            for (int y = top; y < height; y += backgroundHeight)
            {
                for (int x = 0; x < width; x += backgroundWidth)
                {
                    canvas.drawBitmap(background, x, y, null);
                }
            }
            super.dispatchDraw(canvas);
        }
     }
    

    Hope this will solve your problem :)

提交回复
热议问题