android - Gridview,允许背景 Canvas 在 View 外绘制

标签 android gridview canvas

我正在开发一个适用于 Android 的应用程序,一个小型记事本应用程序。 在我的应用程序中,我使用 Gridview 创建一个 2*X 大小的网格,并在每个单元格中 我有一个 LinearLayout 调用 AbstractNote,它的任务是显示每次保存的预览 在应用程序中注释。

我的 AbstractNote 有一个背景图像,为了显示而旋转了一些 预览更精美。但我的问题是,当我旋转图像时,其中的一些图像落在了 AbstractNote 区域之外,而且我找不到显示它的好方法。

我已经尝试使用 View.setAnimation(...) 并且它有效,但给我粗糙的图像和文本, 我已经尝试创建一个自定义 Drawable(设置为 AbstractNote.setBackgroundDrawable(...)),它给我平滑的图像但不显示完整图像。

请注意,我的目标 android 是 2.1,我没有使用 View.setRotation(...)

使用 View.setAnimation 来执行此操作,但图像和文本会变得粗糙

使用自定义可绘制对象,但不能在单元格外绘制。

如果有人知道如何修复它,请修复尝试 1 或中的粗糙图像和文本 尝试2中如何在单元格外绘制图像,将不胜感激。

最佳答案

我终于找到了这个问题的解决方案,这个解决方案不是很好但是有效,我的想法是绘制单元格项目直接在 GridView 上绘制背景而不是 GridView childen/item

我创建一个新的 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);
}
}

在我的 child AbstractNote 中,我添加了方法 drawBackground,这就是我要在我的 AbstractNote 中绘制的背景比我在 AbstractNote

中的小 Canvas 大得多的 GridView Canvas

关于android - Gridview,允许背景 Canvas 在 View 外绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12315965/

相关文章:

android - 从Android接收视频流到PC上的VLC

android - 如何在Android的一次查询中获取流派和每种流派的歌曲数?

Android TV 应用程序不支持 Amazon FireTV

c# - xtraGrid gridControl 中的自定义 summerType

jquery - 需要帮助定位 Canvas 元素

android - SplashActivity中如何使用centerCrop

c# - 如何为复杂类型设置 DataKeyNames?

c# - 格式化 DataTable 列的百分比

javascript - Canvas drawImage 使用数据 URL

javascript - 将 Highcharts 导出到 jsPDF - canvg 错误