views:

53

answers:

3

I studied the Androidreference for hours now, but don't really get the clue how to draw something (Text, Bitmap, Path ....) on a ImageView.

Should I extend View and use the onDraw()-Method? If yes, how can I draw on my ImageView?

Or is there any other way to achieve my goal?

A: 

If you just want to draw another bitmap on your ImageView and it shouldn't be dynamic, than use AbsoluteLayout and position them above each other.

If it should be much more dynamic, I recommend to use a SurfaceView. A tutorial can be found here: http://www.droidnova.com/playing-with-graphics-in-android-part-i,147.html

WarrenFaith
Thanks for this tutorial I will read threw it.
Alien
Hmmm.... He also doesn't have something under his SurfaceView, but I wan't to have the ImageView and wan't to draw dynamically Text over it. Any ideas how to achieve that?
Alien
You didnt read all parts of this series, right? Forget the ImageView and draw a bitmap on the SurfaceView. Than draw your text above this. Just read at least the first 3 parts!
WarrenFaith
After I noticed that the tutorial is only about settings same Icons/Bitmaps on the SurfaceView I only run over the following pages. But I completely read the first 3 parts! And there is nothing overdrawing in those first 3 parts. He is only draw his icons on his SurfaceView....But I solved the problem already, thanks so far.
Alien
Drawing bitmap over bitmap is easy, just use the same coordinates. The target of this tutorial is to show how to work with a SurfaceView. Btw "he" is me :)
WarrenFaith
Nice work, the tutorial is really good for understanding SurfaceViews!Every part gets even more interesting and ending up in a game is a very good idea, too.
Alien
A: 

SurfaceView is what you want. It will give you a Canvas object, onto which you can draw, using canvas.drawCircle(...), canvas.drawText(...), canvas.drawBitamp(...).

fredley
Ok thanks, I will read the reference to SurfaceView and I think then I will be able to achieve my goal
Alien
And how can I have my ImageView (declared in a XML-based Layout) under the SurfaceView, so I can draw over it?
Alien
A: 

Yes, you can use the onDraw method. There is a Canvas object passed into that method that you will use to draw on the view. Here is an example of how to do it... taken from the Zebra Crossing barcode scanner application. It is the view that displays the dark outer box, red scanner line, and yellow scan result dots.

@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);
    }
  }
iandisme
Thanks for the example, it's a little bit overkill but gave me an idea how to achieve my goal.
Alien
Yeah there's a lot there... it's just some source I happened to have handy. If it really helps you out, don't forget to click the little checkmark :)
iandisme
Well, the hint with the Surface View was also great from fredley and the link to the tutorial from WarrenFaith was also nice, so I can't really decide who should get the checkmark :)
Alien