📜  在Android中实现放大或缩小

📅  最后修改于: 2021-05-10 14:50:17             🧑  作者: Mango

放大缩小动画分别用于在Android应用程序中放大和缩小视图的大小。开发人员通常使用这些类型的动画为应用程序提供动态特性。通过观看这些动画,用户还可以感觉到应用程序中正在发生的变化。
放大

比例标记的XML属性

通过使用scale标签在XML文件中定义了“放大”和“缩小”动画的特性。

XML attribute Description
android:duration Used to define the duration of the animation in millisecond
android:fromXScale Used to set initial size of the view in X-axis
android:fromYScale Used to set initial size of the view in Y-axis
android:pivotX To define the X coordinate of the point about which the object is being zoom in/out
android:pivotY To define the Y coordinate of the point about which the object is being zoom in/out
android:toXScale Used to set final size of the view in X-axis
android:toYScale Used to set final size of the view in Y-axis

如何在Android中添加放大/缩小动画

下面的示例演示了对图像文件实施“放大”和“缩小”动画所涉及的步骤。使用ImageView将图像文件添加到活动中。

步骤1:建立新专案

  1. 单击文件,然后单击新建=>新建项目。
  2. 选择语言作为Kotlin。
  3. 根据需要选择最小的SDK。

步骤2:修改activity_main.xml文件

以下是activity_main.xml文件的代码,用于在activity_main.xml中添加TextView,ImageView和两个Button。

activity_main.xml


  
    
  
    
  
    


zoom_in.xml


  
    
  


zoom_out.xml


  
    
  


MainActivity.kt
package com.example.zomminout
   
import android.os.Bundle
import android.view.animation.AnimationUtils
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
   
   
class MainActivity : AppCompatActivity() {
   
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
   
        // assigning id of the button
        // which zoom in the image
        val buttonZoomIn: Button = findViewById(R.id.zoomInButton)
   
        // assigning id of the button
        // which zoom out the image
        val buttonZoomOut: Button = findViewById(R.id.zoomOutButton)
   
        // assiging id of imageview on
        // which zoom in/out will be performed
        val image: ImageView = findViewById(R.id.imageView)
   
        // actions to be performed when
        // "Zoom In" button is clicked
        buttonZoomIn.setOnClickListener() {
   
            // loading the animation of
            // zoom_in.xml file into a variable
            val animZoomIn = AnimationUtils.loadAnimation(this,
                                                R.anim.zoom_in)
            // assigning that animation to
            // the image and start animation
            image.startAnimation(animZoomIn)
        }
   
        // actions to be performed when
        // "Zoom Out" button is clicked
        buttonZoomOut.setOnClickListener() {
   
            // loading the animation of
            // zoom_out.xml file into a variable
            val animZoomOut = AnimationUtils.loadAnimation(this,
                                                R.anim.zoom_out)
   
            // assigning that animation to
            // the image and start animation
            image.startAnimation(animZoomOut)
        }
    }
}


strings.xml

    ZoomInOut
    Zoom In/Out in Android
    Zoom Out
    Zoom In


步骤3:为图片的“放大”和“缩小”动画定义XML文件

通过右键单击res => New => Android Resource Directory在应用程序的res文件夹中创建一个新目录。选择资源类型作为动画目录名称也应该是动画。在此目录中,创建2个动画资源文件,即zoom_inzoom_out 。这两个文件是XML文件,其中包含动画的详细信息。以下是这两个文件的代码。

zoom_in.xml



  
    
  

zoom_out.xml



  
    
  

步骤4:修改MainActivity.kt文件

以下是MainActivity.kt文件的代码,可根据用户单击的按钮在ImageView小部件上加载和启动动画。

MainActivity.kt

package com.example.zomminout
   
import android.os.Bundle
import android.view.animation.AnimationUtils
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
   
   
class MainActivity : AppCompatActivity() {
   
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
   
        // assigning id of the button
        // which zoom in the image
        val buttonZoomIn: Button = findViewById(R.id.zoomInButton)
   
        // assigning id of the button
        // which zoom out the image
        val buttonZoomOut: Button = findViewById(R.id.zoomOutButton)
   
        // assiging id of imageview on
        // which zoom in/out will be performed
        val image: ImageView = findViewById(R.id.imageView)
   
        // actions to be performed when
        // "Zoom In" button is clicked
        buttonZoomIn.setOnClickListener() {
   
            // loading the animation of
            // zoom_in.xml file into a variable
            val animZoomIn = AnimationUtils.loadAnimation(this,
                                                R.anim.zoom_in)
            // assigning that animation to
            // the image and start animation
            image.startAnimation(animZoomIn)
        }
   
        // actions to be performed when
        // "Zoom Out" button is clicked
        buttonZoomOut.setOnClickListener() {
   
            // loading the animation of
            // zoom_out.xml file into a variable
            val animZoomOut = AnimationUtils.loadAnimation(this,
                                                R.anim.zoom_out)
   
            // assigning that animation to
            // the image and start animation
            image.startAnimation(animZoomOut)
        }
    }
}

步骤5:修改字符串.xml文件

此文件中列出了活动中使用的所有字符串。

字符串.xml


    ZoomInOut
    Zoom In/Out in Android
    Zoom Out
    Zoom In

输出:在模拟器上运行

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!