open coding

open coding. 안드로이드 스튜디오 animation추가/ android studio cardview sake animation

◀ ▷ ▶ ♤ ♠ ♡ ♥ ♧ ♣ ⊙e 2020. 12. 1. 02:44
반응형

<shake_animation.xml>


<?xml version="1.0" encoding="utf-8"?>
<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration = "500"
    android:fromDegrees = "-10"
    android:pivotX = "30%"
    android:pivotY="30%"
    android:repeatCount = "1"
    android:repeatMode = "reverse"
    android:toDegrees="10"
    />

<!--
duration: 지속기간
-->

<MainActivity>

package com.example.animation;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

public class MainActivity extends AppCompatActivity {



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

    }

    //버튼클릭시 animation 실행
    public void btnMethod(View view){

        shakeAnimation();

    }


 //shakeAnimation method
    private void shakeAnimation(){
        Animation shake = AnimationUtils.loadAnimation(MainActivity.this,R.anim.shake_animation);
        final CardView cardView = findViewById(R.id.cardView);
        cardView.startAnimation(shake);

        //animation listener
        shake.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                cardView.setCardBackgroundColor(Color.RED); // 애니메이션 동작시 칼라변경
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                cardView.setCardBackgroundColor(Color.BLUE);

                                                    //애니메이션 동작 끝날때 칼라 설정 ㄱㄱ
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });//listener finish

    }//

}