ViewFlipper 简单应用

AdvserView.java

package com.earen.viewflipper;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.util.AttributeSet;

import android.view.Gravity;

import android.view.MotionEvent;

import android.view.animation.Animation;

import android.view.animation.TranslateAnimation;

import android.widget.FrameLayout;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.ViewFlipper;

public class AdvserView extends FrameLayout {

   /** 当前索引值.默认第一张图片被选中 */  

  private int _CurrentIndex = 0;   

  /** 上下文对象 */  

  private Context _Context;   

  /**  点击屏幕的 X 左边值(触摸屏,告知绝对X屏幕位置接触点的中心区域。单位:像素。) */  

  private float _startX;   

  /**  设备的宽度 */  

  private int _DisplayWidth;   

  /**  从左到右进入的动画 */  

  private Animation _Left2RightInAnimation;   

  /**  从左到右出去动画 */  

  private Animation _Left2RightOutAnimation;   

  /**  从右到左进入动画 */  

  private Animation _Right2LeftInAnimation; 

  /**  从右到左出去动画 */  

  private Animation _Right2LeftOutAnimation;   

  /**  动画持续时间 */  

  private long _duration = 1000; 

  /**  ViewFlipper 控件 图片的容器 */  

  private ViewFlipper _ViewFlipper;   

  /**  屏幕上方动态生成点的容器(用于表示图片总数,和提示当前所查看的图片位置) */  

  private LinearLayout _TopTipLinearLayout;   

  /**  默认点的 Bitmap 对象 */  

  private Bitmap _PointNormalBitmap;   

  /**  被选中的点的 Bitmap 对象 */  

  private Bitmap _PointSelectedBitmap;   

  /**  图片的资源  */  

  private int[] _imgResource = {R.drawable.image1,

                                       R.drawable.image2,                         

                   R.drawable.image3, 

                                                  R.drawable.image4,

                                                  R.drawable.image5,

                                                  R.drawable.image6};

  public AdvserView(Context context) {

     super(context);

     setupViews();

     }

  public AdvserView(Context context,AttributeSet attr) {

          super(context,attr);

          setupViews();

      }

   /** 装载 视图 控件*/  

  private void setupViews() {

     _Context = getContext();     // 获取上下文对象   

     _DisplayWidth = getResources().getDisplayMetrics().widthPixels;  // 获取设备屏幕宽度(单位:像素)  

       _ViewFlipper = new ViewFlipper(_Context);             // 创建 ViewFlipper 控件对象      

     _TopTipLinearLayout = new LinearLayout(_Context);  // 创建 LinearLayout 线性布局控件对象   

     _TopTipLinearLayout.setGravity(Gravity.CENTER_HORIZONTAL);

            _PointNormalBitmap =BitmapFactory.decodeResource(getResources(), R.drawable.point_normal);  

            _PointSelectedBitmap =BitmapFactory.decodeResource(getResources(), R.drawable.point_selected);

            // 添加 图片指定的图片资源到 ViewFlipper 对象中   

    for (int i = 0; i < _imgResource.length; i++) {

        ImageView imageView = new ImageView(_Context);

        imageView.setImageResource(_imgResource[i]);    _ViewFlipper.addView(imageView);

     }

           // 为 _TopTipLinearLayout 对象添加 表示图片显示的点情况   

    for (int i = 0; i < _imgResource.length; i++) {

      ImageView imageView = new ImageView(_Context);

               if (i==0) { // 默认第一个为选中情况下的图片     

                  imageView.setImageBitmap(_PointSelectedBitmap);

               }else {

          imageView.setImageBitmap(_PointNormalBitmap);

       }

               _TopTipLinearLayout.addView(imageView);

           }

        addView(_ViewFlipper);

        addView(_TopTipLinearLayout);

           //初始化动画.

            _Left2RightInAnimation = new TranslateAnimation(-_DisplayWidth, 0, 0, 0);

            _Left2RightInAnimation.setDuration(_duration);

            _Left2RightOutAnimation = new TranslateAnimation(0, _DisplayWidth, 0, 0);

            _Left2RightOutAnimation.setDuration(_duration);

            _Right2LeftInAnimation = new TranslateAnimation(_DisplayWidth, 0, 0, 0);

            _Right2LeftInAnimation.setDuration(_duration);

            _Right2LeftOutAnimation = new TranslateAnimation(0, -_DisplayWidth, 0, 0);

            _Right2LeftOutAnimation.setDuration(_duration); 

   } 

  @Override

      public boolean onTouchEvent(MotionEvent event) {

          switch (event.getAction()) {

             case MotionEvent.ACTION_DOWN:    // 手指按下

                 _startX = event.getX(); 

                        break;

                 case MotionEvent.ACTION_UP:      // 手指弹起

                        /** 最后一个被选中的 图片视图控件 ,即当前看到的图片视图控件 */

                        ImageView lastSellectedImageView  = (ImageView)_TopTipLinearLayout.getChildAt(_CurrentIndex);

                        //判断左右滑动切换图片. 

                        if(event.getX() > _startX){  //  从左向右滑动

                              _ViewFlipper.setInAnimation(_Left2RightInAnimation);

                              _ViewFlipper.setOutAnimation(_Left2RightOutAnimation);

                              _ViewFlipper.showNext();

                              _CurrentIndex--;

                              if(_CurrentIndex < 0){

                                     _CurrentIndex = _imgResource.length -1;

                               }

                        }else if(event.getX() < _startX){  //  从右向左滑动

                         _ViewFlipper.setInAnimation(_Right2LeftInAnimation);

                             _ViewFlipper.setOutAnimation(_Right2LeftOutAnimation);

                             _ViewFlipper.showPrevious(); 

                             _CurrentIndex++;

                             if(_CurrentIndex > _imgResource.length -1){

                                   _CurrentIndex = 0;

                              }

                        }

                        // 设置 标识 图片位置的  点 图标

                        ImageView imageview = (ImageView)_TopTipLinearLayout.getChildAt(_CurrentIndex);

                        imageview.setImageBitmap(_PointSelectedBitmap); 

                        lastSellectedImageView.setImageBitmap(_PointNormalBitmap);

                        break;

                 default:

                        break;

             }

             return true;

      } 

}

 

MainActivity.java

package com.earen.viewflipper;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.Window;

public class MainActivity extends Activity {

   @Override  

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

       //setContentView(R.layout.activity_main);

           // 不显示标题栏(须在 setContentView之前使用方能生效)

           requestWindowFeature(Window.FEATURE_NO_TITLE);

           setContentView(new AdvserView(this));  

  }

    @Override  

  public boolean onCreateOptionsMenu(Menu menu) {

     // Inflate the menu; this adds items to the action bar if it is present.

        //getMenuInflater().inflate(R.menu.main, menu);

            return true;

      }

}

最终效果:

posted @ 2014-05-04 18:00  Earen  阅读(115)  评论(0编辑  收藏  举报