- Podemos crear un marco de foto digital que cada X segundos vayan cambiando las fotos.
- Podemos añadir nuestra propia publicidad y que vaya cambiando el anuncio cada X segundos.
- Y otros...
En estes post vamos a ver como añadir en nuestro proyecto un ViewFlipper, ya sea con contenido estático, es decir, en el layout definimos el número máximo de elementos que puede tener. O bien, con contenido dinámico, es decir, en nuestra Activity le vamos añadiendo vistas.
Antes de empezar, destacar, que para el uso de un ViewFlipper se recomienda un mínimo de 2 vistas (layout, imageview, textview, etc.). Así pues, aquí les dejo el código:
res/anim/push_in_left.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2007 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="700"
android:fromXDelta="100%p"
android:toXDelta="0" />
<alpha
android:duration="700"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
res/anim/push_out_left.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2007 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="700"
android:fromXDelta="0"
android:toXDelta="-100%p" />
<alpha
android:duration="700"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
activity_flipper.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_layout_highlighted"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dip"
android:text="Static Flipper with push animation" />
<ViewFlipper
android:id="@+id/flipper_static"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dip"
android:flipInterval="2000" >
<ImageView
android:id="@+id/imageview_highlighted_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="@drawable/android_one" />
<ImageView
android:id="@+id/imageview_highlighted_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:src="@drawable/android_two" />
</ViewFlipper>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dip"
android:text="Dynamic Flipper" />
<ViewFlipper
android:id="@+id/flipper_dynamic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dip"
android:flipInterval="4000" >
</ViewFlipper>
</LinearLayout>
FlipperActivity.java
package com.example.blogviewflipper;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ViewFlipper;
public class FlipperActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flipper);
// static flipper
ViewFlipper mStaticFlipper = ((ViewFlipper) findViewById(R.id.flipper_static));
mStaticFlipper.startFlipping();
mStaticFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_in_left));
mStaticFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_out_left));
// dynamic flipper
ViewFlipper mDynamicFlipper = ((ViewFlipper) findViewById(R.id.flipper_dynamic));
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.android_one);
image.setScaleType(ScaleType.FIT_CENTER);
mDynamicFlipper.addView(image);
image = new ImageView(this);
image.setImageResource(R.drawable.android_two);
image.setScaleType(ScaleType.FIT_CENTER);
mDynamicFlipper.addView(image);
image = new ImageView(this);
image.setImageResource(R.drawable.android_three);
image.setScaleType(ScaleType.FIT_CENTER);
mDynamicFlipper.addView(image);
mDynamicFlipper.startFlipping();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_flipper, menu);
return true;
}
}
A continuación les dejo una imagen, dónde se muestra el contenido:
It works!
Roger Sala,
No hay comentarios:
Publicar un comentario