Friday 24 November 2017

Object Animator

Android provides the Properties Animation API which allow to change object properties over a pre-defined time interval. It can be used for arbitrary object properties and on any object not only Android views




The superclass of the animation API is the Animator class. Typically the ObjectAnimator class is used to modify the attributes of an object.
You can also add an AnimatorListener class to your Animator class. This listener is called in the different phases of the animation. You can use this listener to perform actions before orafter a certain animation, e.g. add or remove a View from a ViewGroup.


 float scale = getResources().getDisplayMetrics().density;
        ObjectAnimator anim1 = ObjectAnimator.ofFloat(img,
                "x", transitionsContainer.getX(), 150.0f * scale);
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(img,
                "y", transitionsContainer.getY(), 150.0f * scale);
//                ObjectAnimator anim3 = ObjectAnimator.ofFloat(someImage,//                        "x", 220.0f*scale, 20.0f*scale);//                ObjectAnimator anim4 = ObjectAnimator.ofFloat(someImage,//                        "y", 220.0f*scale, 20.0f*scale);        AnimatorSet set = new AnimatorSet();
        set.play(anim2).after(500);
        // set.play(anim2).after(500);        set.setDuration(1000);
        set.setInterpolator(new LinearInterpolator());
        set.start();
        set.addListener(new Animator.AnimatorListener() {
            @Override            public void onAnimationStart(Animator animator) {

            }

            @Override            public void onAnimationEnd(Animator animator) {
                img.setVisibility(View.INVISIBLE);
                logo.setVisibility(View.VISIBLE);
                new Handler().postDelayed(new Runnable() {
                    @Override                    public void run() {
                        sharedPreferences= getSharedPreferences("pref",MODE_PRIVATE);
                        if(sharedPreferences.getBoolean("islogin",false)){
                            Intent in = new Intent(getApplicationContext(), Home_Activity.class);
                            Sample sample= new Sample( R.color.colorPrimary, "Shared Elements");
                            in.putExtra("sample", sample);
                            in.putExtra("type", 0);
                            transitionTo(in);
                            finish();
                        }else {
                            Intent in = new Intent(Splash.this, MainActivity.class);
                            // startActivity(in);                            Sample sample= new Sample( R.color.colorPrimary, "Shared Elements");
                            in.putExtra("sample", sample);
                            in.putExtra("type", 0);
                            transitionTo(in);
                            finish();
                        }
                    }
                },1000);

            }

            @Override            public void onAnimationCancel(Animator animator) {

            }

            @Override            public void onAnimationRepeat(Animator animator) {

            }
        });


Activity animations in Android with shared views

  sharedImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //This is where the magic happens.
                // makeSceneTransitionAnimation takes a context, view,
                // a name for the target view.
                ActivityOptions options =
                        ActivityOptions.
                        makeSceneTransitionAnimation(MainActivity.this, sharedImage, "sharedImage");
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent, options.toBundle());
            }
        });