package com.product.software.wireless_order_dishes_system; import android.app.ActionBar; import android.app.Activity; import android.content.Context; import android.media.Image; import android.os.Bundle; import android.util.Log; import android.view.GestureDetector; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import android.widget.ViewFlipper; /** * Created by software on 2016/5/4. */ public class loading_activity extends Activity implements GestureDetector.OnGestureListener,View.OnTouchListener{ private GestureDetector detector; //声明一个手势检测器 private ViewFlipper imageshow; private int imageresource[]=new int[]{R.drawable.lady,R.drawable.moon,R.drawable.ocean,R.drawable.lady}; private Context context; private ImageView imageView; private int imagesindex=1;// 图片的索引 public void loading_activity(Context contexts){ context=contexts; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.loading_layout); getview(); addFlipperView(); } /* * 获取控件设置控件属性*/ private void getview(){ imageshow=(ViewFlipper)findViewById(R.id.imageshow); imageshow.setOnTouchListener(this);//开启触摸监听 imageshow.setLongClickable(true);//开启长按事件 detector=new GestureDetector(this);//实例一个手势检测器 //imageshow.setAutoStart(true); } /* * new 一个向ViewFlipper里添加imageview*/ private void addFlipperView(){ imageView=new ImageView(this); imageView.setImageResource(imageresource[imagesindex]); imageView.setLayoutParams(new ViewGroup.LayoutParams(1000,2000)); imageshow.addView(imageView); } @Override /* * 使用手势检测器来代替ontouch*/ public boolean onTouch(View v, MotionEvent event) { return this.detector.onTouchEvent(event); } @Override public boolean onDown(MotionEvent e) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return false; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override public void onLongPress(MotionEvent e) { } /* * 这里实现滑动替换imageview里的图片和动画效果, 动画效果可用系统提供的效果和自定义动画效果,我这里使用的是系统提供的效果*/ @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { //displayedChildIndex if(imagesindex>=0&&imagesindex<=3) { if (e1.getX() - e2.getX() >= 100) { imagesindex+=1; if(imagesindex>3){imagesindex=3;} imageView.setImageResource(imageresource[imagesindex]); imageshow.setInAnimation(AnimationUtils.loadAnimation(this, android.support.v7.appcompat.R.anim.abc_fade_in)); imageshow.setOutAnimation(AnimationUtils.loadAnimation(this, android.support.v7.appcompat.R.anim.abc_fade_out)); } else if (e2.getX() - e1.getX() >= 100) { imagesindex-=1; if(imagesindex<0){imagesindex=0;} imageView.setImageResource(imageresource[imagesindex]); imageshow.setInAnimation(AnimationUtils.loadAnimation(this, android.support.v7.appcompat.R.anim.abc_fade_in)); imageshow.setOutAnimation(AnimationUtils.loadAnimation(this, android.support.v7.appcompat.R.anim.abc_fade_out)); } } return false; } }
Android开发中有关使用ViewFlipper实习手动拖拉替换图片效果
最新推荐文章于 2023-05-28 22:13:57 发布