android绘图触摸事件 [英] android drawing on touch event
本文介绍了android绘图触摸事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图创建一个应用程序,使用户能够触摸屏幕并根据用户的手指坐标绘制图像。这是我的代码:
I'm trying to make an application which enables user to touch the screen and draw image based on users' finger coordinates. Here's my code :
public class DrawingBoard extends View {
Drawable editIcon = getResources().getDrawable(R.drawable.icon);
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background);
float xPos = 0;
float yPos = 0;
public DrawingBoard (Context context) {
// TODO Auto-generated constructor stub
super (context);
}
@Override
protected void onDraw (Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.drawBitmap(mBitmap, 0, 0, null);
canvas.translate(xPos, yPos);
editIcon.draw(canvas);
canvas.restore();
invalidate();
}
@Override
public boolean onTouchEvent (MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN :
xPos = event.getX();
yPos = event.getY();
break;
}
return true;
}
}
}
每当我尝试点击模拟器中的屏幕,没有显示的图像....
But, whenever I try to click on a screen in emulator, there's no image shown....
指出我的错误... THX
pls point out my mistake... THX
推荐答案
我已经多次回答这个问题,这段代码工作100%。如果u仍有任何查询,则u可以与我联系
I have posted an answer for this question many times,This code is working 100 %. if u have still any query then u can contact me
但此代码将用于在Google Maps上绘制图像:
BUT THIS CODE WILL WORK FOR DRAWING AN IMAGE ON GOOGLE MAPS:
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
if (event.getAction() == 1)
{
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Toast.makeText(getBaseContext(), "lat and longtd is \n "+
p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_LONG).show(); //
mapView.getOverlays().add(new MarkerOverlay(p));
mapView.invalidate();
}
return true;
}
class MarkerOverlay extends Overlay
{
private GeoPoint p;
private Projection projection;
public MarkerOverlay(GeoPoint p)
{
this.p = p;
}
@Override
public boolean draw(Canvas canvas, MapView mapView,boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.pir_pictr);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
这篇关于android绘图触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文