我想向规模化,移动,缩放图片。 我想围绕与指标,引导用户何种操作这些指标进行,即移动,旋转,缩放图像。
我试过了
- 缩放 - 但它只是庄稼下来。 没有机会来提高图像的长度和高度。
- 旋转 - 实现,但通过插入手动度。 这不是在这里一个很好的选择。
- 移动 - 因为我实现对API <11的困难所以有点拖。 在这里仍然没有希望。
有没有可以做我的简单的图像编辑[移动,缩放,旋转]任何库?
我想向规模化,移动,缩放图片。 我想围绕与指标,引导用户何种操作这些指标进行,即移动,旋转,缩放图像。
我试过了
有没有可以做我的简单的图像编辑[移动,缩放,旋转]任何库?
您可以使用矩阵transaformation实现旋转,缩放和移动....我希望这会帮助你....
旋转
Matrix matrix = new Matrix();
matrix.postRotate(90);
缩放
matrix.postScale(scaleWidth, scaleHeight);
创建新的位图,而不改变原来的......像这样
Bitmap bmp2 = Bitmap.createBitmap(bmp1, 0, 0, pow2, pow2, matrix, true);
使用库: StickerView
它是一个第三方库赋予正是我一直在寻找。
解决方法:
不过这些答案是什么,我问过在OP一半。 意味着它不是由一些具体的指标surronded。 我还在寻找如何包装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
它使用
2手指缩放 - 调整{指缩放}
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); } };
只要看看这个图片裁剪参考:
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
我最近尝试同样的事情,但最终使用库,因为它涵盖了很多边缘情况。 这是一个伟大的,漂亮的广泛使用的库:
http://code.google.com/p/android-multitouch-controller/
我想我可以帮你转动件。 我最近在Android轮菜单库。 它跟踪用户的手部动作,并相应地旋转ImageView的。 您可以检查出的源代码,看看我是如何做到在这里:
https://github.com/anupcowkur/Android-Wheel-Menu
你可以https://github.com/siddhpuraamitr/MultitouchWithDynamicAddandRemove尝试在你的项目这段代码
我知道这是不是一个答案,但它是值得一提。
看看这个项目 。 我打算把它放在GitHub上,但还没有完成(所有)。
有一个按钮,旋转图像。 我仍然工作在“保存”。 许多类都是从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);
位图旋转
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;
}
}
}