【问题标题】:Android - Custom view, not drawing correctlyAndroid - 自定义视图,无法正确绘制
【发布时间】:2016-10-02 13:02:47
【问题描述】:

我有点卡住了。

我尝试创建一个像表格一样的自定义视图!但是 onDraw 方法只是绘制第一个单元格/矩形。

这是我的看法:

public class CustomView extends View {


    private static int ROWS = 8;
    private static int COLS = 8;

    final Paint p = new Paint();

    public CustomView(Context context) {
        super(context);

        p.setColor(Color.GREEN);
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawColor(Color.RED);

        float width = getWidth();
        float height = getHeight();
        float cellWidth = width/COLS;
        float cellHeight = height/ROWS;

        for(float row = 0; row<ROWS; row++) {
            for(float col = 0; col<COLS; col++) {
                RectF cell = new RectF( (cellWidth*col), (cellHeight*row), cellWidth, cellHeight);
                canvas.drawRect(cell, p);
            }
        }
    }

}

我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/mainLayout"
    android:orientation="vertical"
    android:background="#000000"
    tools:context="com.biegertfunk.qlocktwo.Act_Main">


    <RelativeLayout
        android:id="@+id/centerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

我的活动:

public class Act_Main extends AppCompatActivity {


//views
public CustomView mainView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_main);

    int size = getSize();

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(size, size);
    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);


    RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.centerView);

    mainView = new CustomView(this);

    mainLayout.addView(mainView, layoutParams);

}

private int getSize() {
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

    if(width>height) {
        return height;
    } else {
        return width;
    }
}

}

但结果只显示一个矩形:

【问题讨论】:

  • 也发布你的xml
  • 补充了一些信息,谢谢!

标签: android canvas ondraw


【解决方案1】:

您在onDraw() 中计算的cell 是错误的。试试这个方法:

RectF cell = new RectF( 
    (cellWidth*col), (cellHeight*row), 
    (cellWidth*col)+cellWidth, (cellHeight*row)+cellHeight
);

RectF的构造函数是

RectF(float left, float top, float right, float bottom)

不是

RectF(float left, float top, float width, float height) // WRONG

请参阅文档here

【讨论】:

    【解决方案2】:

    RectF 方法中传递的参数顺序不正确。正如您在RectF documentation 中此方法的文档中看到的那样 您将看到参数的顺序分别为lefttoprightbottom。此外,您的左参数必须小于或等于右参数,这可能不会在您的代码中看到。

    【讨论】:

      【解决方案3】:

      添加代码

      invalidate();
      

      for(float row = 0; row<ROWS; row++) {
              for(float col = 0; col<COLS; col++) {
                  RectF cell = new RectF( (cellWidth*col), (cellHeight*row), cellWidth, cellHeight);
                  canvas.drawRect(cell, p);
                  invalidate();
              }
          }
      

      【讨论】:

      • 哎呀——在onDraw() 中调用invalidate() 将导致无限循环。你永远不应该在那里打电话给invalidate()...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多