捏缩放不起作用[ANDROID]

时间:2016-10-17 07:43:24

标签: android imageview zoom pinchzoom

我正在创建一个应用程序,我在同一个ImageView上实现各种缩放功能。我已经编写了用于捏缩放的代码,它没有显示任何错误或异常,但它无法正常工作。我使用GestureDetector进行双击和滑动功能,并使用ScaleGestureDetector进行捏缩放 这是我的代码有问题吗?

public class SecondActivity extends AppCompatActivity {
ImageView iv;
private Matrix matrix = new Matrix();
private Matrix original_matrix = new Matrix();
private float x1,x2;
static final int MIN_DISTANCE = 150;
private Matrix matrix_scale=new Matrix();
int view_height;
int view_width;
int drawable_height;
int drawable_width;
Drawable drawable;
Bitmap bmp;
private float scale = 1f;
private ScaleGestureDetector SGD;
private  Matrix matrix_pinch=new Matrix();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
   setContentView(R.layout.activity_second);
    iv = (ImageView) findViewById(R.id.iv_demo_gestures);
    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
            getResources(), R.drawable.demoimg), size.x, size.y, true);
    iv.setImageBitmap(bmp);
    view_height=iv.getHeight();
    view_width=iv.getWidth();
    drawable=iv.getDrawable();
    drawable_height=drawable.getMinimumHeight();
    drawable_width=drawable.getMinimumWidth();
    SGD = new ScaleGestureDetector(this,new ScaleListener());
    iv.setOnTouchListener(new View.OnTouchListener() {
        private GestureDetector gestureDetector = new GestureDetector(SecondActivity.this, new GestureDetector.SimpleOnGestureListener() {
            int check_zoom_state = 0;
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                switch (check_zoom_state) {
                    case 0:
                        float scalefactor = Math.max(1.1f, Math.min(3.0f, 4.0f));
                        float x = e.getX();
                        float y = e.getY();
                        matrix.setScale(scalefactor, scalefactor, x, y);
                        iv.setImageMatrix(matrix);
                        check_zoom_state = 1;
                        break;

                    case 1:
                        //iv.setImageMatrix(original_matrix);
                       // iv.setAdjustViewBounds(true);
                        iv.setBackgroundResource(R.drawable.demoimg);
                        float scale_X = view_width / drawable_width;
                        float scale_Y = view_height / drawable_height;
                        original_matrix.setScale(scale_X, scale_Y);
                        iv.setImageMatrix(original_matrix);
                        check_zoom_state = 0;
                        break;
                    default:
                        float scalefactor1 = Math.max(1.1f, Math.min(3.0f, 4.0f));
                        float x1 = e.getX();
                        float y1 = e.getY();
                        matrix.setScale(scalefactor1, scalefactor1, x1, y1);
                        iv.setImageMatrix(matrix);
                        check_zoom_state = 1;

                }
                   return super.onDoubleTap(e);
            }

            @Override
            public void onLongPress(MotionEvent event) {
               //iv.setImageMatrix(original_matrix);
            /*    Toast.makeText(getApplicationContext(), "Long Pressed",
                        Toast.LENGTH_LONG).show();*/
                //iv.setAdjustViewBounds(true);
                iv.setBackgroundResource(R.drawable.demoimg);
                float scale_X = view_width / drawable_width;
                float scale_Y = view_height / drawable_height;
                original_matrix.setScale(scale_X, scale_Y);
                iv.setImageMatrix(original_matrix);
            }

        });

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    x1 = event.getX();
                    break;
                case MotionEvent.ACTION_UP:
                    x2 = event.getX();
                    float deltaX = x2 - x1;

                    if (Math.abs(deltaX) > MIN_DISTANCE)
                    {
                        // Left to Right swipe action
                        if (x2 > x1)
                        {
                           // Toast.makeText(getApplicationContext(), "Left to Right swipe [Next]", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(SecondActivity.this, MainActivity.class);
                            startActivity(intent);

                        }

                        // Right to left swipe action
                        /*else
                        {
                            Toast.makeText(getApplicationContext(), "Right to Left swipe [Previous]", Toast.LENGTH_SHORT).show();
                        }*/

                    }
                    else
                    {
                        // consider as something else - a screen tap for example
                    }
                    break;
            }
            gestureDetector.onTouchEvent(event);
            return true;
        }
    });

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        Intent intent = new Intent(SecondActivity.this, MainActivity.class);
        startActivity(intent);
    }

    return super.onOptionsItemSelected(item);
}

private float spacing(MotionEvent event) {
    float x = event.getX(0) - event.getX(1);
    float y = event.getY(0) - event.getY(1);

    return (float) Math.sqrt(x * x + y * y);
}
public boolean onTouchEvent(MotionEvent ev) {
    SGD.onTouchEvent(ev);
    return true;
}

private class ScaleListener extends ScaleGestureDetector.

        SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        scale *= detector.getScaleFactor();
        scale = Math.max(0.1f, Math.min(scale, 5.0f));

        matrix_pinch.setScale(scale, scale);
        iv.setImageMatrix(matrix_pinch);
        return true;
    }


}

}

1 个答案:

答案 0 :(得分:0)

试试这个例子 - Multitouch Image

声明全局内部活动

    //variable for counting two successive up-down events
int clickCount = 0;
//variable for storing the time of first click
long startTime;
//variable for calculating the total time
long duration;
//constant for defining the time duration between the click that can be considered as double-tap
static final int MAX_DURATION = 500;

对于Double Tap听力,您可以在OnTouch

中使用此代码
  switch(event.getAction() & MotionEvent.ACTION_MASK)
    {
        case MotionEvent.ACTION_DOWN:
            startTime = System.currentTimeMillis();
            clickCount++;
            break;
        case MotionEvent.ACTION_UP:
            long time = System.currentTimeMillis() - startTime;
            duration=  duration + time;
            if(clickCount == 2)
            {
                if(duration<= MAX_DURATION)
                {
                    Toast.makeText(SecondActivity.this, "double tap", Toast.LENGTH_LONG).show();
                }
                clickCount = 0;
                duration = 0;
                break;
            }
    }