James Hatton
James Hatton

Reputation: 696

Trying to add Imageview to canvas via FrameLayout.addView();

I can add this logo to the canavas if I do this:

FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(200,200);

ImageView boo = new ImageView(ExampletouchActivity.this);
boo.setImageResource(R.drawable.ic_launcher);
boo.setLayoutParams(lp);
fr.addView(boo); //this works fine

But if I try to add the same logo to the canvas like this it does not show anything up:

fr.addView(new Toucher(ExampletouchActivity.this));

My Toucher class is like so...

public class Toucher extends View{
ImageView boo;

public Toucher(Context context) 
{
    super(context);
    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(200,200);
    boo = new ImageView(context);
    boo.setImageResource(R.drawable.ic_launcher);
    boo.setLayoutParams(lp);
}
@Override
public void onDraw(Canvas c)
{   
    boo.draw(c);
}

I have been trying for ages and cannot work out why this imageview will not draw itself to the canvas?

Many Thanks

Upvotes: 1

Views: 1510

Answers (2)

Harsh Dev Chandel
Harsh Dev Chandel

Reputation: 763

When you are using the canvas, in a view things are different, so try using:

canvas.drawBitmap(bg, src, dst, paint);

Upvotes: 0

Sparky
Sparky

Reputation: 8477

Layout and Canvas aren't the same thing. Layout is "draw this for me", and Canvas is "I'm drawing this". In your working example, you're taking a static image and putting it into a Layout.

Upvotes: 1

Related Questions