在Android中旋转,平移和缩放动态创建的图像视图

时间:2015-09-20 19:36:42

标签: android android-layout matrix

它可能是重复的问题,但我没有得到任何完美的线索或回答我的问题。

我正在尝试将图像从一个视图拖放到另一个视图,并尝试应用手指触摸等翻译,缩放和旋转图像视图等操作。 我经历了相同的大部分链接,但我得到的只是图像得到翻译,缩放和旋转图像矩阵,即图像在其图像视图中旋转。

问题是想要用图像重新调整图像视图的大小。我为翻译和缩放解决了这个问题,但是在进行旋转时我遇到了困难。

以下是我的代码。

 switch (event.getAction() & MotionEvent.ACTION_MASK) {

                                case MotionEvent.ACTION_DOWN: //first finger down only
                                    savedMatrix.set(matrix);
                                    start.set(event.getX(), event.getY());

                                    Log.d(TAG, "mode=DRAG");
                                    mode = DRAG;
                                    break;

                                case MotionEvent.ACTION_POINTER_DOWN:
                                    oldDist = spacing(event);
                                    if (oldDist > 10f) {
                                        savedMatrix.set(matrix);
                                        midPoint(mid, event);
                                        mode = ZOOM;
                                    }
                                    lastEvent = new float[4];
                                    lastEvent[0] = event.getX(0);
                                    lastEvent[1] = event.getX(1);
                                    lastEvent[2] = event.getY(0);
                                    lastEvent[3] = event.getY(1);
                                    d = rotation(event);
                                    break;

                                case MotionEvent.ACTION_UP: //first finger lifted
                                    delete.setImageResource(R.drawable.delete);
                                    mode = NONE;

                                   case MotionEvent.ACTION_POINTER_UP:                                   //second finger lifted
                                    mode = NONE;
                                    Log.d(TAG, "mode=NONE");
                                    break;


                          case MotionEvent.ACTION_MOVE:
                          if (mode == DRAG) {
                                        delete.setImageResource(R.drawable.share);
                                        matrix.set(savedMatrix);
                                        matrix.postTranslate(event.getX() - start.x, event.getY()- start.y);
                                        float[] matrixValues = new float[9];
                                        matrix.getValues(matrixValues);
                                        float TransX = matrixValues[Matrix.MTRANS_X];
                                        float TransY = matrixValues[Matrix.MTRANS_Y];
                                        draggedImg.setX(draggedImg.getX()+TransX);
                                        draggedImg.setY(draggedImg.getY()+TransY);
                                         draggedImg.setPivotX(draggedImg.getX()+TransX+draggedImg.getWidth()/2);
                                         draggedImg.setPivotY(draggedImg.getY()+TransY-draggedImg.getHeight()/2);


                   } else if (mode == ZOOM && event.getPointerCount() == 2) {
                                        Log.d(TAG, "In Zoom Mode");
                                        float newDist = spacing(event);
                                        matrix.set(savedMatrix);
                                        if (newDist > 10f) {
                                                Drawable drawing = draggedImg.getDrawable();
                                                Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
                                                scale = newDist / oldDist;
                                                matrix.postScale(scale, scale, mid.x, mid.y);

                                                float[] matrixValues = new float[9];
                                                matrix.getValues(matrixValues);
                                                scaleX = matrixValues[Matrix.MSCALE_X];
                                                scaleY = matrixValues[Matrix.MSCALE_Y];

                                                params.width = (int)(bitmap.getWidth()*scaleX);
                                                params.height =(int)(bitmap.getHeight()*scaleY);

                                                float TransX = matrixValues[Matrix.MTRANS_X];
                                                float TransY = matrixValues[Matrix.MTRANS_Y];
                                                draggedImg.setPivotX(mid.x);
                                                draggedImg.setPivotY(mid.y);

                         draggedImg.setScaleType(ImageView.ScaleType.FIT_CENTER);
                                                draggedImg.setImageMatrix(matrix);
                                                draggedImg.setLayoutParams(params);
                                        }
                                        if (lastEvent != null) {
                                            Drawable drawing = draggedImg.getDrawable();
                                            Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
                                                float newRot = rotation(event);
                                                float r = newRot - d;                                               

                      draggedImg.setScaleType(ImageView.ScaleType.MATRIX);
                                                matrix.postRotate(r, v.getMeasuredWidth() / 2,v.getMeasuredHeight() / 2);
                                               float[] matrixValues = new float[9];
                                               matrix.getValues(matrixValues);

  }

                                    }
                       }

                            return true; // indicate event was handled

                        }
                    });

1 个答案:

答案 0 :(得分:4)

请尝试MultiTouchLibrary。它将满足您的需求 该库为您提供以下功能

  1. 旋转
  2. 缩放图像(缩放以缩放)