ListVIew 项目的位图

Bitmap of ListVIew Item

如何在 ListView 中创建项目的一部分的位图。

我的 ListView 的每个项目都有 4 个组件,2 个 TextView 和 2 个 Button。 单击按钮时,我想创建一个只有 2 个 TextView 的位图。

您需要的是列表适配器。 查看本教程部分“3. 自定义适配器实现”

http://www.vogella.com/tutorials/AndroidListView/article.html

您的项目布局具有以下组件:TextView1、TextView2、Button1、Button2。 您必须将 TextView1 和 TextView2 放在子布局中,以从位图中排除 Button1 + Button2,例如:

<RelativeLayout>
    <!-- main item layout -->
    <RelativeLayout android:id="@+id/relativeLayoutTextViews">
        <!-- child layout for TextViews -->
        <TextView>
        <!-- text view 1 -->
        </TextView>
        <TextView>
        <!-- text view 2 -->
        </TextView>
    </RelativeLayout>
    <Button>
    <!-- Button 1 -->
    </Button>
    <Button>
    <!-- Button 2 -->
    </Button>
</RelativeLayout>

在您的 java 代码中:

    RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativeLayoutTextViews);
if (layout != null) {
    Bitmap image = Bitmap.createBitmap(layout.getWidth(),
            layout.getHeight(), Config.ARGB_8888);
    Canvas b = new Canvas(image);
    Drawable bgDrawable =layout.getBackground();
    if (bgDrawable!=null) 
         bgDrawable.draw(canvas);
    else 
        canvas.drawColor(Color.WHITE);
    layout.draw(b);}
}