Friday, August 17, 2012

Android ViewFlipper

Android ViewFlipper

-->
Simple ViewAnimator that will animate between two or more views that have been added to it. Only one child is shown at a time. If requested, can automatically flip between each child at a regular interval.

Below i wrote the code for through flipping you can see the next ITEM or Previous Item with Animation.
Activity Code :
-->
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ViewFlipper;
/**
*
* @author vijayakumar
*
*/
public class AndroidMADQAActivity extends Activity {
ViewFlipper flipper;
Button btn1_next ,btn_prev ,btn_playwithAnimation;
/**
* This method called when activity started
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Integer[] items = { R.drawable.a, R.drawable.e,R.drawable.d,R.drawable.c};
setContentView(R.layout.main);
btn1_next = (Button)findViewById(R.id.button2);
btn_prev = (Button)findViewById(R.id.button1);
btn_playwithAnimation = (Button)findViewById(R.id.button3);
flipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.left_in));
flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.left_out));
for (Integer item : items) {
TextView textView = new TextView(this);
textView.setTextSize(30);
textView.setBackgroundResource(item);
flipper.addView(textView, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT));
}
btn_playwithAnimation.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(flipper.isFlipping()){
flipper.stopFlipping();
}
flipper.setFlipInterval(1000);
flipper.startFlipping();
}
});
btn1_next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flipper.isFlipping()){
flipper.stopFlipping();
}
flipper.showNext();
}
});;
btn_prev.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flipper.isFlipping()){
flipper.stopFlipping();
}
flipper.showPrevious();
}
});;
}
}

Screen Shot  
 main.xml
 
-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<ViewFlipper
android:id="@+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="370dip" >
</ViewFlipper>
<LinearLayout
android:orientation="horizontal"
android:layout_gravity="bottom"
android:padding="4dip"
android:background="#A4C639"
android:gravity="bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Previous" />

<Button
android:id="@+id/button3"
android:layout_marginLeft="30dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Slide Show" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_marginLeft="30dip"
android:layout_height="wrap_content"
android:text=" Next " />
</LinearLayout>
</LinearLayout>

Animation XML

left_out.xml 
-->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="300"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" />
</set>

left_in.xml
-->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="300"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
</set>

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...