我能够在单个 imageView 上实现拖动和缩放,从这里也是代码;
public class Touch extends Activity {
private static final String TAG = "Touch";
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
private PointF start = new PointF();
private PointF mid = new PointF();
private float oldDist = 1f;
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
ImageView tv1;
LayoutParams layoutParams1;
ImageView tv2;
LayoutParams layoutParams2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final int windowwidth = getWindowManager().getDefaultDisplay()
.getWidth();
final int windowheight = getWindowManager().getDefaultDisplay()
.getHeight();
tv1 = (ImageView) findViewById(R.id.imageView);
tv1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
layoutParams1 = (RelativeLayout.LayoutParams) tv1
.getLayoutParams();
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams1.leftMargin = x_cord - 25;
layoutParams1.topMargin = y_cord - 75;
tv1.setLayoutParams(layoutParams1);
break;
default:
break;
}
return true;
}
});
tv2 = (ImageView) findViewById(R.id.imageView2);
tv2.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
layoutParams2 = (RelativeLayout.LayoutParams) tv2
.getLayoutParams();
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams2.leftMargin = x_cord - 25;
layoutParams2.topMargin = y_cord - 75;
tv2.setLayoutParams(layoutParams2);
break;
default:
break;
}
return true;
}
});
}
}
这是xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="matrix"
android:src="@drawable/ic_launcher" />
</LinearLayout>
现在我的问题是我想动态添加多个 imageView 并在每个 imageView 上应用拖动、缩放。谁能帮我实现这一目标?我见过很多其他方式,但都有一些限制。