在一个活动中拖动并添加缩放/缩放到多个imageView

时间:2015-10-26 13:58:02

标签: android imageview zoom drag

构建像app这样的图像处理你可以用相机拍照,你可以添加剪贴画图像,胡须,眼镜等,我需要能够在视图周围拖动图像(relativeLayout)和那个工作正常,我无法实现的是缩放图像(其中3个),而不会闪烁代码。感谢你的帮助

gesture_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custome="http://schemas.android.com/apk/res/com.jimmy.test"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jimmy.test.GestureTestActivity" 
    android:id="@+id/root">

       <ImageView
        android:id="@+id/robo1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:src="@drawable/ic_launcher"
        android:background="#ff0000"
        android:scaleType="fitCenter"
        />


      <ImageView
          android:id="@+id/robo2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             android:src="@drawable/ic_launcher"
            android:background="#00ff00"
            android:scaleType="fitCenter"
        />


        <ImageView
            android:id="@+id/robo3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             android:src="@drawable/ic_launcher"
            android:background="#0000ff"
             android:scaleType="fitCenter"

        />
</RelativeLayout>

以及此处的活动代码

package com.jimmy.test;



import android.app.Activity;
 import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;



public class GestureTestActivity extends Activity implements OnTouchListener {

private int _xDelta;
private int _yDelta;
private RelativeLayout _root;

private ImageView robo1, robo2, robo3;

private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.0f;

private ImageView curView;

private int width1, height1;
private int mActivePointerId;
private float mLastTouchX;
private float mLastTouchY;


// These matrices will be used to move and zoom image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();

// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;


private static final String TAG = "Touch";

// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;


@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.gesture_layout);


    _root = (RelativeLayout)findViewById(R.id.root);

    robo1 = (ImageView) findViewById(R.id.robo1);
    robo2 = (ImageView) findViewById(R.id.robo2);
    robo3 = (ImageView) findViewById(R.id.robo3);

    robo1.setOnTouchListener(this);
    robo2.setOnTouchListener(this);
    robo3.setOnTouchListener(this);

     // Create our ScaleGestureDetector
    mScaleDetector = new ScaleGestureDetector(this, new ScaleListener());

}

@Override
public void onWindowFocusChanged(boolean hasFocus){
     width1 = robo1.getWidth();
    height1 = robo1.getHeight();
}

@Override
public boolean onTouch(View view_, MotionEvent event) {

    final int X = (int) event.getRawX();
    final int Y = (int) event.getRawY();

    curView = (ImageView) view_;


    mScaleDetector.onTouchEvent(event);

    switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:

            RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) curView.getLayoutParams();
            _xDelta = X - lParams.leftMargin;
            _yDelta = Y - lParams.topMargin;

            final float x = event.getX();
            final float y = event.getY();

            mLastTouchX = x;
            mLastTouchY = y;

            mActivePointerId = event.getPointerId(0);

            break;
        case MotionEvent.ACTION_UP:

             mActivePointerId = -1;
            break;

        case MotionEvent.ACTION_POINTER_DOWN:

            break;
        case MotionEvent.ACTION_POINTER_UP:

             final int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
             final int pointerId = event.getPointerId(pointerIndex);
             if (pointerId == mActivePointerId) {
                 // This was our active pointer going up. Choose a new
                 // active pointer and adjust accordingly.
                 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                 mLastTouchX = event.getX(newPointerIndex);
                 mLastTouchY = event.getY(newPointerIndex);
                 mActivePointerId = event.getPointerId(newPointerIndex);
             }

            break;
        case MotionEvent.ACTION_MOVE:


             if (!mScaleDetector.isInProgress()) {

                 Log.d("scaling state ", "<<<<<<<<<NOT SCALING");
                 RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) curView.getLayoutParams();
                layoutParams.leftMargin = X - _xDelta;
                layoutParams.topMargin = Y - _yDelta;
                layoutParams.rightMargin = -250;
                layoutParams.bottomMargin = -250;
               curView.setLayoutParams(layoutParams);

             //  _root.invalidate();
             }else{
                 //Log.d("scaling state ", ">>>>>>>>>>>>>> SCALING"); 
             }

            break;
    }


    return true;
}


private float spacing(MotionEvent event) {
   float x = event.getX(0) - event.getX(1);
   float y = event.getY(0) - event.getY(1);
   return FloatMath.sqrt(x * x + y * y);
}


private void midPoint(PointF point, MotionEvent event) {
       float x = event.getX(0) + event.getX(1);
       float y = event.getY(0) + event.getY(1);
       point.set(x / 2, y / 2);
    }

  private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            mScaleFactor *= detector.getScaleFactor();

            // Don't let the object get too small or too large.
            mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));



            curView.setScaleX(mScaleFactor);
            curView.setScaleY(mScaleFactor);



            _root.invalidate();
            return true;
        }


        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {

            curView.setScaleX(mScaleFactor);
            curView.setScaleY(mScaleFactor);



            _root.invalidate();
        super.onScaleEnd(detector);
        }
    }

}

0 个答案:

没有答案