在Android中,“缩放控制”是一个类,具有一些用于控制缩放功能的方法集。它具有两个用于控制缩放功能的按钮(即“放大”和“缩小”)。 API版本29中不推荐使用Zoom Control类。通过自定义视图和自定义布局以及专用缩放控件小部件,可以更好地处理ZoomControls类提供的功能。
缩放控件的重要方法
- 假设ZoomControls是ZoomControl类的引用,该类被用来调用ZoomControls类的不同方法。
ZoomControl zoomControls = (ZoomControls) findViewById(R.id.simpleZoomControl);
- show():此方法用于在App UI上显示缩放控件。
// will show the zoom controls
zoomControls.show()
- hide():此方法用于在App UI上隐藏缩放控件。
// will hide the zoom controls
zoomControls.hide()
- setOnZoomInClickListener(OnClickListenerlistener):按下“放大”按钮时,将调用此方法。它用于自定义在按下放大按钮时将显示的UI。
zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// add the code which will be executed when
// the zoom in button has been pressed
}
});
- setOnZoomOutClickListener(OnClickListenerlistener):按下“缩小”时将调用此方法。它的工作方式也与setOnZoomInClickListener()方法的工作方式相同,但是它使UI最小化(即收敛)。
zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// add the code which will be executed when
// the zoom out button has been pressed
}
});
- setIsZoomInEnabled(boolean isEnabled):这是缩放控件的方法之一,用于启用或禁用放大功能。
// it will enable the zoomIn button
zoomControls.setIsZoomInEnabled(true)
// it will disable the zoomIn button
zoomControls.setIsZoomInEnabled(false)
- setIsZoomOutEnabled(boolean isEnabled):这也是缩放控件的方法之一,用于启用或禁用缩小功能。
// it will enable the zoomOut button
zoomControls.setIsZoomOutEnabled(true)
// it will disable the zoomOut button
zoomControls.setIsZoomOutEnabled(false)
- setZoomSpeed(long speed):用于设置使用缩放控件完成的缩放的缩放速度
缩放控件的重要属性
- id:此属性用于赋予缩放控件唯一的标识。
- 背景:用于将背景颜色赋予缩放控件。
android:id=”@+id/zoom_controls android:background=”#fffff”/>
- padding:用于在缩放控件的侧面进行填充。
android:id=”@+id/zoom_controls android:padding=”20dp”/>
例子
下面的样本GIF给出得到什么我们将在本文中做的想法。请注意,我们将同时使用Java和Kotlin语言来实现此项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Kotlin作为编程语言。
步骤2:使用activity_main.xml文件
现在转到代表应用程序UI的activity_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.kt和MainActivity的代码。 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();
}
}
}
);
}
}