OnTouchListner не работает для масштабирования изображения
Я создал слайдер изображений с помощью видового пейджера, и я хочу сделать масштабирование этих изображений, но масштабирование изображения не работает с событием OnTouchListner. Это событие работает на одном изображении без ползунка, но я не понимаю, что пошло не так с приведенным ниже кодом в ImageAdapter для масштабирования изображения. Вот мой код
FullscreenImage.java
public class FullscreenImage extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen_image);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter1 adapter = new ImageAdapter1(getApplicationContext() ,FullscreenImage.this);
viewPager.setAdapter(adapter);
}
}
ImageAdapter1.java
public class ImageAdapter1 extends PagerAdapter {
Context context;
Activity activity;
private static final String TAG = "Touch" ;
// These matrices will be used to move and zoom image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
PointF start = new PointF();
public static PointF mid = new PointF();
private ProgressDialog pDialig;
// We can be in one of these 3 states
public static final int NONE = 0;
public static final int DRAG = 1;
public static final int ZOOM = 2;
public static int mode = NONE;
float oldDist;
private float[] matrixValues = new float[9];
private int[] GalImages = new int[] {
R.drawable.ic_animal,
R.drawable.ic_animal,
R.drawable.ic_animal
};
ImageAdapter1(Context context ,Activity activity){
this.activity = activity;
this.context=context;
}
@Override
public int getCount() {
return GalImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
imageView.setOnClickListener(new OnImageClickListener(position));
imageView.setOnTouchListener(new OnImageTouch());
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
class OnImageClickListener implements View.OnClickListener {
int _postion;
// constructor
public OnImageClickListener(int position) {
this._postion = position;
}
@Override
public void onClick(View v) {
// on selecting grid view image
// launch full screen activity
System.out.println("Hey --->"+_postion);
Intent i = new Intent(context, Touch.class);
i.putExtra("position", _postion);
context.startActivity(i);
}
}
class OnImageTouch implements View.OnTouchListener{
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
System.out.println("1");
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG");
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM" );
}
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
}
else if (mode == ZOOM) {
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG, "mode=NONE" );
break;
}
// Perform the transformation
view.setImageMatrix(matrix);
return true;
}
}
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);
}
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);
}}