1. 首页
  2. 移动开发
  3. Android
  4. Android组件ViewFlipper的使用

Android组件ViewFlipper的使用

上传者: 2018-12-21 18:32:58上传 RAR文件 455KB 热度 38次
使用ViewFlipper的手势功能完成的图像之间的切换、 package com.action; import android.app.Activity; import android.os.Bundle; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.view.GestureDetector.OnGestureListener; import android.view.View.OnTouchListener; import android.view.animation.AlphaAnimation; import android.view.animation.AnimationUtils; import android.widget.ImageView; import android.widget.ViewFlipper; public class MainFlingDemoActivity extends Activity implements OnGestureLi stener { /** Called when the activity is first created. */ private ViewFlipper vf; private GestureDetector detector; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView iv1=new ImageView(this); ImageView iv2=new ImageView(this); ImageView iv3=new ImageView(this); iv1.setImageResource(R.drawable.a1); iv2.setImageResource(R.drawable.a2); iv3.setImageResource(R.drawable.a3); detector = new GestureDetector(this); vf=(ViewFlipper) findViewById(R.id.viewFlipper1); AlphaAnimation an=new AlphaAnimation(0.1f,1.0f); an.setDuration(1000); vf.setAnimation(an); vf.addView(iv1, 0); vf.setAnimation(an); vf.addView(iv2, 1); vf.setAnimation(an); vf.addView(iv3, 2); vf.setAnimation(an); if(detector.isLongpressEnabled()){ vf.setFlipInterval(5000); vf.startFlipping(); } } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub return this.detector.onTouchEvent(event); } @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // TODO Auto-generated method stub System.out.println("==============="); //vf.stopFlipping(); if (e1.getX() - e2.getX() > 120) { this.vf.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); this.vf.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); this.vf.showNext(); return true; } else if (e1.getX() - e2.getX() < -120) { this.vf.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); this.vf.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); this.vf.showPrevious(); return true; } if (e1.getY() - e2.getY() > 120) { this.vf.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); this.vf.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); this.vf.showNext(); return true; } else if (e1.getY() - e2.getY() < -120) { this.vf.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); this.vf.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); this.vf.showPrevious(); return true; } return false; } @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub //vf.startFlipping(); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // 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; } }
用户评论