Android 4 가지 추가 애니메이션 기본 사용.

3157 단어 Android자바
package com.example.thinkpad.animation;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
    ImageView imageView;
    TranslateAnimation translateAnimation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Toast.makeText(this,"hello",Toast.LENGTH_SHORT).show();

        imageView=(ImageView) findViewById(R.id.image);
        Button button0 =(Button) findViewById(R.id.bt_translation);
        Button button1=(Button) findViewById(R.id.bt_rotate);
        Button button2 =(Button) findViewById(R.id.bt_scale);
        Button button3 =(Button) findViewById(R.id.bt_alpha);

        button1.setOnClickListener(this);
        button0.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
    }
    public  void onClick(View v){
        switch (v.getId()){
            case R.id.bt_translation://  
                translateAnimation =new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,
                        Animation.RELATIVE_TO_PARENT,1.0f,Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,
                        1.0f);

                translateAnimation.setDuration(3000);
                translateAnimation.setRepeatCount(Animation.INFINITE);
                translateAnimation.setFillAfter(true);
                translateAnimation.setRepeatMode(Animation.REVERSE);

                imageView.startAnimation(translateAnimation);

            break;
            case R.id.bt_rotate://  
                RotateAnimation rotateAnimation=new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                rotateAnimation.setRepeatCount(Animation.INFINITE);
                rotateAnimation.setDuration(3000);
                imageView.startAnimation(rotateAnimation);
                break;
            case R.id.bt_scale://  
                ScaleAnimation scaleAnimation=new ScaleAnimation(Animation.RELATIVE_TO_SELF,0,1.5f,Animation.RELATIVE_TO_SELF,0,1.5f);
                scaleAnimation.setDuration(3000);
                scaleAnimation.setRepeatCount(3);
                imageView.startAnimation(scaleAnimation);
                break;
            case R.id.bt_alpha://   
                AlphaAnimation alphaAnimation=new AlphaAnimation(0,1);
                alphaAnimation.setDuration(3000);
                imageView.startAnimation(alphaAnimation);


        }
    }
}

메모: 애니메이션 류 의 start () 나 startNow () 가 아 닌 view 류 의 start Animation () 을 사용 하여 애니메이션 을 켜 야 합 니 다. 그렇지 않 으 면 애니메이션 을 제때에 재생 하지 못 할 수도 있 습 니 다.

좋은 웹페이지 즐겨찾기