2013-03-21 3 views
0

코드는 배열을 통해 실행됩니다. 문자 'X'가 배열에 표시되면 화면의 각 지점에 drawRect가 표시됩니다.안드로이드 화면에 캔버스 그리기 및 만들기

문제는 색상이 실제로 화면에 그려지지 않는다는 것입니다. 일부 안드로이드 웹 사이트에서 캔버스, 비트 맵 등이 필요한 방식에 대해 이야기했습니다.

내 질문 : drawRect 메서드의 색상을 실제로 화면에 표시하려면 어떻게해야합니까?

package com.example.routedrawingtest; 

import android.app.Activity; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Point; 
import android.os.Bundle; 
import android.view.Display; 
import android.graphics.Paint; 
import android.view.View; 

public class MainActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    float scale_x; 
    float scale_y; 

    public void main(String[] args){ 
     @SuppressWarnings("unused") 
     Display display = getWindowManager().getDefaultDisplay(); 
     Point size = new Point(); 

     int screen_width = size.x; 
     int screen_height = size.y; 
     scale_x = screen_width/125; 
     scale_y = screen_height/100; 

    } 

    public void onDraw(Canvas canvas) { 

     int i; 
     Paint paint = new Paint(); 
     paint.setColor(Color.RED); 
    paint.setStrokeWidth(10); 
     int j; 

     for (i=0; i<125; i++) 
      for (j=0; j<100; j++) 
       if (charMap[i][j]=='X') 

       canvas.drawRect(i*scale_x, j*scale_y, scale_x,scale_y, paint); 
    } 

} 
+0

이 질문을하기 전에 몇 가지 조사를. 예를 들어 안드로이드는 기본 방법을 사용하지 않습니다. – FutureSci

답변

0

방법 onDraw은 당신이 원하는 일을하기 위해 Activity에 의해 호출되지 않습니다, 당신은 클래스 뷰를 확장하고이 방법을 구현해야합니다. 부모에게보기를 추가 할 때 더 많이 알기 전까지는 fill_parent과 같은 명시적인 크기를 지정해야합니다. 그렇지 않으면 화면에 나타나지 않습니다. 예를 들어 :

public class Drawer extends View 
    { 
     Paint paint = new Paint(); 

     public Drawer() 
     { 
      paint.setStyle(Style.STROKE); 
      paint.setStrokeColor(Color.BlUE); 
      // etc... 

     } 

    public void onDraw(Canvas canvas) 
    { 
     for (i=0; i<125; i++) 
     for (j=0; j<100; j++) 
      if (charMap[i][j]=='X') 

      canvas.drawRect(i*scale_x, j*scale_y, scale_x,scale_y, paint); 
    } 

    } 

는 XML을 통해 또는를 사용하여 같은 코드를 통해 코드보기를 추가 정규화 된 이름입니다 :

XML :

 <com.example.comp.Drawer 
     android:layout_width="fill_parent"<<<< specify a size 
     etc... 
    />