我想在相对布局中添加动态图像视图,并且此图像视图在OnTouch事件上的任何位置移动/缩放/旋转
代码在相对布局中添加动态Imageview:
private void Mount_Image(String btn_id ) {
// TODO Auto-generated method stub
final ImageView my_ballon = new ImageView(this);
my_ballon.setTag(btn_id);
my_ballon.setScaleType(ScaleType.MATRIX);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
my_ballon.setLayoutParams(lp);
my_ballon.setImageResource(R.drawable.mustache_1);
rel_image.addView(my_ballon);
// Add_Drag_hadler(btn_id);
my_ballon.setOnTouchListener(this);
}
在rel_image中添加Imageview(相对布局)。
以下是ontouch事件的代码:
@Override
public boolean onTouch(View v, MotionEvent event) {
// handle touch events here
ImageView view = (ImageView) v;
final float x = event.getX();
final float y = event.getY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
mode = DRAG;
lastEvent = null;
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:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
lastEvent = null;
break;
case MotionEvent.ACTION_MOVE:
if (!rotate_enabled) {
if (mode == DRAG) {
view.requestLayout();
matrix.set(savedMatrix);
float dx = event.getX() - start.x;
float dy = event.getY() - start.y;
matrix.postTranslate(dx, dy);
view.invalidate();
} else if (mode == ZOOM) {
float newDist = spacing(event);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = (newDist / oldDist);
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
}
else {
if (lastEvent != null && event.getPointerCount() == 2) {
view.requestLayout();
newRot = rotation(event);
float r = newRot - d;
float[] values = new float[9];
matrix.getValues(values);
float tx = values[2];
float ty = values[5];
float sx = values[0];
float xc = (view.getWidth() / 2) * sx;
float yc = (view.getHeight() / 2) * sx;
// matrix.postRotate(r, tx + xc, ty + yc);
animate(r, tx + xc, 0,view);
// lastEvent=null;
}
}
break;
}
view.setImageMatrix(matrix);
return true;
}
/**
* Determine the space between the first two fingers
*/
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);
}
/**
* Calculate the mid point of the first two fingers
*/
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 void animate(double fromDegrees, double toDegrees, long durationMillis,ImageView wheel) {
final RotateAnimation rotate = new RotateAnimation((float)fromDegrees, (float)toDegrees,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rotate.setDuration(durationMillis);
rotate.setFillEnabled(true);
rotate.setFillAfter(true);
wheel.startAnimation(rotate);
}
/**
* Calculate the degree to be rotated by.
*
* @param event
* @return Degrees
*/
private float rotation(MotionEvent event) {
double delta_x = (event.getX(0) - event.getX(1));
double delta_y = (event.getY(0) - event.getY(1));
double radians = Math.atan2(delta_y, delta_x);
return (float) Math.toDegrees(radians);
}
Imageview拖动和缩放工作正常但当我尝试旋转图像时,这会围绕背景中心旋转...我需要在自我周围旋转图像
答案 0 :(得分:0)
更改这些行:
Animation.RELATIVE_TO_SELF
至Animation.RELATIVE_TO_PARENT
final RotateAnimation rotate = new RotateAnimation((float)fromDegrees, (float)toDegrees,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
到
final RotateAnimation rotate = new RotateAnimation((float)fromDegrees, (float)toDegrees,
Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT,
0.5f);
答案 1 :(得分:0)
问题是旋转枢轴。试试这个:
matrix.postTranslate(-mid.x, -mid.y);
matrix.postRotate(r);
matrix.postTranslate(mid.x, mid.y);