我是 ANDROID 的新手,想在我自己的自定义视图中旋转、缩放和移动图像。但是我在同一个 onTouchEvent 方法中编写了所有这些(旋转、缩放、移动)。因此,当我移动图像时,它也会旋转或缩放图像,它会被移动。我必须分别处理它们,但不知道如何处理。我想我可以添加一个按钮,例如名称是“旋转”。所以我可以修复图像,它只允许旋转。但我无法在我的自定义视图中创建按钮。代码如下所示:
公共类 MyView 扩展视图 {
// The ‘active pointer’ is the one currently moving our object.
private int mActivePointerId = INVALID_POINTER_ID;
private static final int INVALID_POINTER_ID = -1;
private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.f;
private float mPosX;
private float mPosY;
private float mLastTouchX;
private float mLastTouchY;
//For Fetching image
private Bitmap bmp;
private Drawable image;
private byte[] byteArray;
private ByteArrayBuffer baf = null;
//For Rotation
int direction = 0;
int degree = 0;
private float centerX ;
private float centerY ;
private float newX ;
private float newY ;
private float rotateX;
private float rotateY;
public MyView(Context context) {
this(context, null, 0);
}
public MyView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
String url = "http://www.queness.com/resources/images/png/apple_ex.png";
AsyncTask<String,Void,ByteArrayBuffer> connection=new GetUrlData().execute(url);
try {
baf=connection.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
byteArray = baf.toByteArray();
bmp = BitmapFactory.decodeByteArray(byteArray, 0 , byteArray.length);
image =new BitmapDrawable(context.getResources() , bmp);
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.BLACK);
//For Rotation
int height = this.getHeight();
int width = this.getWidth();
centerX = width/2;
centerY = height/2;
canvas.rotate(direction, width / 2, height / 2);
canvas.save();
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor);
image.draw(canvas);
canvas.restore();
}
//For Rotation
private void updateRotation(float newX2, float newY2) {
degree = (int)Math.toDegrees(Math.atan2(newY, newX))-90;
setDirection(degree);
}
//For Rotation
public void setDirection(int direction) {
this.direction = direction;
this.invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Let the ScaleGestureDetector inspect all events.
mScaleDetector.onTouchEvent(event);
final int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
final float x = event.getX();
final float y = event.getY();
mLastTouchX = x;
mLastTouchY = y;
mActivePointerId = event.getPointerId(0);
}
if (action == MotionEvent.ACTION_MOVE) {
final int pointerIndex = event.findPointerIndex(mActivePointerId);
final float x = event.getX(pointerIndex);
final float y = event.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;
//For Rotation
rotateX = event.getX();
rotateY = event.getY();
newX = centerX-rotateX;
newY = centerY-rotateY;
updateRotation( newX, newY);
}
if (action == MotionEvent.ACTION_UP) {
mActivePointerId = INVALID_POINTER_ID;
updateRotation( newX, newY);
}
if (action == MotionEvent.ACTION_CANCEL) {
mActivePointerId = INVALID_POINTER_ID;
}
if (action == MotionEvent.ACTION_POINTER_UP) {
final int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = event.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 = event.getX(newPointerIndex);
mLastTouchY = event.getY(newPointerIndex);
mActivePointerId = event.getPointerId(newPointerIndex);
}
}
return true;
}
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;
}
}
}
我希望我能说出我想要什么。你能帮我解决这个问题吗?