0

I am trying to draw a image at dynamic positions on top of a RelativeLayout that has other elements. I tried creating a class that extends View and add this view to the RelativeLayout but the image is only thing that is shown on the screen. The other elements in the RelativeLayout are not visible.

this is the view class

@Override
protected void onDraw (Canvas canvas) {

    super.onDraw(canvas);
    Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),    R.drawable.ic_launcher);
    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(myBitmap, 50,50, null);   
}

this is in the onCreate of the Activity

    mainLayout = (RelativeLayout)findViewById(R.id.main_tutorial_layout);
    mainLayout.addView( new DrawAnimationTutotial(getApplicationContext()));
    mainLayout.invalidate();
user591124
  • 495
  • 2
  • 9
  • 23

1 Answers1

1

Why not use an ImageView?

mainLayout = (RelativeLayout) findViewById(R.id.main_tutorial_layout);
ImageView image = new ImageView(this);
image.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(50, 50);
layoutParams.leftMargin = xValue; 
layoutParams.topMargin = yValue;
image.setLayoutParams(layoutParams);
mainLayout.addView(image);
Ryan
  • 937
  • 1
  • 10
  • 17
  • I also need to be able to rotate the image – user591124 Jul 15 '12 at 15:24
  • Check here http://stackoverflow.com/questions/8981845/androidrotate-image-in-imageview-by-an-angle – Ryan Jul 18 '12 at 03:01
  • Thanks for your help. I got it working using canvas and extending the view class. I jus had one more question are the canvas drawing operations and animations carried out using the GPU. For android 3.0 and higher i think they are but I am developing my app on 2.3. Will including animation in any way effect the functionality of my app? – user591124 Jul 18 '12 at 17:47
  • As far as I know, if you want your app to use the GPU, you have to use OpenGL. – Ryan Jul 19 '12 at 21:10