📜  Android中的ZoomControls与示例

📅  最后修改于: 2021-05-13 17:02:02             🧑  作者: Mango

在Android中,“缩放控制”是一个类,具有一些用于控制缩放功能的方法集。它具有两个用于控制缩放功能的按钮(即“放大”和“缩小”)。 API版本29中不推荐使用Zoom Control类。通过自定义视图和自定义布局以及专用缩放控件小部件,可以更好地处理ZoomControls类提供的功能。

Android中的ZoomControls

缩放控件的重要方法

  • 假设ZoomControls是ZoomControl类的引用,该类被用来调用ZoomControls类的不同方法。
  • show():此方法用于在App UI上显示缩放控件。
  • hide():此方法用于在App UI上隐藏缩放控件。
  • setOnZoomInClickListener(OnClickListenerlistener):按下“放大”按钮时,将调用此方法。它用于自定义在按下放大按钮时将显示的UI。
  • setOnZoomOutClickListener(OnClickListenerlistener):按下“缩小”时将调用此方法。它的工作方式也与setOnZoomInClickListener()方法的工作方式相同,但是它使UI最小化(即收敛)。
  • setIsZoomInEnabled(boolean isEnabled):这是缩放控件的方法之一,用于启用或禁用放大功能。
  • setIsZoomOutEnabled(boolean isEnabled):这也是缩放控件的方法之一,用于启用或禁用缩小功能。
  • setZoomSpeed(long speed):用于设置使用缩放控件完成的缩放的缩放速度

缩放控件的重要属性

  • id:此属性用于赋予缩放控件唯一的标识。
  • 背景:用于将背景颜色赋予缩放控件。
  • padding:用于在缩放控件的侧面进行填充。

例子

下面的样本GIF给出得到什么我们将在本文中做的想法。请注意,我们将同时使用JavaKotlin语言来实现此项目

Android中的ZoomControls

分步实施

步骤1:创建一个新项目

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

步骤2:使用activity_main.xml文件

现在转到代表应用程序UIactivity_main.xml文件。以下是activity_main.xml文件的代码在代码内部添加了注释,以更详细地了解代码。

XML


  
    
    
  
    
    
      


Kotlin
// Kotlin code to implement the zoom controls
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // onTouch listener function when the image is clicked
        image_View.setOnTouchListener { v, m -> // Perform tasks here
            zoom_controls.show()
            false
        }
  
        // This function will be automatically called out,when
        // zoom in button is being pressed
        zoom_controls.setOnZoomInClickListener(
                View.OnClickListener {
                    val x: Float = image_View.getScaleX()
                    val y: Float = image_View.getScaleY()
  
                    // setting the new scale
                    image_View.setScaleX((x + 0.5f) as Float)
                    image_View.setScaleY((y + 0.5f) as Float)
                    zoom_controls.hide()
                }
        )
  
        // This function will be called when
        // zoom out button is pressed
        zoom_controls.setOnZoomOutClickListener(
                View.OnClickListener {
                    val x: Float = image_View.getScaleX()
                    val y: Float = image_View.getScaleY()
                    if (x == 1f && y == 1f) {
                        image_View.setScaleX(x as Float)
                        image_View.setScaleY(y as Float)
                        zoom_controls.hide()
                    } else {
                        // setting the new scale
                        image_View.setScaleX((x - 0.5f) as Float)
                        image_View.setScaleY((y - 0.5f) as Float)
                        // hiding the zoom controls
                        zoom_controls.hide()
                    }
                }
        )
    }
}


Java
// Java code to implement the zoom controls
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ZoomControls;
  
public class MainActivity extends AppCompatActivity {
  
    ImageView imageView;
    ZoomControls zoomControls;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        imageView=findViewById(R.id.image_View);
        zoomControls=findViewById(R.id.zoom_controls);
        
        zoomControls.setBackgroundColor(Color.BLACK);
        zoomControls.show();
  
      // onTouch listener function  when the image is clicked  
      imageView.setOnTouchListener(
                new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        zoomControls.show();
                        return false;
                    }
                }
        );
  
        // This function will be automatically called out,when
        // zoom in button is being pressed
        zoomControls.setOnZoomInClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        float x=imageView.getScaleX();
                        float y=imageView.getScaleY();
                        
                        // setting the new scale
                        imageView.setScaleX((float)(x+0.5f));
                        imageView.setScaleY((float)(y+0.5f));
                        zoomControls.hide();
                    }
                }
        );
  
        // This function will be called when
        // zoom out button is pressed
        zoomControls.setOnZoomOutClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        float x=imageView.getScaleX();
                        float y=imageView.getScaleY();
                        if(x==1 && y==1)
                        {
                            // the scale will remain same,since
                            // it is maximum possible zoom out
                            imageView.setScaleX((float)(x));
                            imageView.setScaleY((float)(y));
                            zoomControls.hide();
                        }
                        else
                        {
                            // setting the new scale
                            imageView.setScaleX((float)(x-0.5f));
                            imageView.setScaleY((float)(y-0.5f));
                            // hiding the zoom controls
                            zoomControls.hide();
                        }
                    }
                }
        );
    }
}


步骤3:使用MainActivity文件

转到MainActivity文件,并参考以下代码。下面是MainActivity.ktMainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

科特林

// Kotlin code to implement the zoom controls
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // onTouch listener function when the image is clicked
        image_View.setOnTouchListener { v, m -> // Perform tasks here
            zoom_controls.show()
            false
        }
  
        // This function will be automatically called out,when
        // zoom in button is being pressed
        zoom_controls.setOnZoomInClickListener(
                View.OnClickListener {
                    val x: Float = image_View.getScaleX()
                    val y: Float = image_View.getScaleY()
  
                    // setting the new scale
                    image_View.setScaleX((x + 0.5f) as Float)
                    image_View.setScaleY((y + 0.5f) as Float)
                    zoom_controls.hide()
                }
        )
  
        // This function will be called when
        // zoom out button is pressed
        zoom_controls.setOnZoomOutClickListener(
                View.OnClickListener {
                    val x: Float = image_View.getScaleX()
                    val y: Float = image_View.getScaleY()
                    if (x == 1f && y == 1f) {
                        image_View.setScaleX(x as Float)
                        image_View.setScaleY(y as Float)
                        zoom_controls.hide()
                    } else {
                        // setting the new scale
                        image_View.setScaleX((x - 0.5f) as Float)
                        image_View.setScaleY((y - 0.5f) as Float)
                        // hiding the zoom controls
                        zoom_controls.hide()
                    }
                }
        )
    }
}

Java

// Java code to implement the zoom controls
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ZoomControls;
  
public class MainActivity extends AppCompatActivity {
  
    ImageView imageView;
    ZoomControls zoomControls;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        imageView=findViewById(R.id.image_View);
        zoomControls=findViewById(R.id.zoom_controls);
        
        zoomControls.setBackgroundColor(Color.BLACK);
        zoomControls.show();
  
      // onTouch listener function  when the image is clicked  
      imageView.setOnTouchListener(
                new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        zoomControls.show();
                        return false;
                    }
                }
        );
  
        // This function will be automatically called out,when
        // zoom in button is being pressed
        zoomControls.setOnZoomInClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        float x=imageView.getScaleX();
                        float y=imageView.getScaleY();
                        
                        // setting the new scale
                        imageView.setScaleX((float)(x+0.5f));
                        imageView.setScaleY((float)(y+0.5f));
                        zoomControls.hide();
                    }
                }
        );
  
        // This function will be called when
        // zoom out button is pressed
        zoomControls.setOnZoomOutClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        float x=imageView.getScaleX();
                        float y=imageView.getScaleY();
                        if(x==1 && y==1)
                        {
                            // the scale will remain same,since
                            // it is maximum possible zoom out
                            imageView.setScaleX((float)(x));
                            imageView.setScaleY((float)(y));
                            zoomControls.hide();
                        }
                        else
                        {
                            // setting the new scale
                            imageView.setScaleX((float)(x-0.5f));
                            imageView.setScaleY((float)(y-0.5f));
                            // hiding the zoom controls
                            zoomControls.hide();
                        }
                    }
                }
        );
    }
}

输出