在我使用Android Studio的当前应用程序中,我需要动态地将一些ImageView添加到我的FrameLayout中。用户将从图库中选择一个或多个图像,我将图像的路径放在一个arraylist中,将其传递给将使用它的活动,然后创建一个drawable并将其设置为每个所选图像的imageview。
这样工作正常,但我需要使用onTouchListener移动,旋转和调整它们。问题是我只能移动最后添加的ImageView。这就像最后一个ImageView是一个专注的,它占用了同一个ImageView的所有屏幕尺寸,我无法移动其余部分。 我试着用“ids”做一些我发现寻找答案的想法,但它仍然总是得到相同的id,从最后添加的ImageView。 Here you can see both imageviews, but just the one on the right it's moving when I touch anywhere on the screen
这是我的XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:id="@+id/FrameLayout"
tools:context="com.example.anna.tfg3.WindowWorld"
android:foregroundGravity="bottom"></FrameLayout>
我的代码:
for(int i = 0 ; i < selectedList.size(); i++){ //List with selected image paths
ImageView characterView = new ImageView(this);//Creating one imageview for each selected image
characterView.setOnTouchListener(this); //I add the listener to each one
Drawable charDrw = Drawable.createFromPath(selectedList.get(i));
charDrw.createFromPath(selectedList.get(i)); //Create the drawable foreach image path selected
characterView.setImageDrawable(charDrw); //I set the drawable for each image view
characterView.setScaleType(ImageView.ScaleType.MATRIX);
fl.addView(characterView); //Add it to the frame layout
}
我的onTouchListener:
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
System.out.println("/777777777777777777777777"+v.getId());
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
startPoint.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 (mode == DRAG) {
matrix.set(savedMatrix);
float dx = event.getX() - startPoint.x;
float dy = event.getY() - startPoint.y;
matrix.postTranslate(dx, dy);
} 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);
}
if (lastEvent != null && event.getPointerCount() == 3) {
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);
}
}
break;
}
view.setImageMatrix(matrix);
return true;
}
非常感谢!