반응형

 

 

Android View Propery Animation(속성 애니메이션)

  • Fade in, Fade out
  • Scale up, Scale down
  • Move x, Move y
  • Rotation 90, Rotation 180
  • Set x, Set y

 

MainActivity.java

package com.srctree.viewpropertyanimation;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    View view;
    Button btn_fade_in;
    Button btn_fade_out;
    Button btn_scale_up;
    Button btn_scale_down;
    Button btn_move_x;
    Button btn_move_y;
    Button btn_rotation_90;
    Button btn_rotation_180;
    Button btn_set_x;
    Button btn_set_y;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        view = findViewById(R.id.view);
        btn_fade_in = findViewById(R.id.btn_fade_in);
        btn_fade_out = findViewById(R.id.btn_fade_out);
        btn_scale_up = findViewById(R.id.btn_scale_up);
        btn_scale_down = findViewById(R.id.btn_scale_down);
        btn_move_x = findViewById(R.id.btn_move_x);
        btn_move_y = findViewById(R.id.btn_move_y);
        btn_rotation_90 = findViewById(R.id.btn_rotation_90);
        btn_rotation_180 = findViewById(R.id.btn_rotation_180);
        btn_set_x = findViewById(R.id.btn_set_x);
        btn_set_y = findViewById(R.id.btn_set_y);

        btn_fade_in.setOnClickListener(this);
        btn_fade_out.setOnClickListener(this);
        btn_scale_up.setOnClickListener(this);
        btn_scale_down.setOnClickListener(this);
        btn_move_x.setOnClickListener(this);
        btn_move_y.setOnClickListener(this);
        btn_rotation_90.setOnClickListener(this);
        btn_rotation_180.setOnClickListener(this);
        btn_set_x.setOnClickListener(this);
        btn_set_y.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v == btn_fade_in) {
            view.setVisibility(View.VISIBLE);
            view.setAlpha(0);
            view.animate()
                    .alpha(1f)
                    .setDuration(1000)
                    .setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            view.setVisibility(View.VISIBLE);
                        }
                    });
        } else if(v == btn_fade_out) {
            view.setVisibility(View.VISIBLE);
            view.setAlpha(1);
            view.animate()
                    .alpha(0f)
                    .setDuration(1000)
                    .setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            view.setVisibility(View.GONE);
                        }
                    });
        } else if(v == btn_scale_up) {
            final int width = view.getLayoutParams().width;
            final int height = view.getLayoutParams().height;
            Log.d("onlytree", String.format("[전]w:%d, h:%d", view.getLayoutParams().width, view.getLayoutParams().height));
            view.animate()
                    .scaleX(view.getScaleX()+1)
                    .scaleY(view.getScaleY()+1)
                    .setDuration(1000)
                    .setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            Log.d("onlytree", String.format("[후]w:%d, h:%d", view.getLayoutParams().width, view.getLayoutParams().height));
                        }
                    });
        } else if(v == btn_scale_down) {
            view.animate()
                    .scaleX(view.getScaleX()-1f)
                    .scaleY(view.getScaleY()-1f)
                    .setDuration(1000)
                    .setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            Log.d("onlytree", String.format("[후]w:%d, h:%d", view.getLayoutParams().width, view.getLayoutParams().height));
                        }
                    });
        } else if(v == btn_move_x) {
            view.animate().translationXBy(100f).setDuration(1000);
        } else if(v == btn_move_y) {
            view.animate().translationYBy(100f).setDuration(1000);
        } else if(v == btn_rotation_90) {
            view.animate().rotation(90).setDuration(1000);
        } else if(v == btn_rotation_180) {
            view.animate().rotation(180).setDuration(1000);
        } else if(v == btn_set_x) {
            view.animate().x(view.getX()+100).setDuration(1000);
        } else if(v == btn_set_y) {
            view.animate().y(view.getY()+100).setDuration(1000);
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
            <View
                android:id="@+id/view"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="#0000ff"
                android:layout_gravity="center"/>
        </FrameLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="#bbbbbb"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/btn_fade_in"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:layout_margin="2dp"
                    android:text="Fade In"/>
                <Button
                    android:id="@+id/btn_fade_out"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:layout_margin="2dp"
                    android:text="Fade Out"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/btn_scale_up"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:layout_margin="2dp"
                    android:text="Scale Up"/>
                <Button
                    android:id="@+id/btn_scale_down"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:layout_margin="2dp"
                    android:text="Scale Down"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/btn_move_x"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:layout_margin="2dp"
                    android:text="Move X"/>
                <Button
                    android:id="@+id/btn_move_y"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:layout_margin="2dp"
                    android:text="Move Y"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/btn_rotation_90"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:layout_margin="2dp"
                    android:text="Rotation 90"/>
                <Button
                    android:id="@+id/btn_rotation_180"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="Rotation 180"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/btn_set_x"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:layout_margin="2dp"
                    android:text="Set X"/>
                <Button
                    android:id="@+id/btn_set_y"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:layout_margin="2dp"
                    android:text="Set Y"/>
            </LinearLayout>


        </LinearLayout>
    </LinearLayout>

</androidx.drawerlayout.widget.DrawerLayout>

 

프로젝트 소스 다운↓

ViewPropertyAnimation.zip
0.51MB

반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기