2011-03-11 6 views
1

그래서 나는 리소스 파일에서로드 한 비트 맵 (AN PNG 이미지)이 : 내가 사용 번만 비트 맵을 그리면안드로이드 : 문제 캔버스에 반복해서 같은 비트 맵을 그릴 때

Bitmap map = BitmapFactory.decodeResource(getResources(), R.drawable.wave); 

canvas.drawBitmap(...); 아무 문제 없습니다. 그러나 동일한 비트 맵을 여러 번 그으면 그림이 계속 깜박입니다.

동일한 비트 맵을 두 번 이상 사용할 수 없다는 의심 때문에 동일한 그림을 그릴 때마다 이미지를 새로운 비트 맵에로드하려고 시도했지만 도움이되지 않아 동작이 계속 유지됩니다.

프로그램은 복잡하지만 기본적으로 나는 바다 물결을 그려보고 싶습니다. 나는 작은 파도의 이미지를 가지고있다. 웨이브의 효과를 화면의 왼쪽 가장자리에서 오른쪽 가장자리로 이동하려면 비트 맵 왼쪽 가장자리의 위치를 ​​추적합니다.

// The ocean. 
private ArrayList<Wave> waves; 

      // Draw the waves and update their positions. 
    for (int i = 0; i < this.waves.size(); i++) 
    { 
     Wave wave = this.waves.get(i); 

     // Go through each of the sub-waves of this current wave. 
     for (int j = 0; j < wave.getSubWaveEdges().size(); j++) 
     { 
      // Get the sub wave. 
      final float subWaveEdge = wave.getSubWaveEdges().get(j); 

      canvas.drawBitmap(wave.getSubWave(j), subWaveEdge, 40, brush); 

      wave.setSubWaveEdge(j, subWaveEdge + (float) 0.5); 
     } 

     // Update this current wave. 
     wave.update(); 

     // If the wave has passed the left edge of the screen then add a new sub-wave. 
     if (wave.getFarthestEdge() >= 0) 
      wave.addSubWaveEdges(wave.getFarthestEdge() - this.getWidth()); 
    } 

비트 맵의 ​​좌측 에지가 화면 내에 있을지

다음 I 동일한 이미지 파일에서 새로운 맵을 만들고 그린다. 다음은 클래스 웨이브는 다음과 같습니다

private class Wave 
{ 
    private Bitmap wave; 
    private float farthestEdge; 
    private ArrayList<Float> subWaveEdges; 
    private ArrayList<Bitmap> subWaves; 

    public Wave(Bitmap wave) 
    { 
     this.wave = wave; 

     this.farthestEdge = 0; 

     this.subWaveEdges = new ArrayList<Float>(); 

     this.subWaves = new ArrayList<Bitmap>(); 
    } 

    public Bitmap getWave() 
    { return this.wave; } 

    public void setWave (Bitmap wave) 
    { this.wave = wave; } 

    public float getFarthestEdge() 
    { return this.farthestEdge; } 

    public void setFarthestEdge (final float furthestEdge) 
    { this.farthestEdge = furthestEdge; } 

    public ArrayList<Float> getSubWaveEdges() 
    { return subWaveEdges; } 

    public void setSubWaveEdge (final int index, final float value) 
    { 
     this.subWaveEdges.remove(index); 

     this.subWaveEdges.add(value); 
    } 

    public void addSubWaveEdges (final float edge) 
    { 
     this.subWaveEdges.add(edge); 

     Bitmap newSubWave = BitmapFactory.decodeResource(getResources(), R.drawable.wave); 

     newSubWave = Bitmap.createScaledBitmap(newSubWave, MasterView.this.getWidth(), newSubWave.getHeight(), true); 

     this.subWaves.add(newSubWave); 
    } 

    public Bitmap getSubWave(final int index) 
    { return this.subWaves.get(index); } 

    public void update() 
    { 

     // Check to see if there is any sub-wave going outside of the screen. 
     // If there is then remove that wave. 
     for (int index = 0; index < this.subWaveEdges.size(); index++) 
      if (this.subWaveEdges.get(index) > MasterView.this.getWidth()) 
      { 
       this.subWaveEdges.remove(index); 

       this.subWaves.remove(index); 
      } 

     // Set the farthest edge to the other side of the screen. 
     this.farthestEdge = MasterView.this.getWidth(); 

     // Get the farthest edge of the wave. 
     for (int index = 0; index < this.subWaveEdges.size(); index++) 
      if (this.subWaveEdges.get(index) < this.farthestEdge) 
       this.farthestEdge = this.subWaveEdges.get(index); 
    } 
} 

또 다른 의심 나는 이미지의 픽셀이 각 비트 맵 만 얻을 수 있다는 것을 의미, 두 개의 비트 맵 중 분할 나도 같은 리소스 파일에서 두 개의 비트 맵을 만들 때이 될 수있다 가지고 모든 픽셀이 아닌 일부. 비트 맵을 그릴 때 겹치는 부분이 꾸준히 그려 지므로 깜박 거리지 않아서 의심 스럽습니다.

누구나이 문제가 발생하여 해결 방법을 알고 있습니까?

감사합니다,

+1

포스트 소스. – Wroclai

+0

방금 ​​한 번 해봐. – Einiosaurus

답변

1

빅토르 Lannér은, 도와 주셔서 감사합니다,하지만 난 그게 문제라고 생각하지 않습니다. 큰 프로그램의 작은 조각 일 뿐이므로 코드를 읽는 것이 어렵다는 것을 이해합니다.

그러나이 문제는 원래의 질문에서 언급하지 않았지만 두 파도가 서로 움직이게하려면 첫 번째 파도가 화면에 들어가 자마자 다음 파도를 그려야합니다. 그러나 각 웨이브는 화면 폭보다 깁니다. 따라서, 내가 무슨 뜻인지 안다면 화면의 "외부"에서 다음 웨이브를 그려야합니다.

// If the wave has passed the left edge of the screen then add a new sub-wave. 
    if (wave.getFarthestEdge() >= 0) 
     wave.addSubWaveEdges(wave.getFarthestEdge() - this.getWidth()); 

을 그리고 나는이 좋아하지 않는 것을 발견 : 그것은 다음 파 부정적인에서 그려지는 것을 의미한다 화면 밖에서 x 좌표. 이것이 앞뒤로 깜박이는 원인입니다. 받는 그려 질

 canvas.drawBitmap (Bitmap bitmap, Rect source, Rect destination, Paint paint) 

이 방법은 비트 맵의 ​​사각형 영역을 지정할 수 대신 화면 외부로부터 차세대 그리기,이 문제를 해결하기 위해

,이 방법을 사용 화면과 비트 맵의 ​​해당 부분이 그려지는 사각형 영역이 있습니다. 이 방법을 사용하여 다음 웨이브를 그립니다.다음 웨이브가 화면으로 이동함에 따라 "소스"와 "대상"을 적절히 변경하여 비트 맵의 ​​일부분을 그립니다.

0

canvas.drawBitmap(...) 문의하기 전에; 이전 그림에서 Canvas을 지우려면 canvas.drawColor(Color.BLACK)을 사용해보십시오.

샘플 코드 :

// Stuff. 
canvas.drawColor(Color.BLACK); 
canvas.drawBitmap(wave.getSubWave(j), subWaveEdge, 40, brush); 
// Stuff. 
1

캔버스의 이미지가 앞뒤로 깜박이거나 검은 색과 첫 번째 프레임 사이에서 깜박 거리면서 움직일 때까지 거의 캔버스가 빠르게 전환되는 것처럼 보였습니다. 현재와 ​​마지막 이미지.

이것은 상황과 관련이 있었을 수도 있고, 그것을 고치기 위해 내가 그릴 때마다 캔버스를 모든 프레임에 고정했기 때문에 그 사실을 알았습니다. 어떤 이유로 든, 그 잠금 장치가이 상황을 만들어 냈습니다.

나는 이런 식으로 뭔가를 수행하여 주위 가지고 :

if (needToRedraw == true) { 
     canvas = mSurfaceHolder.lockCanvas(null); 
     ... logic to eventually draw on that canvas ... 
    } 
관련 문제