Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

Android: แอนิเมชั่นขยายรูปภาพจากจุดกึ่งกลาง (Scale Animation)


ตัวอย่าง: แอนิเมชั่นขยายรูปภาพ (Zoom) จากจุดกึ่งกลาง

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio ไปที่ File → New Project แล้วกรอกรายละเอียดที่จำเป็น

ขั้นตอนที่ 2: จัดการ Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://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="คลิก"
        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>

ในโค้ดข้างต้น มี Button เพื่อเริ่มแอนิเมชั่นขยายรูปภาพ (Zoom) ให้ ImageView

ขั้นตอนที่ 3: เขียนโค้ด Java (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.widget.Button;
import android.widget.ImageView;

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 จากจุดกึ่งกลาง

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);

พารามิเตอร์: (fromX, toX, fromY, toY, pivotXType, pivotXValue, pivotYType, pivotYValue)

  • 0f, 1f, 0f, 1f: ขยายจาก 0% ถึง 100% ทั้งแกน X และ Y
  • RELATIVE_TO_SELF, 0.5f: จุดหมุนอยู่ที่กึ่งกลางของ View เอง (50%)

การทดสอบแอปพลิเคชัน

เชื่อมต่ออุปกรณ์ Android จริง ใน Android Studio คลิกไอคอน Run เลือกอุปกรณ์ แล้วตรวจสอบหน้าจอ

Android: แอนิเมชั่นขยายรูปภาพจากจุดกึ่งกลาง (Scale Animation)

กดปุ่ม: รูปภาพจะโชว์พร้อมเอฟเฟกต์ Zoom จากจุดกึ่งกลาง:

Android: แอนิเมชั่นขยายรูปภาพจากจุดกึ่งกลาง (Scale Animation)