1. 程式人生 > >圖解 Android 動畫中 android:pivotX 和 android:pivotY 屬性的含義

圖解 Android 動畫中 android:pivotX 和 android:pivotY 屬性的含義

在 Android 動畫中,縮放動畫(scale標籤)和旋轉動畫(rotate標籤)均有 android:pivotXandroid:pivotY 這兩個屬性,其值可以有多種形式,並不是很好理解。本文就用一個小 Demo 來展示這兩個屬性的含義,相信看了之後會有很直觀的感受。

android:pivotX 表示縮放/旋轉起點 X 軸座標,可以是整數值、百分數(或者小數)、百分數p 三種樣式,比如 50、50% / 0.5、50%p。當屬性值為數值時,表示在當前 View 的左上角,即原點處加上 50px,作為起始點;如果是百分數,比如 50%,表示在當前控制元件的左上角加上自己寬度的 50% (即自身寬度中心)作為起始點;如果是 50%p(字母 p 是 parent 的意思),取值的基數是父控制元件,那麼 50%p 就是表示在當前的左上角加上父控制元件寬度的 50% 作為起始點 x 軸座標

總之就是:相對於物件的頂部邊緣的畫素(50);或相對於物體的頂部邊緣的百分比 (例如 “50%”);或相對於父容器的頂部邊緣的百分比 (例如 “50%p”).

android:pivotY 為縮放/旋轉起點 Y 軸座標,取值及其意義與android:pivotX類似。

下面以縮放動畫為例,在工程的 res 資料夾下新建 anim 資料夾,然後在該資料夾中新建scale_anim.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.0" android:toXScale="2.0" android:fromYScale="0.0" android:toYScale="2.0" android:pivotX="50" android:pivotY="50" android:duration="1500">
</scale>

MainActivity.java 如下:

public class MainActivity extends Activity {

    private
TextView mTvAnimation; private Button mBtnStartAnimation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mTvAnimation = findViewById(R.id.tv_animation); mBtnStartAnimation = findViewById(R.id.btn_start_animation); mBtnStartAnimation.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //通過loadAnimation從XML檔案中獲取動畫 Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale_anim); //利用startAnimation將動畫傳遞給指定控制元件 mTvAnimation.startAnimation(animation); } }); } }

MainActivity 的佈局檔案 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/btn_start_animation"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tv_animation"
        android:layout_marginBottom="40dp"
        android:text="開始動畫"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:id="@+id/tv_animation"
        android:layout_width="100dp"
        android:layout_height="200dp"
        android:text="Hello World!"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:background="@color/colorAccent"/>

</RelativeLayout>

執行 Demo 後效果如下:


這裡寫圖片描述

scale_anim.xml 中的屬性改為 百分數/小數 的形式,如下:

       android:pivotX="50%"
       android:pivotY="50%"

執行後效果如下:


這裡寫圖片描述

再將 scale_anim.xml 中的屬性改為 百分數p 的形式,如下:

       android:pivotX="50%p"
       android:pivotY="50%p"

其執行後效果如下:


這裡寫圖片描述

上面三種取值的大致起始點如下圖所示:


這裡寫圖片描述

特別的,當android:pivotX和android:pivotY 為百分數/小數時,若均為 0.0 時,起始點為 View 左上角;均為 0.5 時起始點是 View 中心點;均為 1.0 時起始點是 View 右下角。