Android draw image when clicking button
basically, I have a button which when clicked, an image will appear in random coordinate..
Here's my code :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//mDrawable = getResources().getDrawable(R.drawable.icon);
Log.i("Info", "Resource got !!");
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
mCanvas = new Canvas();
invo开发者_运维问答ker = (Button) findViewById (R.id.invoke);
invoker.setOnClickListener(this);
}
public void onClick(View v) {
if (v.getId() == R.id.invoke) {
Log.i("Info", "Button Clicked !!");
Display d = getWindowManager().getDefaultDisplay();
Log.i("Info", "Returning window Info !!");
// TODO Auto-generated method stub
xPos = randInt.nextInt(d.getWidth());
yPos = randInt.nextInt(d.getHeight() - invoker.getHeight())+ invoker.getHeight();
Log.i("Info", "Coord got !!, xPos = "+xPos+", yPos = "+yPos);
mCanvas.drawBitmap(mBitmap, yPos, xPos, null);
Log.i ("Info", "Image drawn at ("+xPos+", "+yPos+")");
}
}
Here's the problem. Whenever I clicked the button, the image was drawn, but it was nowhere to be seen....there's no image drawn on the screen whenever I press the button...
pls point out my mistake ?? THX
note : no error in my LogCat
Just because you created a canvas does not mean it's associated with anything on the display, this canvas is just a virtual drawing surface the way you have done it.
There are a few ways to do this, the easiest is probably just create a custom view, override the onDraw method, and then display your bitmap in there.
Add your custom view to your layout with fill_parent for height and width
When your button is clicked just set your x/y for the image and invalidate the view
Update
Ok here is a custom View this becomes your drawing surface for your image, insert it into your main layout, make it full screen if you like (assuming you are using a RelativeLayout you can also add other views,buttons,etc anywhere you want) a
Extend it by putting your drawing code into the appropriate if/block. Then get the custom class by using findviewbyid just like you do for a button.
Now when you want to draw to your image based on the button click call the methods on the custom view instance. Thats one trivial way to do it anyway.
You could also extend your layout class (whatever you are using in main) and do the same thing without adding a child view.
public class myView extends View {
private boolean imageShow = false;
private int x = 0;
private int y = 0;
public myView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public myView(Context context) { this(context,null,0); }
public myView(Context context, AttributeSet attrs) { this(context, attrs,0); }
public void drawImageRandom()
{
imageShow = true;
invalidate();
}
public void hideImage()
{
imageShow = false;
invalidate();
}
@Override
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
if (imageShow) {
// do your drawing code in here
}
}
}
精彩评论