2016-10-02 7 views
0

私はちょっと立ち往生しています。Android - カスタム描画、正しく描画されない

テーブルのようなカスタムビューを作成しようとしました!しかし、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; 
    } 
} 

} 

しかし、結果は一つだけの四角形を示しています。

を210

+0

あなたのXMLを投稿する –

+0

さらに詳しい情報を追加しました。 – Flo

答えて

-1

onDraw()内部cellのあなたの計算が間違っている

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(); 
     } 
    } 
+0

'onDraw()'で 'invalidate()'を呼び出すと、無限ループになります。そこに 'invalidate()'を呼ぶべきではありません... – gus27

1

するコード

invalidate(); 

を追加します。 RectFのコンストラクタは

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

ない

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

ある

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

ドキュメントhereを参照してください。それをこの方法を試してみてください。

0

RectFメソッドで渡されるパラメータの順序が正しくありません。このメソッドのドキュメントには次のように書かれていますRectF documentation 引数の順番はそれぞれleft,top,rightおよびbottomです。さらに、あなたの左の引数はあなたのコードには見えないかもしれない右の引数よりも小さいか等しい必要があります。

関連する問題