Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách tạo animation rung/lắc (shake/wobble) cho View trong Android

Giới thiệu

Ví dụ này minh họa cách tạo animation rung/lắc (shake) cho View (ImageView, Button...) trong Android bằng file XML animation.

Bước 1: Tạo dự án mới

Android Studio → File → New Project.

Bước 2: Giao diện (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="Rung"
        android:layout_height="wrap_content"/>

    <ImageView
        android:id="@+id/imageView"
        android:src="@mipmap/ic_launcher_round"
        android:layout_width="match_parent"
        android:layout_height="300dp" />

</RelativeLayout>

Bước 3: Tạo file animation (res/anim/shake.xml)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
    <translate
        android:duration="150"
        android:fromXDelta="-10%"
        android:repeatCount="5"
        android:repeatMode="reverse"
        android:toXDelta="10%"/>
</set>
  • translate: Di chuyển View theo trục X/Y
  • fromXDelta="-10%": Bắt đầu từ -10% chiều rộng View (bên trái)
  • toXDelta="10%": Kết thúc ở +10% chiều rộng View (bên phải)
  • duration="150": Mỗi lần di chuyển mất 150ms
  • repeatCount="5": Lặp lại 5 lần
  • repeatMode="reverse": Đảo chiều mỗi lần lặp (trái→phải→trái...)

Bước 4: Áp dụng animation (src/MainActivity.java)

package com.example.andy.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
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);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Animation animShake = AnimationUtils.loadAnimation(MainActivity.this, R.anim.shake);
                view.startAnimation(animShake);
            }
        });
    }
}

Chạy ứng dụng

Chạy app → Nhấn nút "Rung" → ImageView rung lắc ngang 5 lần qua trái/phải.

Biến thể: Wobble (rung xoay nhẹ)

Tạo res/anim/wobble.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
    <rotate
        android:duration="100"
        android:fromDegrees="-5"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="5"
        android:repeatMode="reverse"
        android:toDegrees="5"/>
</set>