2012-10-02 3 views
1

처음으로 여기에 질문하는 것이므로 주제에 머물러보십시오. 비트 맵 객체의 알맞은 크기의 ArrayList를 만들어 배경을 무작위로 생성하고이를 순서대로 그리려고합니다. 이 구현은 그런 식으로 단일 비트 맵을 로딩 할 때 유용합니다. 그냥 목록에 걸려 넘어 질뿐입니다.Android : 비트 맵의 ​​arraylist를 검은 색 배경으로 그립니다.

코드를 작성하기 전에 개별 픽셀 또는 타일을 추가하여 단일 비트 맵을 만들고 실제로 몇 가지 변형을 시도했지만 이상적으로 모두 검정색으로 표시된다는 점을 지적하고 싶습니다. 스크린; 캔버스에 그리는 방법에 문제가 있다고 생각하기 시작했습니다. 어쨌든, 내가 가지고있는 것은 다음과 같습니다.

먼저 3 개의 색상 만 사용하여 임의의 ArrayList를 생성합니다. 나는 목록을 반환하게 만들지 만 스레드의 변수 중 하나를 참조하는 스레드 내부의 개인 메소드 일 뿐이므로 중요하지 않습니다.

private void genMap(Resources res) 
    { 
     // Load basic tiles. 
     Bitmap green = BitmapFactory.decodeResource(res, R.drawable.green); 
     Bitmap red = BitmapFactory.decodeResource(res, R.drawable.red); 
     Bitmap blue = BitmapFactory.decodeResource(res, R.drawable.blue); 

        // All tiles must be the same size. 
     int tile_width = green.getWidth(); 
     int tile_height = green.getHeight(); 
     int num_x = mCanvasWidth/tile_width; 
     int num_y = mCanvasHeight/tile_height; 

     for (int j = 0; j < num_y; j++) 
     { 
      for (int i = 0; i < num_x; i++) 
      { 
       double r = Math.random(); 
       Bitmap tile; 
       if (r <= 1/3) {tile = green;} 
       else if (r <= 2/3) {tile = red;} 
       else {tile = blue;} 
       // Create a new Bitmap in order to avoid referencing the old value. 
       mBackgroundImages.add(Bitmap.createBitmap(tile)); 
      } 
     } 
    } 

그래서, 그 임의의 값이 패턴에 매핑되는 방법입니다. 이 메소드는 스레드의 생성자에서 호출되며 onCreate가 호출 될 때마다 호출됩니다. 지금은 목록을 지우고 매번 새로운 무작위 패턴을 만들뿐입니다.

... 
Resources res = context.getResources(); 
mBackgroundImages = new ArrayList<Bitmap>(); 
genMap(res); 
... 

마지막으로 그리기 방법입니다. 그것을 잘 부하에게 BitmapFactory.decodeResources를 통해 하나의 비트 맵을 작동하지만,이 일을 검은 화면을 보여줍니다

어떤 도움을 주시면 감사하겠습니다
private void doDraw(Canvas canvas) 
    { 
     /* Draw the bg. 
     * Remember, Canvas objects accumulate. 
     * So drawn first = most in the background. */ 
     if (canvas != null) 
     { 
      if (mBackgroundImages.size() > 0) 
      { 
       int tile_width = mBackgroundImages.get(0).getWidth(); 
       int tile_height = mBackgroundImages.get(0).getHeight(); 
       for (int y = 0; y < mCanvasHeight/tile_height; y++) 
       { 
        for(int x = 0; x < mCanvasWidth/tile_width; x++) 
        { 
         // Draw the Bitmap at the correct position in the list; Y * XWIDTH + X, at pos X * XWIDTH, Y * YWIDTH. 
         canvas.drawBitmap(mBackgroundImages.get((x + y * (mCanvasWidth/tile_width))), x * tile_width, y * tile_height, null); 
        } 
       } 
      } 
     } 
    } 

, 감사합니다.

+0

타일링 알고리즘 나중에 올 수 있습니다 :이 실제로 비트 맵을 만드는 경우 또한 볼 것입니다. 타일링 문제가있는 경우 일반적으로 이미지 그림에 이상한 오프셋이 생길 수 있으므로 문제는 이미지를 처리해야한다고 생각합니다. –

답변

0

다음 줄의 우선 순위를 다시 확인 했습니까?

((x + y * (mCanvasWidth/tile_width))) 

재미있는 질문 : 단색을 사용하고 있습니까?

mBackgroundImages.add(Bitmap.createBitmap(tile)); 

당신은 실제로 단지 대신 비트 맵을 많이 만드는 참조를 유지할 수 있습니다,하지만 확실히

관련 문제