本文介绍了在触摸事件的Andr​​oid图纸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用户根据用户的手指坐标触摸屏,并绘制图像的应用。这是我的code:

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

推荐答案

您不必无效()的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;

        }

这篇关于在触摸事件的Andr​​oid图纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:11