29

我想缩放、移动、调整图像大小。我想用指示器包围图像,指导用户这些指示器执行什么操作,即移动、旋转、缩放。

在此处输入图像描述

我试过了

  • 缩放 - 但它只会减少。没有机会增加图像的长度和高度。
  • 旋转 - 实现但通过插入手动度数。这不是一个好的选择。
  • 移动 - 因为我正在实现 Api < 11 的阻力。所以有点困难。在这里仍然没有希望。

是否有任何库可以让我进行简单的图像编辑[移动、缩放、旋转]?

4

8 回答 8

6

您可以使用矩阵变换来实现旋转、缩放和移动....希望对您有所帮助....

用于旋转

Matrix matrix = new Matrix();
    matrix.postRotate(90);

用于缩放

 matrix.postScale(scaleWidth, scaleHeight);

在不更改原始位图的情况下创建新位图...像这样

Bitmap bmp2 = Bitmap.createBitmap(bmp1, 0, 0, pow2, pow2, matrix, true);
于 2013-02-10T16:57:26.670 回答
4

只需查看此图像裁剪参考:

http://www.londatiga.net/featured-articles/how-to-select-and-crop-image-on-android/

图像旋转,参考这个例子即可;此示例代码动态表示图像的旋转:

http://android-er.blogspot.in/2010/07/rotate-bitmap-image-using-matrix.html

此链接对于图像裁剪、旋转和缩放也很有用:

https://github.com/Yalantis/uCrop

于 2013-02-19T09:18:13.533 回答
4

使用库:StickerView

它是一个第 3 方图书馆,它提供了我正在寻找的东西。

解决方法:

这些答案仍然是我在 OP 中要求的一半。表示它没有被某些特定指标包围。我仍在寻找如何ImageView用指标包装并将它们用于翻译和调整大小。

Activity初始化对翻译和调整大小很重要的变量ImageView

public static final int DRAG = 1;
public static final int NONE = 0;
private static final String TAG = "Touch";
public static final int ZOOM = 2;
public static PointF mid = new PointF();
public static int mode = 0;
float d = 0.0F;
Matrix savedMatrix = new Matrix();
Matrix matrix = new Matrix();
PointF start = new PointF();

设置ImageView为 scaleType -Matrix

iv = new ImageView(this);
iv.setPadding(10, 10, 25, 25);
iv.setScaleType(ImageView.ScaleType.MATRIX);

iv.setOnTouchListener(t);

添加使用的 onTouch 监听ImageView

  • 1 指翻译 - 拖动
  • 2 指缩放 - 调整大小 {Pinch To Zoom}

    View.OnTouchListener t = new View.OnTouchListener()
    {
    public boolean onTouch(View paramView, MotionEvent event)
    {
      ImageView view = (ImageView)paramView;
      switch (event.getAction() & MotionEvent.ACTION_MASK)
      {
      case MotionEvent.ACTION_DOWN:
    
          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);
          Log.d(TAG, "oldDist=" + oldDist);
          if (oldDist > 10f) {
    
              savedMatrix.set(matrix);
              midPoint(mid, event);
              mode = ZOOM;
              Log.d(TAG, "mode=ZOOM" );
          }
          break;
    
      case MotionEvent.ACTION_MOVE:
    
          if (mode == DRAG) {
    
              matrix.set(savedMatrix);
              matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
          }
          else if (mode == ZOOM) {
    
              float newDist = spacing(event);
              Log.d(TAG, "newDist=" + newDist);
              if (newDist > 10f) {
    
                  matrix.set(savedMatrix);
                  float scale = newDist / oldDist;
                  matrix.postScale(scale, scale, mid.x, mid.y);
              }
          }
          break;
    
      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_POINTER_UP:
    
          mode = NONE;
          Log.d(TAG, "mode=NONE" );
          break;
      }  
      view.setImageMatrix(matrix);
      return true;
    
    }
    
     private void midPoint(PointF point, MotionEvent event) {
    
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        point.set(x / 2, y / 2);
    }
    
          private float spacing(MotionEvent event) {
          float x = event.getX(0) - event.getX(1);
          float y = event.getY(0) - event.getY(1);
          return FloatMath.sqrt(x * x + y * y);
          }
         };
    
于 2013-02-19T14:41:29.137 回答
3

我最近尝试了同样的事情,但最终使用了一个库,因为它涵盖了更多的边缘情况。这是一个很棒且使用非常广泛的库:

http://code.google.com/p/android-multitouch-controller/

于 2013-02-19T01:00:05.690 回答
2

我想我可以在轮换部分为您提供帮助。我最近制作了一个 android 轮菜单库。它跟踪用户的手部动作并相应地旋转图像视图。你可以查看源代码,看看我是怎么做到的:

https://github.com/anupcowkur/Android-Wheel-Menu

于 2013-02-18T08:14:10.127 回答
2

我知道这不是一个答案,但值得一提。

看看这个项目。我打算把它放在 GitHub 上,但它还没有完成(根本)。

有一个按钮可以旋转图像。我仍在努力“保存”。由于Lorenzo Villani 的项目,许多类都取自 Android 源代码

添加到项目的清单中:

    <activity
        android:name="mobi.sherif.camera.CropImage"
        android:hardwareAccelerated="false"
        android:screenOrientation="portrait" >

使用以下方式调用活动:

Intent intent = new Intent(mContext, CropImage.class);
intent.setData(uri);
startActivityForResult(intent, S.request.cropimage);

在此处输入图像描述

于 2013-02-20T07:57:50.167 回答
1

位图旋转

Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, config);
Canvas canvas = new Canvas(targetBitmap);
Matrix matrix = new Matrix();
matrix.setRotate(mRotation,source.getWidth()/2,source.getHeight()/2);
canvas.drawBitmap(source, matrix, new Paint());

图像缩放 缩放

AssetManager assetManager = context.getAssets();
InputStream imageIn;
String imgPath = "test.png";
Bitmap image;
try {
    imageIn = assetManager.open(imgPath, AssetManager.ACCESS_BUFFER);
    image = BitmapFactory.decodeStream(imageIn);
} catch (IOException e) {

}
//TODO: calculate width and height to fill or fit screen
image = Bitmap.createScaledBitmap(image, width, height, true);

带有触摸事件的画布中的图像移动图像

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

public class ImgView extends View {

private static final int INVALID_POINTER_ID = -1;

    private Drawable mImage;
    private float mPosX;
    private float mPosY;

    private float mLastTouchX;
    private float mLastTouchY;
    private int mActivePointerId = INVALID_POINTER_ID;

    private ScaleGestureDetector mScaleDetector;
    private float mScaleFactor = 1.f;

    public ImgView(Context context) {
        this(context, null, 0);
    mImage = getResources().getDrawable(R.drawable.imagename);

        mImage.setBounds(0, 0, mImage.getIntrinsicWidth(), mImage.getIntrinsicHeight());
    }

    public ImgView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ImgView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // Let the ScaleGestureDetector inspect all events.
        mScaleDetector.onTouchEvent(ev);

        final int action = ev.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN: {
            final float x = ev.getX();
            final float y = ev.getY();

            mLastTouchX = x;
            mLastTouchY = y;
            mActivePointerId = ev.getPointerId(0);
            break;
        }

        case MotionEvent.ACTION_MOVE: {
            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(pointerIndex);
            final float y = ev.getY(pointerIndex);

            // Only move if the ScaleGestureDetector isn't processing a gesture.
            if (!mScaleDetector.isInProgress()) {
                final float dx = x - mLastTouchX;
                final float dy = y - mLastTouchY;

                mPosX += dx;
                mPosY += dy;

                invalidate();
            }

            mLastTouchX = x;
            mLastTouchY = y;

            break;
        }

        case MotionEvent.ACTION_UP: {
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }

        case MotionEvent.ACTION_CANCEL: {
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }

        case MotionEvent.ACTION_POINTER_UP: {
            final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) 
                    >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
            final int pointerId = ev.getPointerId(pointerIndex);
            if (pointerId == mActivePointerId) {
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mLastTouchX = ev.getX(newPointerIndex);
                mLastTouchY = ev.getY(newPointerIndex);
                mActivePointerId = ev.getPointerId(newPointerIndex);
            }
            break;
        }
        }

        return true;
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.save();
        Log.d("DEBUG", "X: "+mPosX+" Y: "+mPosY);
        canvas.translate(mPosX, mPosY);
        canvas.scale(mScaleFactor, mScaleFactor);
        mImage.draw(canvas);
        canvas.restore();
    }

    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            mScaleFactor *= detector.getScaleFactor();

            // Don't let the object get too small or too large.
            mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f));

            invalidate();
            return true;
        }
    }

}
于 2013-02-19T08:30:20.073 回答
1

您可以https://github.com/siddhpuraamitr/MultitouchWithDynamicAddandRemove在您的项目中尝试此代码

于 2015-07-08T11:00:55.637 回答