【问题标题】:android drawing on touch eventandroid在触摸事件上绘图
【发布时间】:2011-07-20 03:49:49
【问题描述】:

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

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

【问题讨论】:

    标签: android canvas bitmap touch-event


    【解决方案1】:

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

    【讨论】:

    • 如果我在 onDraw 中直接使用 xPos 和 yPos,为什么我们需要在 onTouchEvent 中失效?
    【解决方案2】:

    我已多次发布此问题的答案,此代码 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;   
         }
    
    
    
     }
    

    【讨论】:

    • 这个问题和谷歌地图有什么关系??
    • @SimonAndréForsberg 伙计,你为什么不投票给这个答案。你成功了吗???你看不到。此代码将在触摸事件时在 Google 地图上绘制图像。我不知道你有什么问题。如果你不明白,请向我提问,我会详细说明。
    猜你喜欢
    • 2019-05-09
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多