【问题标题】:Images on canvas画布上的图像
【发布时间】:2016-05-27 17:26:30
【问题描述】:

我制作了一个井字游戏。这里我有图像,当我触摸该框时,我想将其放置在框中。问题是,当我触摸屏幕时,它会在画布上显示图像,但是当我再次触摸另一个框,然后第一个图像消失,该框中的新显示和 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);
        }

    }

【问题讨论】:

    标签: android canvas touch-event


    【解决方案1】:

    onDraw() 应该在那一刻绘制您视图的全部内容。它不是累积的;您必须重新绘制所有内容,因为您上次调用 onDraw() 时绘制的内容不会被保留。如果您只在您触摸的最后一个方格中绘制图像,则只会显示该图像。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      • 1970-01-01
      • 2014-11-03
      • 1970-01-01
      • 1970-01-01
      • 2014-06-09
      • 2022-01-04
      相关资源
      最近更新 更多