Skip to content

Instantly share code, notes, and snippets.

@arianimartins
Created April 29, 2014 22:45
Show Gist options
  • Save arianimartins/dd79aec53bed8fb0ad9b to your computer and use it in GitHub Desktop.
Save arianimartins/dd79aec53bed8fb0ad9b to your computer and use it in GitHub Desktop.
Animação do background
<RelativeLayout 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:background="@color/skyline"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/relativeFundo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="40dp" >
<ImageView
android:id="@+id/img_fundo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/fundo1" />
<ImageView
android:id="@+id/img_fundo2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/fundo1" />
<ImageView
android:id="@+id/img_meio"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/meio1" />
<ImageView
android:id="@+id/img_meio2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/meio1" />
<ImageView
android:id="@+id/img_frente"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/frente1" />
<ImageView
android:id="@+id/img_frente2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/frente1" />
</RelativeLayout>
</RelativeLayout>
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class Animacao {
public static Integer ANIM_INICIO = 0;
public static Integer ANIM_FIM = 1;
public static Integer FRENTE = 1;
public static Integer MEIO = 2;
public static Integer FUNDO = 3;
public static void animFundo(ImageView imagem, Integer animacao, Integer nivel, Integer width){
if (imagem == null){
return;
}
TranslateAnimation anim;
if (animacao.equals(ANIM_INICIO)){
anim = new TranslateAnimation(0.0f, -width, 0.0f, 0.0f);
} else if (animacao.equals(ANIM_FIM)){
anim = new TranslateAnimation(width, 0.0f, 0.0f, 0.0f);
} else {
return;
}
switch (nivel) {
case 1:
anim.setDuration(10000);
break;
case 2:
anim.setDuration(30000);
break;
case 3:
anim.setDuration(100000);
break;
default:
return;
}
anim.setRepeatCount(TranslateAnimation.INFINITE);
anim.setRepeatMode(1);
anim.setFillAfter(false);
anim.setFillBefore(false);
anim.setInterpolator(new LinearInterpolator());
imagem.startAnimation(anim);
}
}
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.widget.ImageView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
animacaoFundo();
}
private void animacaoFundo() {
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
Animacao.animFundo(findImg(R.id.img_frente), Animacao.ANIM_INICIO, Animacao.FRENTE, width);
Animacao.animFundo(findImg(R.id.img_frente2), Animacao.ANIM_FIM, Animacao.FRENTE, width);
Animacao.animFundo(findImg(R.id.img_meio), Animacao.ANIM_INICIO, Animacao.MEIO, width);
Animacao.animFundo(findImg(R.id.img_meio2), Animacao.ANIM_FIM, Animacao.MEIO, width);
Animacao.animFundo(findImg(R.id.img_fundo), Animacao.ANIM_INICIO, Animacao.FUNDO, width);
Animacao.animFundo(findImg(R.id.img_fundo2), Animacao.ANIM_FIM, Animacao.FUNDO, width);
}
private ImageView findImg(int id){
return (ImageView) findViewById(id);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment