I'm using Android Studio and trying to write a Pinch Zooom Class for ViewPager. Zoom is fine, but the image is stuck at the top left corner of the screen. I know that this question has been asked multiple times but I have gone through each one none of them is working for me.
Code:
public class PinchZoom implements View.OnTouchListener {
public static enum State {Zoom, None}
private State curentState = State.None;
private float initialDistance = 0;
private PointF firstTouchLocation = new PointF();
private PointF midPoint = new PointF();
private Matrix matrix = new Matrix();
private Matrix savedMatrix = new Matrix();
Context mContext;
PinchZoom(Context context){
mContext = context;
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int maskedAction = motionEvent.getActionMasked();
PointF location = new PointF(motionEvent.getX(), motionEvent.getY());
ImageView imageView= (ImageView) view;
switch (maskedAction){
case MotionEvent.ACTION_DOWN:{
firstTouchLocation.set(location);
//matrix = imageView.getImageMatrix();
savedMatrix.set(matrix);
break;
}
case MotionEvent.ACTION_POINTER_DOWN:{
initialDistance = setDistance(motionEvent);
if(initialDistance > 20f){
setMidPoint(motionEvent);
curentState = State.Zoom;
savedMatrix.set(matrix);
}
break;
}
case MotionEvent.ACTION_MOVE:{
if(curentState == State.Zoom){
float currentDistance = setDistance(motionEvent);
float scaleFactor = currentDistance / initialDistance;
matrix.set(savedMatrix);
matrix.postScale(scaleFactor, scaleFactor, midPoint.x, midPoint.y);
}
break;
}
case MotionEvent.ACTION_UP:{
curentState = State.None;
break;
}
case MotionEvent.ACTION_POINTER_UP:{
curentState = State.None;
break;
}
}
imageView.setImageMatrix(matrix);
imageView.invalidate();
return true;
}
public float setDistance(MotionEvent event){
float distX = event.getX() - firstTouchLocation.x;
float distY = event.getY() - firstTouchLocation.y;
float distance = (float) Math.sqrt(distX * distX + distY * distY);
return distance;
}
public void setMidPoint(MotionEvent event){
float x = (event.getX() + firstTouchLocation.x ) / 2;
float y = (event.getY() + firstTouchLocation.y) / 2;
midPoint.set(x, y);
}
}
public class MyPagerAdapter extends PagerAdapter {
private ArrayList<ImageView> imageViewList;
private ArrayList<Bitmap> bitmapArrayList;
private Context mContext;
public MyPagerAdapter(Context mContext){
this.mContext = mContext;
}
@Override
public int getCount() {
return imageViewList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup parent, int position){
ImageView imageView = imageViewList.get(position);
imageView.setScaleType(ImageView.ScaleType.MATRIX);
imageView.setOnTouchListener(new PinchZoom(mContext));
if(imageView.getParent() != null){
((ViewGroup) imageView.getParent()).removeView(imageView);
}
parent.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup parent, int position, Object view){
imageViewList.remove(position);
}
public void setList(ArrayList<Bitmap> list){
bitmapArrayList = list;
imageViewList = new ArrayList<>();
for (Bitmap b : bitmapArrayList) {
ImageView image = new ImageView(mContext);
image.setImageBitmap(b);
imageViewList.add(image);
}
}
public ArrayList<Bitmap> getList(){
return bitmapArrayList;
}
}
Thanks!
Aucun commentaire:
Enregistrer un commentaire