网格视图,允许背景画布在视图外绘制。

4
我正在为Android开发一个小型记事本应用程序。在我的应用程序中,我使用GridView创建了一个2*X大小的网格,在每个单元格中,我有一个LinearLayout称为AbstractNote,它的任务是显示应用程序中每个保存笔记的预览。
我希望我的AbstractNote具有一个背景图像,该图像旋转一些位以更美观地显示预览。但我的问题是,当我旋转图像时,其中一些部分会落在AbstractNote区域之外,我找不到一个好的方法来解决这个问题。
我尝试使用View.setAnimation(...),它可以工作,但给我带来了崎岖不平的图像和文本,并且我尝试创建一个自定义Drawable(设置为AbstractNote.setBackgroundDrawable(...)),它可以给我平滑的图像,但无法显示完整的图像。
请注意,我的目标Android版本是2.1,我不使用View.setRotation(...)。
对于尝试1,使用View.setAnimation进行操作,但得到了崎岖不平的图像和文本: 对于尝试2,使用自定义drawable,但无法绘制单元格之外的内容: 如果有人知道如何修复尝试1中的崎岖不平的图像和文本,或者如何在尝试2中绘制单元格之外的图像,我将非常感激。
1个回答

5

我终于找到了一个解决问题的方案,虽然不是很好但可以工作,想法是直接在GridView上绘制单元格背景而不是在GridView的子项/单元格中绘制背景。

我创建了一个新的GridView:

public class NoteGridView extends GridView {
@Override
protected void dispatchDraw(Canvas canvas) {
    final int count = getChildCount();
    if (count > 0) {
        for (int i = 0; i < this.getChildCount(); i++) {
            // For each of my childen draw it backgrund that allow to draw bg that is bigger then cell.
            View childAt = this.getChildAt(i);
            if (childAt instanceof AbstractNote) {
                final AbstractNote note = (AbstractNote) childAt;
                // Set the zero point to the start of the childen, then call the childen draw methods.
                canvas.save();
                canvas.translate(childAt.getLeft(), childAt.getTop());
                note.drawBackground(canvas);
                canvas.restore();
            }
        }
    }
    super.dispatchDraw(canvas);
}
}

在我的子类AbstractNote中,我添加了一个方法drawBackground,它使得我想要绘制在AbstractNote中的背景现在被绘制到GridView画布上,这个画布比AbstractNote中的小画布大得多。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接