Friday, July 1, 2016

Làm việc với đối tượng animation trên android 5.0

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    ImageView imgLoGo;
    // Nho: class TranslateAnimation (x1,x2,y1,y2) co method setDuration, setFillAter, setAnimationListener    // gan animation cho doi tuong: btn.setAnimation(animation)    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imgLoGo = (ImageView) findViewById(R.id.imageViewLoGo);

        // Tao doi tuong TranslateAnimation        TranslateAnimation animation = new TranslateAnimation(0,200,0,500);
        // Set cac gia tri cho doi tuong TranslateAnimation        animation.setDuration(6000);
        animation.setFillAfter(true);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override            public void onAnimationStart(Animation animation) {
                Toast.makeText(MainActivity.this,"Begin!",Toast.LENGTH_SHORT).show();
            }

            @Override            public void onAnimationEnd(Animation animation) {
                //Toast.makeText(MainActivity.this,"Finish!",Toast.LENGTH_SHORT).show();            }

            @Override            public void onAnimationRepeat(Animation animation) {
                Toast.makeText(MainActivity.this,"Repeating!",Toast.LENGTH_SHORT).show();
            }
        });
        // Set Animation cho imgView        imgLoGo.setAnimation(animation);
    }

No comments:

Post a Comment