android 在触摸事件上绘图

发布于 2024-11-25 12:55:11 字数 1213 浏览 2 评论 0原文

我正在尝试制作一个应用程序,使用户能够触摸屏幕并根据用户的手指坐标绘制图像。这是我的代码:

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;

        }
    }
}

但是,每当我尝试单击模拟器中的屏幕时,都没有显示图像......

请指出我的错误......THX

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....

pls point out my mistake... THX

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七秒鱼° 2024-12-02 12:55:11

onTouchEvent() 中没有 invalidate()

        @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();
                    invalidate(); // add it here
                    break;
            }

            return true;

        }

You don't have invalidate() in onTouchEvent()

        @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();
                    invalidate(); // add it here
                    break;
            }

            return true;

        }
ぶ宁プ宁ぶ 2024-12-02 12:55:11

我已经多次发布了这个问题的答案,这段代码100%有效。如果您还有任何疑问,那么您可以联系我,

但是此代码将适用于在 GOOGLE 地图上绘制图像:

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;   
     }



 }

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

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;


    }  

and also define another(2nd) overlay class... where this event will get.

  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;   
     }



 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文