2016-10-02 2 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> 

내 활동

,763,158,653 : 결과는 하나 개의 사각형을 보여줍니다

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; 
    } 
} 

} 

그러나

+0

또한 XML을 –

+0

추가했습니다. 감사합니다. – Flo

답변

-1

210 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 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입니다.

0

RectF 메서드에서 전달 된 매개 변수 순서가 올바르지 않습니다. 이 방법의 문서에서 알 수 있듯이 RectF documentation 인수의 순서는 각각 left, top, rightbottom입니다. 게다가 당신의 왼쪽 인자는 당신의 코드에 보이지 않을 수도있는 오른쪽 인자보다 작거나 같아야합니다.

관련 문제