在精确的触摸位置缩放图像

时间:2017-12-27 19:37:12

标签: java android dialogfragment

我正在尝试制作对话框以显示全屏图像+在Instagram中缩放它。但它是按中心缩放,而不是触摸位置。如何通过触摸位置进行缩放? 我试过这个,我找到了比例类型

public class DFragment extends DialogFragment {

    public final static String BITMAP_ARG="BITMAP_ARG";

    private ScaleGestureDetector mScaleGestureDetector;
    private ImageView mImageView;
    float factor;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {



        View rootView=inflater.inflate(R.layout.photo_dialog_view,container,false);
        mImageView=((ImageView)rootView.findViewById(R.id.photo_dialog_image_view));
        mImageView.setScaleType(ImageView.ScaleType.MATRIX);
        String path=getArguments().getString(BITMAP_ARG);
        if(path!=null){
            mImageView.setImageBitmap(PictureUtils.getScaledBitmap(path,getActivity()));

        }

        mScaleGestureDetector=new ScaleGestureDetector(getActivity(),new MySimpleOnScaleGestureDetector(mImageView));

        rootView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                mScaleGestureDetector.onTouchEvent(motionEvent);
                return true;
            }
        });
        return rootView;
    }

    public class MySimpleOnScaleGestureDetector extends ScaleGestureDetector.SimpleOnScaleGestureListener{

        ImageView mImageView;
        public MySimpleOnScaleGestureDetector(ImageView v) {
            super();
            mImageView=v;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            factor=1.0f;
            return true;

            //return super.onScaleBegin(detector);
        }

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            float scaleFactor=detector.getScaleFactor()-1;

            factor+=scaleFactor;
            mImageView.setScaleX(factor);
            mImageView.setScaleY(factor);
            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            super.onScaleEnd(detector);
            mImageView.setScaleX(1.0f);
            mImageView.setScaleY(1.0f);
        }
    }
}

哪种比例类型更好用?或者我应该以某种方式从事件中获取触摸坐标并使用它们?

UPD:问题已关闭,我使用了库Zoomy

0 个答案:

没有答案