Home > Mobile >  How to use animations in Android؟
How to use animations in Android؟

Time:07-04

I want to use button animations in my selection but I don't know how .I want to use animation in android studio is there any way?? I searched a lot in YouTube, but I couldn't find a way. Is there a special library or a special site?

CodePudding user response:

You can read about it here.
Just make an animation-list. Take it in backgraound your button.
And use it

(view.findViewById<Button>(R.id.btn_save).background as AnimationDrawable).start()

CodePudding user response:

Сreate a directory "anim" in "res" and put animations in it

There is a lot of information about this on the Internet, you searched badly and yet I will give you the code:

//variable:

Animation scaleUp, scaleDown;

scaleUp = AnimationUtils.loadAnimation(this,R.anim.scale_up);
scaleDown = AnimationUtils.loadAnimation(this,R.anim.scale_down);



Button Start = (Button)findViewById(R.id.buttonStart);
    Start.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
                        Start.startAnimation(scaleUp);
            }else if (motionEvent.getAction()==MotionEvent.ACTION_UP){
                        Start.startAnimation(scaleDown);
                       //write button action here
            }
            return true;
        }
});

Animation code "scaleUp"

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<scale
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromXScale="1"
  android:fromYScale="1"
  android:toXScale="1.2"
  android:pivotX="50%"
  android:pivotY="50%"
  android:toYScale="1.2"
  android:duration="100">
  </scale>
</set>

Animation code "scaleDown"

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="1.2"
    android:fromYScale="1.2"
    android:toXScale="1"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toYScale="1"
    android:duration="100">
</scale>
</set>
  • Related