2010-08-17 5 views
1

나는 몇 시간 동안 Android reference를 연구했지만, ImageView에 무언가 (텍스트, 비트 맵, 경로 ....)를 그릴 수있는 단서를 얻지 못했습니다.ImageView에 그리기

보기를 확장하고 onDraw() -Method를 사용해야합니까? 그렇다면 ImageView에서 어떻게 그릴 수 있습니까?

내 목표를 달성하기위한 다른 방법이 있습니까?

답변

4

ImageView에 다른 비트 맵을 그리려는 경우 AbsoluteLayout을 사용하고 서로 위에 배치하는 것보다 동적이어서는 안됩니다.

동적 인 경우 SurfaceView를 사용하는 것이 좋습니다. 튜토리얼은 여기에서 찾을 수 있습니다 : http://www.droidnova.com/playing-with-graphics-in-android-part-i,147.html

(현재는 온라인 webarchive를 통해 : http://web.archive.org/web/20121005111921/http://www.droidnova.com/2d-tutorial-series)

+0

이 튜토리얼을 읽어 주셔서 감사합니다. – Alien

+0

Hmmm .... 그는 SurfaceView 아래에 뭔가가 없지만 ImageView를 사용하지 않고 동적으로 텍스트를 그리지 않아도됩니다. 어떤 아이디어를 달성하는 방법? – Alien

+0

이 시리즈의 모든 부분을 읽지 못했습니까? ImageView를 잊어 버리고 SurfaceView에 비트 맵을 그립니다. 위의 텍스트를 그립니다. 처음 3 부분 이상을 읽으십시오! – WarrenFaith

0

SurfaceView 당신이 원하는 것입니다. 그것은 당신에게 canvas.drawCircle(...), canvas.drawText(...), canvas.drawBitamp(...)을 사용하여 그릴 수있는 Canvas 개체를 줄 것입니다.

+0

그래, 고마워, SurfaceView에 대한 참조 그리고 나는 내 목표를 달성 할 수있을 것이라고 생각한다. – Alien

+0

그리고 SurfaceView 아래에 ImageView (XML 기반 레이아웃으로 선언 된)를 어떻게 가져 와서 그릴 수 있을까? – Alien

3

예, onDraw 메소드를 사용할 수 있습니다. 뷰에 그리는 데 사용할 메소드에 전달 된 Canvas 객체가 있습니다. 다음은 Zebra Crossing 바코드 스캐너 응용 프로그램에서 가져온 방법의 예입니다. 어두운 외부 상자, 빨간색 스캐너 선 및 노란색 스캔 결과 점을 표시하는보기입니다.

@Override 
public void onDraw(Canvas canvas) { 
    Rect frame = CameraManager.get().getFramingRect(); 
    if (frame == null) { 
     return; 
    } 
    int width = canvas.getWidth(); 
    int height = canvas.getHeight(); 
    Log.v("ViewfinderView", "Canvas size: " + width + ", " + height); 

    // Draw the exterior (i.e. outside the framing rect) darkened 
    paint.setColor(resultBitmap != null ? resultColor : maskColor); 
    canvas.drawRect(0, 0, width, frame.top, paint); 
    canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint); 
    canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint); 
    canvas.drawRect(0, frame.bottom + 1, width, height, paint); 

    if (resultBitmap != null) { 
     // Draw the opaque result bitmap over the scanning rectangle 
     paint.setAlpha(OPAQUE); 
     canvas.drawBitmap(resultBitmap, frame.left, frame.top, paint); 
    } else { 

     // Draw a two pixel solid black border inside the framing rect 
     paint.setColor(frameColor); 
     canvas.drawRect(frame.left, frame.top, frame.right + 1, frame.top + 2, paint); 
     canvas.drawRect(frame.left, frame.top + 2, frame.left + 2, frame.bottom - 1, paint); 
     canvas.drawRect(frame.right - 1, frame.top, frame.right + 1, frame.bottom - 1, paint); 
     canvas.drawRect(frame.left, frame.bottom - 1, frame.right + 1, frame.bottom + 1, paint); 

     // Draw a red "laser scanner" line through the middle to show decoding is active 
     paint.setColor(laserColor); 
     paint.setAlpha(SCANNER_ALPHA[scannerAlpha]); 
     scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length; 
     int middle = frame.height()/2 + frame.top; 
     canvas.drawRect(frame.left + 2, middle - 1, frame.right - 1, middle + 2, paint); 

     Collection<ResultPoint> currentPossible = possibleResultPoints; 
     Collection<ResultPoint> currentLast = lastPossibleResultPoints; 
     if (currentPossible.isEmpty()) { 
     lastPossibleResultPoints = null; 
     } else { 
     possibleResultPoints = new HashSet<ResultPoint>(5); 
     lastPossibleResultPoints = currentPossible; 
     paint.setAlpha(OPAQUE); 
     paint.setColor(resultPointColor); 
     for (ResultPoint point : currentPossible) { 
      canvas.drawCircle(frame.left + point.getX(), frame.top + point.getY(), 6.0f, paint); 
     } 
     } 
     if (currentLast != null) { 
     paint.setAlpha(OPAQUE/2); 
     paint.setColor(resultPointColor); 
     for (ResultPoint point : currentLast) { 
      canvas.drawCircle(frame.left + point.getX(), frame.top + point.getY(), 3.0f, paint); 
     } 
     } 

     // Request another update at the animation interval, but only repaint the laser line, 
     // not the entire viewfinder mask. 
     postInvalidateDelayed(ANIMATION_DELAY, frame.left, frame.top, frame.right, frame.bottom); 
    } 
    } 
+0

예를 들어 주셔서 감사합니다. 약간 과잉이지만 내 목표를 달성하는 방법을 알려주었습니다. – Alien

+0

그래, 저기에 많이있어 ... 내가 우연히 만난 소스 일 뿐이야. 정말 도움이된다면 작은 체크 표시를 잊지 마세요 :) – iandisme

+0

Surface View의 힌트는 Fredley에서 훌륭했고 WarrenFaith의 튜토리얼 링크도 좋았 기 때문에 실제로는 할 수 없었습니다. 누가 체크 표시를해야할지 결정하십시오 :) – Alien