如何在捏缩放后获得imageview的高度和宽度?

时间:2014-09-08 08:57:17

标签: android image pinchzoom

在我的应用程序中,我希望在缩放图像后获得图像视图的高度和宽度。这是缩放图像视图的代码。这是onCreate方法..

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

        btnGet=(Button) findViewById(R.id.get);
        relImage=(RelativeLayout) findViewById(R.id.image);
        imgMarker = (ImageView) findViewById(R.id.imagemarker);



        btnGet.setOnClickListener(this);

        dragImage=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        imgMarker.setImageBitmap(dragImage);

        imgMarker.setOnTouchListener(this);


    }

这是ontouch方法..

public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
        ImageView view = (ImageView) v;
        view.setScaleType(ImageView.ScaleType.MATRIX);
        float scale;

        // Handle touch events here...
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:  // first finger down only
                savedMatrix.set(matrix);
                start.set(event.getX(), event.getY());
                Log.d(TAG, "mode=DRAG"); // write to LogCat
                mode1 = DRAG1;
                break;

            case MotionEvent.ACTION_UP: // first finger lifted
                x_cordinate=event.getX();
                y_cordinate=event.getY();

                Log.e("height====",""+dragImage.getHeight());
                Log.e("width=====",""+dragImage.getWidth());

                Log.e("x_cordinate==",""+x_cordinate);
                Log.e("y_cordinate==",""+y_cordinate);

                break;

            case MotionEvent.ACTION_POINTER_UP: // second finger lifted
                mode1 = NONE1;
                Log.d(TAG, "mode1=NONE1");
                break;

            case MotionEvent.ACTION_MOVE:

                if (mode1 == DRAG1){ 
                    matrix.set(savedMatrix);
                    matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); // create the transformation in the matrix  of points
                } 
                else if (mode1 == ZOOM1){ 
                    // pinch zooming
                    float newDist = spacing(event);
                    Log.d(TAG, "newDist=" + newDist);
                    if (newDist > 5f){
                        matrix.set(savedMatrix);
                        scale = newDist / oldDist; // setting the scaling of the
                        matrix.postScale(scale, scale, mid.x, mid.y);
                    }

                }
                break;

            case MotionEvent.ACTION_POINTER_DOWN:
                oldDist = spacing(event);
                Log.d(TAG, "oldDist=" + oldDist);
                if (oldDist > 10f) {
                    savedMatrix.set(matrix);
                    midPoint(mid, event);
                    mode1 = ZOOM1;
                    Log.d(TAG, "mode=ZOOM" );
                }               
                break;

        }

        view.setImageMatrix(matrix); // display the transformation on screen
        return true; // indicate event was handled

    }

这是xml文件..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="get" 
        android:id="@+id/get"/>
    <RelativeLayout 
        android:id="@+id/image"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/get"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">
        <ImageView
            android:scaleType="matrix"
            android:visibility="gone"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:id="@+id/imagemarker"/>
    </RelativeLayout>
</RelativeLayout>

在这个开关案例MotionEvent.ACTION_UP中,我得到了图像视图的高度和宽度。但是这些值在缩放图像后也是一样的。所以请建议我怎么办?谢谢所有人..

0 个答案:

没有答案
相关问题