画布上的图像

时间:2016-05-27 17:26:30

标签: android canvas touch-event

我做了一个游戏tic tac toe。当我触摸那个盒子时,我有想要放在盒子里的图像。问题是,当我触摸屏幕然后它在画布上显示我的图像但是当我再次触摸另一个框然后第一个图像消失,该框中的新显示和pervoius删除



 public class caanvas extends View  {
    public Canvas canvas1=new Canvas();
        public float x_axis,y_axis;
        int height,width;
        Bitmap mFinalbitmap= BitmapFactory.decodeResource(getResources(), R.drawable.cross);

       public  float firstcord=0f,secndcord=0f,thirdcord=0f,forthcord=0f,fifthcord=0f,sixcord=0f,sevencord=0f,eightcord=0f;
        public float widthborder=0f;
       public float h = getContext().getResources().getDisplayMetrics().heightPixels;
       public  float w = getContext().getResources().getDisplayMetrics().widthPixels;
        public caanvas(Context context) {
            super(context);

            widthborder=w/72f;
            firstcord=w/2.88f;
            secndcord=w/1.5f;
            thirdcord=w/18f;
            forthcord=w/1.035f;
            fifthcord=h/4.2f;
            sixcord=h/2.46f;
            sevencord=h/1.63f;
            eightcord=h/1.28f;
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas1=canvas;
            canvas.save();
            Paint paint = new Paint();
            paint.setColor(Color.LTGRAY);
            paint.setStrokeWidth(widthborder);
            canvas.drawLine(firstcord, fifthcord, firstcord, eightcord, paint);
            canvas.drawLine(secndcord, fifthcord, secndcord, eightcord, paint);
            canvas.drawLine(thirdcord, sixcord, forthcord, sixcord, paint);
            canvas.drawLine(thirdcord, sevencord, forthcord, sevencord, paint);

            if(thirdcord<x_axis&&x_axis<firstcord&&fifthcord<y_axis&&y_axis<sixcord)//For A11 Box
            {
             width=(int)(firstcord-thirdcord);
             height=(int)(sixcord-fifthcord);
             Showimage();
            }
            else if(firstcord<x_axis&&x_axis<secndcord&&fifthcord<y_axis&&y_axis<sixcord)
        Showimage2();
            //else if(secndcord<x_axis&&x_axis<forthcord&&fifthcord<y_axis&&y_axis<sixcord)
       // Showimage();
           // else if(thirdcord<x_axis&&x_axis<firstcord&&sixcord<y_axis&&y_axis<sevencord)
           // Showimage();
            //else if(firstcord<x_axis&&x_axis<secndcord&&sixcord<y_axis&&y_axis<sevencord)
             //   Showimage();
           // else if (secndcord<x_axis&&x_axis<forthcord&&sixcord<y_axis&&y_axis<sevencord)
         //Showimage();
            //else if (thirdcord<x_axis&&x_axis<firstcord&&sevencord<y_axis&&y_axis<eightcord)
       // Showimage();
            //else  if (firstcord<x_axis&&x_axis<secndcord&&sevencord<y_axis&&y_axis<eightcord)
        //Showimage();
          //  else if(secndcord<x_axis&&x_axis<forthcord&&sevencord<y_axis&&y_axis<eightcord)
      //  Showimage();

        }

        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            x_axis = ev.getX();
            y_axis = ev.getY();
            Log.i("Tag","Rana asad");
            switch (ev.getAction())
            {
                case MotionEvent.ACTION_DOWN://avc

                    ;
                    invalidate();
                    break;
            }


            return true;
        }

        public void Showimage()
        {

           mFinalbitmap = Bitmap.createScaledBitmap(mFinalbitmap, width, height, false);
            canvas1.drawBitmap(mFinalbitmap, thirdcord, fifthcord, null);
        }
        public  void Showimage2()
        {
            mFinalbitmap = Bitmap.createScaledBitmap(mFinalbitmap, width, height, false);
            canvas1.drawBitmap(mFinalbitmap, firstcord, fifthcord, null);
        }

    }
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

onDraw()应该在那一刻绘制View的全部内容。它不是累积的;你必须再次绘制所有内容,因为你最后一次调用onDraw()时所画的内容并未保留。如果您只在最后触摸的方块中绘制图像,则只显示该图像。

这意味着你需要跟踪所有被触摸过的方块,并在onDraw()期间在每个方块中绘制图像。您的触摸事件代码应找出用户正在触摸的方块,将该方块标记为已触摸,然后invalidate()