Gem*_*low 4 android background android-listview android-listfragment
我将图像设置为Listview背景,如果我想用项目滚动它,我该怎么办?
例如:1是背景,如果我向下滚动Listview,它将改变
1
-----1-----1--------
1 1
-1-------------1----
Run Code Online (Sandbox Code Playgroud)
至
--------1----------
1 1
---1----------1----
1 1
Run Code Online (Sandbox Code Playgroud)
也许我可以扩展listview并覆盖dispatchDraw,但如果我使用listFragment,我该怎么办?有人帮帮我吗?
在Activity的XML文件中定义listview,如:
(根据您的要求在此xml文件中定义属性)
<com.example.MyCustomListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Run Code Online (Sandbox Code Playgroud)
创建一个名为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);
}
}
Run Code Online (Sandbox Code Playgroud)
希望这会解决你的问题:)
| 归档时间: |
|
| 查看次数: |
1118 次 |
| 最近记录: |