如何在 Android Imageview 中一起执行旋转、缩放和拖动
问问题
2950 次
2 回答
2
对于旋转、缩放和旋转,试试这个。工作100%。
于 2013-04-03T10:07:52.540 回答
1
你可以尝试这样的事情:
package com.my.test;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.ScaleGestureDetector.OnScaleGestureListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class ImageViewActivity extends Activity implements OnClickListener, SurfaceHolder.Callback {
ImageView view;
final static String TAG = "TAG";
ScaleGestureDetector scaleGestureDetector;
Thread mDrawingThread;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*PinchImageView view = new PinchImageView(this);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
setContentView(view);*/
setContentView(R.layout.main);
PinchImageView view = (PinchImageView) findViewById(R.id.image);
view.setDrawable(getResources().getDrawable(R.drawable.pic));
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
button = (Button) findViewById(R.id.button2);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.button1) {
PinchImageView view = (PinchImageView) findViewById(R.id.image);
view.setDrawable(getResources().getDrawable(R.drawable.pic));
}
if(v.getId() == R.id.button2) {
PinchImageView view = (PinchImageView) findViewById(R.id.image);
view.setDrawable(getResources().getDrawable(R.drawable.penguins));
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
/**
* @author
*
*/
public class PinchImageView extends SurfaceView {
final String TAG = "TAG";
final static int HORIZONTAL_SNAP_GAP = 30;
final static int VERTICAL_SNAP_GAP = 50;
private static final int INVALID_POINTER_ID = -1;
private Drawable mIcon;
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 PinchImageView(Context context) {
this(context, null, 0);
}
public PinchImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public PinchImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
//mIcon = context.getResources().getDrawable(R.drawable.pic);
//mIcon.setBounds(0, 0, mIcon.getIntrinsicWidth(), mIcon.getIntrinsicHeight());
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;
if(mPosX > HORIZONTAL_SNAP_GAP) mPosX = HORIZONTAL_SNAP_GAP;
if(mPosY > VERTICAL_SNAP_GAP) mPosY = VERTICAL_SNAP_GAP;
Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int displayWidth = display.getWidth();
int displayHeight = display.getHeight();
if(mPosX + mIcon.getBounds().width() < getWidth() - HORIZONTAL_SNAP_GAP) mPosX = getWidth() - HORIZONTAL_SNAP_GAP - mIcon.getBounds().width();
if(mPosY + mIcon.getBounds().height() < getHeight() - VERTICAL_SNAP_GAP) mPosY = getHeight() - VERTICAL_SNAP_GAP - mIcon.getBounds().height();
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);
}
/*if(mPosX > 0 || mPosY > 0) {
Animation an = new TranslateAnimation(mPosX, 0, mPosY, 0);
an.setDuration(2000);
an.setRepeatCount(-1);
//an.initialize(10, 10, 10, 10);
setAnimation(an);
an.startNow();
invalidate();
}*/
break;
}
}
return true;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor);
if(mIcon != null) mIcon.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, 5.0f));
invalidate();
return true;
}
}
public void setDrawable(Drawable draw) {
mIcon = draw;
mIcon.setBounds(0, 0, mIcon.getIntrinsicWidth(), mIcon.getIntrinsicHeight());
invalidate();
}
}
于 2012-07-19T10:00:44.733 回答