Android 点击按钮时绘制图像
基本上,我有一个按钮,单击该按钮时,图像会出现在随机坐标中。
这是我的代码:
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();
invoker = (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+")");
}
}
这就是问题所在。每当我单击按钮时,就会绘制图像,但无处可见......每当我按下按钮时,屏幕上都没有绘制图像...
请指出我的错误? THX
注意:我的 LogCat 中没有错误
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();
invoker = (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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅仅因为您创建了画布并不意味着它与显示器上的任何内容相关联,该画布只是您所做的虚拟绘图表面。
有几种方法可以做到这一点,最简单的可能就是创建一个自定义视图,重写 onDraw 方法,然后在其中显示位图。
将自定义视图添加到布局中,使用 fill_parent 指定高度和宽度
单击按钮时,只需设置图像的 x/y 并使视图无效
更新
好的,这是一个自定义视图,这将成为您的绘图表面对于您的图像,将其插入到您的主布局中,如果您愿意,请将其设为全屏(假设您使用的是RelativeLayout,您还可以在任何您想要的地方添加其他视图、按钮等)a
通过将绘图代码放入适当的位置来扩展它如果/阻止。然后使用 findviewbyid 获取自定义类,就像获取按钮一样。
现在,当您想要根据按钮单击绘制图像时,请调用自定义视图实例上的方法。无论如何,这是一种简单的方法。
您还可以扩展您的布局类(无论您在 main 中使用什么)并执行相同的操作而不添加子视图。
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.