📜  Android 中的缩放滚动视图

📅  最后修改于: 2022-05-13 01:54:31.062000             🧑  作者: Mango

Android 中的缩放滚动视图

在本文中,我们将在 ScrollView 上实现缩放。大多数情况下,当我们创建滚动视图时,它包含大量数据,如果我们想在缩放时查看任何内容,那么我们可以实现此功能。当我们在应用程序中滚动并且包含使用 RecyclerView 的数据时,此功能非常有用。在那里我们可以实现这个功能,通过放大查看 RecyclerView 中的数据。下面给出了一个示例 GIF,以了解我们将在本文中做什么。请注意,我们将使用Java语言来实现这个项目。

分步实施

第 1 步:创建一个新项目

要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择Java作为编程语言。

步骤 2:使用 activity_main.xml 文件



导航到app > res > layout > activity_main.xml并将以下代码添加到该文件中。下面是activity_main.xml文件的代码。

XML


  
    
  
        
          
    
  


Java
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.animation.ScaleAnimation;
import android.widget.ScrollView;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private float mScale = 1f;
    private ScaleGestureDetector mScaleGestureDetector;
    GestureDetector gestureDetector;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // initialising the values
        gestureDetector = new GestureDetector(this, new GestureListener());
        mScaleGestureDetector = new ScaleGestureDetector(this, new ScaleGestureDetector.SimpleOnScaleGestureListener() {
            @Override
            public boolean onScale(ScaleGestureDetector detector) {
                
                // firstly we will get the scale factor
                float scale = 1 - detector.getScaleFactor();
                float prevScale = mScale;
                mScale += scale;
                  
                // we can maximise our focus to 10f only
                if (mScale > 10f)
                    mScale = 10f;
  
                ScaleAnimation scaleAnimation = new ScaleAnimation(1f / prevScale, 1f / mScale, 1f / prevScale, 1f / mScale, detector.getFocusX(), detector.getFocusY());
                  
                // duration of animation will be 0.It will 
                // not change by self after that
                scaleAnimation.setDuration(0);
                scaleAnimation.setFillAfter(true);
                 
                // initialising the scrollview
                ScrollView layout = (ScrollView) findViewById(R.id.scrollView);
                  
                // we are setting it as animation
                layout.startAnimation(scaleAnimation);
                return true;
            }
        });
    }
  
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        super.dispatchTouchEvent(event);
          
        // special types of touch screen events such as pinch ,
        // double tap, scrolls , long presses and flinch,
        // onTouch event is called if found any of these
        mScaleGestureDetector.onTouchEvent(event);
        gestureDetector.onTouchEvent(event);
        return gestureDetector.onTouchEvent(event);
    }
  
    private class GestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
  
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            return true;
        }
    }
}


第 3 步:使用MainActivity。 Java文件

下面是实现这个功能的代码。

// implementation of this feature
mScaleGestureDetector = new ScaleGestureDetector(this, new ScaleGestureDetector.SimpleOnScaleGestureListener(){
            @Override
            public boolean onScale(ScaleGestureDetector detector) {
                // firstly we will get the scale factor by which we want to zoom
                float scale = 1 - detector.getScaleFactor();
                float prevScale = mScale;
                mScale += scale;
                
                // we can maximise our focus to 10f only
                if (mScale > 10f)
                    mScale = 10f;
              
                ScaleAnimation scaleAnimation = new ScaleAnimation(1f / prevScale, 1f / mScale, 1f / prevScale, 1f / mScale, detector.getFocusX(), detector.getFocusY());
                
                // duration of animation will be 0.It will not change by self after that  
                scaleAnimation.setDuration(0);
               
                scaleAnimation.setFillAfter(true);
              
                // initialising the scrollview
                ScrollView layout = (ScrollView) findViewById(R.id.scrollView);
                
                // we are setting it as animation
                layout.startAnimation(scaleAnimation);
                return true;
       }
  });

下面是MainActivity的完整代码。 Java文件。代码中添加了注释以更详细地理解代码。

Java

import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.animation.ScaleAnimation;
import android.widget.ScrollView;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private float mScale = 1f;
    private ScaleGestureDetector mScaleGestureDetector;
    GestureDetector gestureDetector;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // initialising the values
        gestureDetector = new GestureDetector(this, new GestureListener());
        mScaleGestureDetector = new ScaleGestureDetector(this, new ScaleGestureDetector.SimpleOnScaleGestureListener() {
            @Override
            public boolean onScale(ScaleGestureDetector detector) {
                
                // firstly we will get the scale factor
                float scale = 1 - detector.getScaleFactor();
                float prevScale = mScale;
                mScale += scale;
                  
                // we can maximise our focus to 10f only
                if (mScale > 10f)
                    mScale = 10f;
  
                ScaleAnimation scaleAnimation = new ScaleAnimation(1f / prevScale, 1f / mScale, 1f / prevScale, 1f / mScale, detector.getFocusX(), detector.getFocusY());
                  
                // duration of animation will be 0.It will 
                // not change by self after that
                scaleAnimation.setDuration(0);
                scaleAnimation.setFillAfter(true);
                 
                // initialising the scrollview
                ScrollView layout = (ScrollView) findViewById(R.id.scrollView);
                  
                // we are setting it as animation
                layout.startAnimation(scaleAnimation);
                return true;
            }
        });
    }
  
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        super.dispatchTouchEvent(event);
          
        // special types of touch screen events such as pinch ,
        // double tap, scrolls , long presses and flinch,
        // onTouch event is called if found any of these
        mScaleGestureDetector.onTouchEvent(event);
        gestureDetector.onTouchEvent(event);
        return gestureDetector.onTouchEvent(event);
    }
  
    private class GestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
  
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            return true;
        }
    }
}

输出: