我怎么能在网格视图中只画一个项目呢



所有人:

gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//              v.invalidate(0, 0, 5, 5);
                v.invalidate(); //v is a ImageView here
            }
        }); 

我只想画v,但事实是,v高度以下的每一个项目都被重新绘制了,我该怎么办。

public class MyGridView extends GridView 
{
    private Bitmap background;
    public MyGridView(Context context, AttributeSet attrs) 
    {
        super(context, attrs);
        background = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
    }
}
@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

<your.package.name.MyGridView 
    android:id="@+id/mygridview"
    <!-- other GridView configuration parameters -->
/>

相关内容

  • 没有找到相关文章

最新更新