通过双击放大和缩小只能第一次工作..如何解决这个问题? [ANDROID]

时间:2016-10-17 04:19:00

标签: android zooming

我正在创建一个应用程序,我必须放大和动物园输出双击图像就像在普通的Android设备中缩放图像。在我的情况下,我的代码工作正常但只有一次。在第一次双击图像完全放大和第二次双击它完全缩小,但在第三时间双击没有任何事情发生。如何解决这个问题?任何帮助将不胜感激 这是我的代码

public class SecondActivity extends AppCompatActivity {
ImageView iv;
ScaleGestureDetector scaleGestureDetector;
private Matrix original_matrix = 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);
    Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
            getResources(), R.drawable.demoimg), size.x, size.y, true);
    iv.setImageBitmap(bmp);
    original_matrix = iv.getImageMatrix();
    iv.setOnTouchListener(new View.OnTouchListener() {
        private GestureDetector gestureDetector = new GestureDetector(SecondActivity.this, new GestureDetector.SimpleOnGestureListener() {
            boolean check_zoom_state = false;
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                if(check_zoom_state==false) {
                    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=true;
                }
                else
                {
                    //iv.setImageMatrix(original_matrix);
                     iv.setAdjustViewBounds(true);

                }
                   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);
            }

        });

        @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;
        }
    });

}

0 个答案:

没有答案