如何获得一个背景图像滚动与And​​roid的ListView控件 [英] How to get a background image scrollable with Android's ListView

查看:210
本文介绍了如何获得一个背景图像滚动与And​​roid的ListView控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个平铺图像,我想作为一个的ListView 的背景下使用。我已经设定 setCacheColorHint(android.R.color.transparent),以正确地查看背景图像,使用 setBackgroundDrawable()

I have a tileable image I would like to use as the background of a ListView. I have already set setCacheColorHint(android.R.color.transparent) in order to view the background image properly, using setBackgroundDrawable().

我的问题是,当我滚动的ListView ,图像停留在原地,太不滚动。我假设这是正常的行为,但我想图像与文本滚动。

My problem is the when I scroll the ListView, the image stays in-place and doesn't scroll too. I'm assuming this is normal behaviour, but I would like the image to scroll with the text.

有没有办法做到这一点,而不诉诸滚动我自己的的ListView 或设定每一个细胞的看法的背景是什么?我不使用XML对于这一点,因为我想这是动态的。

Is there any way to do that without resorting to rolling my own ListView or setting the background of every cell's view? I'm not using XML for this, as I would like this to be dynamic.

推荐答案

创建一个类,并与ListView控件扩展了它:

Create one class and extends it with ListView ::

public class MyCustomListView extends ListView
{
        private Bitmap background;

        public MyCustomListView(Context context, AttributeSet attrs) 
        {
            super(context, attrs);
            background = BitmapFactory.decodeResource(getResources(), R.drawable.yourImage);//yourImage means your listView Background which you want to move
        }

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

现在在XML文件中,需要一个listiview这样的:

//设置其他属性根据您的要求。

Now in your XML File ,take one listiview like this ::
//Set other attributes as per your requirement

 <com.test.MyCustomListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
 </com.test.MyCustomListView>

这篇关于如何获得一个背景图像滚动与And​​roid的ListView控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆