获取坐标X和Y画布Android Image Translate

时间:2014-12-06 20:01:22

标签: android matrix path android-canvas translate

我正在尝试使用带路径的矩阵通过PATH移动图像,但我想知道图像的位置(X,Y)。但是我无法得到确切的X和Y.你知道我怎样才能获得图像的X和Y位置?

paint = new Paint();
      paint.setColor(Color.BLUE);
      paint.setStrokeWidth(1);
      paint.setStyle(Paint.Style.STROKE);             
      bm1 = BitmapFactory.decodeResource(getResources(), R.drawable.rock);
      bm_offsetX = bm1.getWidth()/2;
      bm_offsetY = bm1.getHeight()/2;       
      animPath = new Path();
      animPath.moveTo(sprite1.x-bm_offsetX,sprite1.y-bm_offsetY);
      animPath.quadTo(sprite1.x-bm_offsetX + 100*direction, -200, sprite1.x-bm_offsetX + 200*direction, getHeight());
      pathMeasure = new PathMeasure(animPath, false);
      pathLength = pathMeasure.getLength();         
      step = 5;
      distance = 0;
      pos = new float[2];
      tan = new float[2];       
      matrix = new Matrix();

canvas.drawPath(animPath, paint);
            if(distance < pathLength){
                pathMeasure.getPosTan(distance, pos, tan);                  
                matrix.reset();
                float degrees = (float)(Math.atan2(tan[1], tan[0])*180.0/Math.PI);
                matrix.postRotate(degrees, bm_offsetX, bm_offsetY);
                matrix.postTranslate(pos[0]-bm_offsetX, pos[1]-bm_offsetY);
                float []m = new float[9];
                matrix.getValues(m);
                float transX = m[Matrix.MTRANS_X] * -1;
                float transY = m[Matrix.MTRANS_Y] * -1;
                float scaleX = m[Matrix.MSCALE_X];
                float scaleY = m[Matrix.MSCALE_Y];
                String posXX = Integer.toString((int)(pos[0]-bm_offsetX));
                int posYY = (int)(transX/scaleX);
                sprite1.set(new Integer(posXX), posYY);
                System.out.println("STRING posXX "+  Math.abs(posYY));
                System.out.println("STRING posXX "+ new Integer(posXX));
                System.out.println("Sprite X "+ sprite1.x);
                System.out.println("Sprite Y "+ sprite1.y);
                canvas.drawBitmap(bm1, matrix, null);

                distance += step;
            }else{
                distance = 0;
            }
            if(step > 10f){
                step--;
            }else{
                step++;
            }

0 个答案:

没有答案