处理捏缩放

时间:2015-12-29 21:37:46

标签: android android-studio pinchzoom

我有一个扩展Fragment的课程。我正在尝试进行捏缩放。但它不起作用。我已经尝试过每一段代码,但它不起作用。 我没有在输出中得到任何错误没有调试器错误它只是当我们尝试从我的手指扩展它保持完整它不起作用的东西。 我的xml文件是这样的:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:src="@drawable/mypic"
    android:scaleType="matrix"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

,测试地图活动就是这样:

public class MapFragment extends Fragment implements View.OnTouchListener{


private ScaleGestureDetector SGD;
private ImageView imageView;
private float scale = 1f;
private Matrix matrix = new Matrix();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_map, container, false);
    imageView = (ImageView)view.findViewById(R.id.imageView);
    SGD = new ScaleGestureDetector(getContext(),new ScaleListener());
    return view;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    SGD.onTouchEvent(event);
    return true;
}


private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener{
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        scale  = scale*detector.getScaleFactor();
        scale = Math.max(0.1f,Math.min(scale,5.0f));
        matrix.setScale(scale,scale);
        imageView.setImageMatrix(matrix);
        return true;
    }
}}

0 个答案:

没有答案