自定義ViewFlipperTest

package hyz.com.viewflipper;

import android.app.Activity;
import android.os.Bundle;
public class ViewFlipperTestActivity extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);	
		setContentView(R.layout.main);
	}

}


 

package hyz.com.viewflipper;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector.OnGestureListener;
import android.view.animation.AnimationUtils;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ViewFlipper;
import android.view.View.OnTouchListener;

public class MyViewFlipper extends ViewFlipper implements OnGestureListener,OnTouchListener
{
    private GestureDetector mGestureDetector;
    //private LayoutInflater inflater;
    private final Context mContext;
    
	public MyViewFlipper(Context context, AttributeSet attrs) 
	{
		super(context, attrs);
		mContext = context;
//		addView(R.layout.silent);
//		addView(R.layout.on);
		
		mGestureDetector = new GestureDetector(this);
    	setOnTouchListener(this);
    	setLongClickable(true); 
	}	
	
   public MyViewFlipper(Context context) {
		super(context);
		mContext = context;	}

//    private View addView(int layout) 
//    {
//    	inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//    	View view = inflater.inflate(layout, null);   
//    	return view;  
//    }
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return mGestureDetector.onTouchEvent(event);
	}
	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 
	{
		setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_right_in));
        if (e1.getX() - e2.getX() > 10 || e1.getX() - e2.getX() < -10) 
        {           
            showNext();            
       //   if(R.id.silent == flipper.getCurrentView().getId())        
            return true;
        }        
        return false;
	}   
} 


 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">	
	<alpha android:fromAlpha="1.0" android:toAlpha="1.0"/>
</set>


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/ll">
   <hyz.com.viewflipper.MyViewFlipper
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
       <include 
           layout="@layout/on"/>
       <include 
           layout="@layout/silent"/>
    </hyz.com.viewflipper.MyViewFlipper>
</LinearLayout>
on.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/on"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/btn_sound_silent"
        android:layout_alignParentRight="true"
        android:layout_marginTop="10dip"
        />
    <TextView 
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"       
        android:layout_marginLeft="267dip"
        android:layout_marginTop="12dip"
        android:text="on"
        android:textColor="#4c4c4c"
        android:textSize="16px"
        />
</RelativeLayout>
silent.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/silent">

    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/btn_sound_on"
        android:layout_marginTop="10dip"
        android:layout_alignParentRight="true"
        />
    <TextView 
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
        android:text="silent"
        android:layout_marginLeft="283dip"
        android:layout_marginTop="12dip"
        android:textColor="#bfbfbf"
        android:textSize="16px"/>
</RelativeLayout>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章