【问题标题】:How to draw a line over a GridView programmatically如何以编程方式在 GridView 上绘制一条线
【发布时间】:2015-06-14 00:19:17
【问题描述】:

我不熟悉堆栈溢出问题,所以我会尽力在这里进行描述。

现在我正在玩 android studio。我已经设置了一个 gridview(我构建了自己的自定义图像适配器),其中包含形成 5 x 6 网格的方形图像,每个网格空间之间有一些填充。我想要做的是能够以编程方式在此网格视图之上绘制一条线(使用画布和油漆)。

为了尝试解决我的问题,我查看了很多关于堆栈溢出的帖子,例如:Draw a line on top of existing layout programmatically

按照上面链接中的说明 ^^^,我能够创建一个在 ImageView 上画线的程序。

但是当我在我的程序中使用类似的代码试图在 gridView 上画一条线时,它只是画了这条线,没有别的。

这是我正在尝试做的代码 sn-ps。这是 XML 文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- Game layout *er -->
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridviewGame"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:numColumns="5"
        android:stretchMode="columnWidth"
        android:gravity="center"
        />

    <com.example.ella.lazorsgame.DrawView
        android:id="@+id/paintView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

这是自定义视图代码:

public class DrawView extends View {
    Paint paint = new Paint();
    private int strokeWidth = 8;

    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(strokeWidth);
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawLine(0, 0, 300, 300, paint);
    }

}

这就是我在我的主要活动中绘制图像网格的地方(此代码自行工作......当我尝试在顶部绘制线条时网格不会显示):

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_setup);

        GridView gridview = (GridView) findViewById(R.id.gridviewGame);
        blockGridAdapter = new ImageAdapter(this, R.drawable.selectedblock, 30);

        // set up grid space object array *er
        for (int i = 0; i < BlockSpaces.length; i++) {
            BlockSpaces[i] = new BlockSpace(gridview, i);
        }

        // set up grid visual display on phone screen *er
        gridview.setAdapter(blockGridAdapter);

        // ------------- INITIAL GAME SETUP (block placements) -------------- *er
        blockSetup(levelSelected);

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                updateSelection(position);
            }
        });
    }

感谢您的帮助!如果需要更多信息,请告诉我。

【问题讨论】:

    标签: android gridview line ondraw


    【解决方案1】:

    你没有调用 onDraw 的超级方法

    super.onDraw(canvas);
    

    因此,唯一绘制的是线,系统不会处理您其他无效并需要重新绘制的视图!!!

    看看这个:http://developer.android.com/training/custom-views/custom-drawing.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-06
      • 1970-01-01
      • 1970-01-01
      • 2013-05-26
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      相关资源
      最近更新 更多