获取X和y相对布局夹点缩放子视图的坐标?

时间:2014-11-14 13:04:40

标签: android coordinates pinchzoom

//这里是xml

   <com.example.zoomrelative.ZoomableRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/rlContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.zoomrelative.MainActivity$PlaceholderFragment" >

        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:contentDescription="@string/app_name"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/ic_launcher" />

    </com.example.zoomrelative.ZoomableRelativeLayout>

// Java代码

    public class MainActivity extends ActionBarActivity {

        private ZoomableRelativeLayout rlContainer;
        private ScaleGestureDetector scaleGestureDetector;
        private ImageView image;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            image = (ImageView) findViewById(R.id.image);

            rlContainer = (ZoomableRelativeLayout) findViewById(R.id.rlContainer);

//gesture listener
            scaleGestureDetector = new ScaleGestureDetector(this, new OnPinchListener());

            rlContainer.setOnTouchListener(new OnTouchListener() {

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

        }
        // pinch zoom on view
        private class OnPinchListener extends SimpleOnScaleGestureListener {

            float startingSpan; 
            float startFocusX;
            float startFocusY;


            public boolean onScaleBegin(ScaleGestureDetector detector) {
                startingSpan = detector.getCurrentSpan();
                startFocusX = detector.getFocusX();
                startFocusY = detector.getFocusY();
                return true;
            }


            @SuppressLint("NewApi")
            public boolean onScale(ScaleGestureDetector detector) {
                rlContainer.scale(detector.getCurrentSpan() / startingSpan,
                        startFocusX, startFocusY);
                rlContainer.requestFocus();
                image.requestFocus();

                int[] posXY = new int[2];
                image.getLocationOnScreen(posXY);
                int x = posXY[0];
                int y = posXY[1];
                Log.e("X-Y", x + " - " + y);

                return true;
            }

            public void onScaleEnd(ScaleGestureDetector detector) {
                //rlContainer.restore();
            }
        }
    }

//这里我使用了上面的代码,但是我在缩放缩放时得到相同的x和y坐标,我想进入imageview的当前x和y坐标。请给我解决方案如果你有。感谢。

0 个答案:

没有答案