Android使用ViewPager实现左右切换02(转)

来源:互联网 发布:网络写作集剧本编写 编辑:程序博客网 时间:2024/06/05 18:05

临时转载

public class Welcome extends Activity implements OnGestureListener {    private GestureDetector detector;    private ViewFlipper flipper;    private Button button;    ImageView []iamges=new ImageView[4];    int i = 0;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.layoutwelcome);        iamges[0]=(ImageView) findViewById(R.id.imageview1);        iamges[1]=(ImageView) findViewById(R.id.imageview2);        iamges[2]=(ImageView) findViewById(R.id.imageview3);        iamges[3]=(ImageView) findViewById(R.id.imageview4);        detector = new GestureDetector(this);        flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper1);        flipper.addView(addImageView(R.drawable.png1o));        flipper.addView(addImageView(R.drawable.png2o));        flipper.addView(addImageView(R.drawable.png3o));        flipper.addView(addView());    }    private View addImageView(int id) {        ImageView iv = new ImageView(this);        iv.setImageResource(id);        return iv;    }    @Override    public boolean onTouchEvent(MotionEvent event) {        // TODO Auto-generated method stub        return this.detector.onTouchEvent(event);     }    private View addView() {        LayoutInflater inflater = LayoutInflater.from(this);        View view = inflater.inflate(R.layout.layoutwelcome2, null);        button = (Button) view.findViewById(R.id.button);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // Intent intent = new                // Intent(getApplicationContext(),ActContent.class);                // startActivity(intent);                // finish();                Toast.makeText(Welcome.this, "Clicked", 2000).show();            }        });        return view;    }    @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) {        System.out.println("in------------>>>>>>>");        if (e1.getX() - e2.getX() > 120) {            if (i < 3) {                i++;                setImage(i);                this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,                        R.anim.animation_right_in));                this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,                        R.anim.animation_left_out));                this.flipper.showNext();            }            return true;        }         else if (e1.getX() - e2.getX() < -120) {            if (i > 0) {                i--;                setImage(i);                this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,                        R.anim.animation_left_in));                this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,                        R.anim.animation_right_out));                this.flipper.showPrevious();            }            return true;        }        return false;    }    void setImage(int i)    {        for(int j=0;j<4;j++)        {            if(j!=i)            iamges[j].setImageResource(R.drawable.xiao);            else                iamges[j].setImageResource(R.drawable.da);        }    }}

主要布局

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ViewFlipper android:id="@+id/ViewFlipper1"        android:layout_width="fill_parent"         android:layout_height="fill_parent">    </ViewFlipper>    <LinearLayout        android:orientation="horizontal"        android:layout_width="wrap_content"        android:layout_gravity="bottom|center_horizontal"        android:layout_height="wrap_content"        >        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/da"            android:id="@+id/imageview1"            />        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/xiao"            android:id="@+id/imageview2"            />        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/xiao"            android:id="@+id/imageview3"            />        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/xiao"            android:id="@+id/imageview4"            />        </LinearLayout></FrameLayout>

次要布局

<?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"    ><Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginBottom="45dp"              android:text="进入程序"        android:textColor="#3E3E3E"         android:layout_gravity="center_horizontal"        /></LinearLayout> 

由于工作原因,无法下载代码。因此贴出代码仅供自己学习。学习结束整理分析或者删除博文。

0 0