Android 圖片縮放動畫相對於中心點?
本例演示了 Android 圖片縮放動畫相對於中心點。
步驟 1 − 在 Android Studio 中建立一個新專案,轉到 檔案 ⇒ 新建專案,並填寫所有必需的詳細資訊以建立新專案。
步驟 2 − 將以下程式碼新增到 res/layout/activity_main.xml。
<?xml version = "1.0" encoding = "utf-8"?> <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent"> <Button android:id = "@+id/button" android:layout_centerHorizontal = "true" android:layout_marginTop = "100dp" android:layout_width = "150dp" android:text = "Click" android:layout_height = "wrap_content"/> <ImageView android:id = "@+id/imageView" android:src = "@mipmap/ic_launcher_round" android:layout_width = "match_parent" android:layout_height = "match_parent" /> </RelativeLayout>
在上面的程式碼中,我們使用了按鈕來顯示圖片縮放動畫(縮放動畫)。
步驟 3 − 將以下程式碼新增到 src/MainActivity.java
package com.example.andy.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
ImageView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = findViewById(R.id.imageView);
view.setVisibility(View.INVISIBLE);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ScaleAnimation fade_in = new ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
fade_in.setDuration(1000);
fade_in.setFillAfter(true);
view.startAnimation(fade_in);
}
});
}
}當用戶點選按鈕時,它將使用以下程式碼顯示圖片縮放動畫:
ScaleAnimation fade_in = new ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); fade_in.setDuration(1000); fade_in.setFillAfter(true); view.startAnimation(fade_in);
讓我們嘗試執行您的應用程式。我假設您已將您的實際 Android 移動裝置連線到您的計算機。要在 Android Studio 中執行應用程式,請開啟您專案中的一個活動檔案,然後單擊執行
工具欄中的圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕:

在上面的螢幕中,我們使用了按鈕,當用戶點選按鈕時,它將顯示帶有縮放效果的影像,如下所示:

點選 此處 下載專案程式碼
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP