Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Animacoes no Android Part 1 - View Animation
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="app.demo.blog.animacoes.MainActivity">
<Button
android:id="@+id/fade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fade"
android:onClick="onClick"
/>
<Button
android:id="@+id/scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scale"
android:onClick="onClick"
/>
<Button
android:id="@+id/rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate"
android:onClick="onClick"
/>
<Button
android:id="@+id/translatex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Translate X"
android:onClick="onClick"
/>
<Button
android:id="@+id/translatey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Translate Y"
android:onClick="onClick"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1"
android:toAlpha="0"
android:duration="500"
>
</alpha>
package app.demo.blog.animacoes;
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.AnimationUtils;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
switch(view.getId()){
case R.id.fade:{
runAnimationFromXml(view,R.anim.fade_animation);
break;
}
case R.id.scale:{
runAnimationFromXml(view,R.anim.scale_animation);
break;
}
case R.id.rotate:{
runAnimationFromXml(view,R.anim.rotate_animation);
break;
}
case R.id.translatex:{
runAnimationFromXml(view,R.anim.translatex_animation);
break;
}
case R.id.translatey:{
runAnimationFromXml(view,R.anim.translatey_animation);
break;
}
}
}
public void runAnimationFromXml(View view, int animResId){
Animation animation = AnimationUtils.loadAnimation(this,animResId);
view.startAnimation(animation);
}
}
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="700"
>
</rotate>
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0dp"
android:fromYScale="0dp"
android:toXScale="100dp"
android:toYScale="100dp"
android:duration="700"
>
</scale>
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:interpolator/accelerate_cubic"
android:fromXDelta="0"
android:toXDelta="400"
android:duration="500"
>
</translate>
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0"
android:toYDelta="100"
android:duration="700"
>
</translate>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.