'[안드로이드] 좌우로 넘기는 ViewFlipper'에 해당되는 글 1건

  1. 2011.09.15 [안드로이드] 좌우로 넘기는 ViewFlipper
  

View를 좌우로 밀었을 때의 애니메이션 효과를 미리 만들어줍니다.

actViewFlipper.class
package View.Flipper;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

public class actViewFlipper extends Activity {
private ViewFlipper m_viewFlipper;
private int m_nPreTouchPosX = 0;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        m_viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
        m_viewFlipper.setOnTouchListener(MyTouchListener);
    }
    
    private void MoveNextView()
    {
    m_viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.appear_from_right));
m_viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.disappear_to_left));
m_viewFlipper.showNext();
    }
    
    private void MovewPreviousView()
    {
    m_viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.appear_from_left));
m_viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.disappear_to_right));
    m_viewFlipper.showPrevious();
    }
    
    View.OnTouchListener MyTouchListener = new View.OnTouchListener()
    {
    public boolean onTouch(View v, MotionEvent event) 
    {
    if (event.getAction() == MotionEvent.ACTION_DOWN)
    {
    m_nPreTouchPosX = (int)event.getX();
    }
   
    if (event.getAction() == MotionEvent.ACTION_UP)
    {
    int nTouchPosX = (int)event.getX();
   
    if (nTouchPosX < m_nPreTouchPosX)
    {
    MoveNextView();
    }
    else if (nTouchPosX > m_nPreTouchPosX)
    {
    MovewPreviousView();
    }
   
    m_nPreTouchPosX = nTouchPosX;
    }
   
            return true;
        }
    };
}
 
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"
    >
    
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"/>
    
    <ViewFlipper
android198="@+id/viewFlipper"  
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent">

<TextView
   android198="@+id/view1"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_gravity="center"
   android:text="View 1"
   android:textSize="30px"/>

<Button
    android198="@+id/view2"
android:src="@drawable/icon"
android:text="View 2"
android:layout_width="wrap_content"
   android:layout_height="wrap_content"/>
<ImageView
android198="@+id/view3"
android:src="@drawable/icon"
android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_gravity="center"/>
   
</ViewFlipper>
</LinearLayout>

appear_from_left.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%p" 
android:duration="500"/>
<alpha 
android:fromAlpha="0.0" 
android:toAlpha="1.0" 
android:duration="500" />
</set>
 
appear_from_right.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%p" 
android:duration="500"/>
<alpha 
android:fromAlpha="0.0" 
android:toAlpha="1.0" 
android:duration="500" />
</set>
 
disappear_to_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate 
android:fromXDelta="0%p" 
android:toXDelta="-100%p" 
android:duration="500"/>
<alpha 
android:fromAlpha="1.0" 
android:toAlpha="0.0" 
android:duration="500" />
</set>

disappear_to_right.xml
 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate 
android:fromXDelta="0%p" 
android:toXDelta="100%p" 
android:duration="500"/>
<alpha 
android:fromAlpha="1.0" 
android:toAlpha="0.0" 
android:duration="500" />
</set>  
 
 
Posted by 예배하는 프로그래머