Android基于ImageSwitcher实现图片切换功能

 更新时间:2016年02月17日 14:28:03   投稿:lijiao  
这篇文章主要介绍了Android基于ImageSwitcher实现图片切换功能的相关资料,需要的朋友可以参考下

左右切换图片控件大家都用ViewPager, ViewFipper比较多吧,我之前也用ViewPager实现了,使用ViewPager实现左右循环滑动图片,有兴趣的可以去看下,今天介绍的是基于ImageSwitcher实现的左右切换图片,先上截图吧

好了,接下来来看代码吧,第一张图是一个GridView,点击item跳转到第二个界面,第一个界面可以忽略,主要是讲解ImageSwitcher的左右却换图片,先看布局文件

<?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" > 
  <ImageSwitcher 
    android:id="@+id/imageSwitcher1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
  </ImageSwitcher> 
   
  <RelativeLayout   
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:orientation="vertical" >   
    <LinearLayout   
      android:id="@+id/viewGroup"   
      android:layout_width="fill_parent"   
      android:layout_height="wrap_content"   
      android:layout_alignParentBottom="true"   
      android:layout_marginBottom="30dp"   
      android:gravity="center_horizontal"   
      android:orientation="horizontal" >   
    </LinearLayout>   
  </RelativeLayout> 
</FrameLayout> 

然后就是Activity代码啦,总体来说比较简单,代码我添加了注释

package com.example.photoalbum; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.view.ViewGroup; 
import android.view.animation.AnimationUtils; 
import android.widget.ImageSwitcher; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.RelativeLayout.LayoutParams; 
import android.widget.Toast; 
import android.widget.ViewSwitcher.ViewFactory; 
 
public class ShowPhotoActivity extends Activity implements ViewFactory, OnTouchListener{ 
  /** 
   * ImagaSwitcher 的引用 
   */ 
  private ImageSwitcher mImageSwitcher; 
  /** 
   * 图片id数组 
   */ 
  private int[] imgIds; 
  /** 
   * 当前选中的图片id序号 
   */ 
  private int currentPosition; 
  /** 
   * 按下点的X坐标 
   */ 
  private float downX; 
  /** 
   * 装载点点的容器 
   */ 
  private LinearLayout linearLayout; 
  /** 
   * 点点数组 
   */ 
  private ImageView[] tips; 
  
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.show_photo); 
     
    imgIds = new int[]{R.drawable.item01,R.drawable.item02,R.drawable.item03,R.drawable.item04, 
        R.drawable.item05, R.drawable.item06, R.drawable.item07, R.drawable.item08,R.drawable.item09, 
        R.drawable.item10, R.drawable.item11, R.drawable.item12}; 
    //实例化ImageSwitcher 
    mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1); 
    //设置Factory 
    mImageSwitcher.setFactory(this); 
    //设置OnTouchListener,我们通过Touch事件来切换图片 
    mImageSwitcher.setOnTouchListener(this); 
     
    linearLayout = (LinearLayout) findViewById(R.id.viewGroup); 
     
    tips = new ImageView[imgIds.length]; 
    for(int i=0; i<imgIds.length; i++){ 
      ImageView mImageView = new ImageView(this); 
      tips[i] = mImageView; 
      LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,   
          LayoutParams.WRAP_CONTENT));  
      layoutParams.rightMargin = 3; 
      layoutParams.leftMargin = 3; 
       
      mImageView.setBackgroundResource(R.drawable.page_indicator_unfocused); 
      linearLayout.addView(mImageView, layoutParams); 
    } 
     
    //这个我是从上一个界面传过来的,上一个界面是一个GridView 
    currentPosition = getIntent().getIntExtra("position", 0); 
    mImageSwitcher.setImageResource(imgIds[currentPosition]); 
     
    setImageBackground(currentPosition); 
     
  } 
   
   /** 
   * 设置选中的tip的背景 
   * @param selectItems 
   */  
  private void setImageBackground(int selectItems){  
    for(int i=0; i<tips.length; i++){  
      if(i == selectItems){  
        tips[i].setBackgroundResource(R.drawable.page_indicator_focused);  
      }else{  
        tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);  
      }  
    }  
  }  
 
  @Override 
  public View makeView() { 
    final ImageView i = new ImageView(this); 
    i.setBackgroundColor(0xff000000); 
    i.setScaleType(ImageView.ScaleType.CENTER_CROP); 
    i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    return i ; 
  } 
 
  @Override 
  public boolean onTouch(View v, MotionEvent event) { 
    switch (event.getAction()) { 
    case MotionEvent.ACTION_DOWN:{ 
      //手指按下的X坐标 
      downX = event.getX(); 
      break; 
    } 
    case MotionEvent.ACTION_UP:{ 
      float lastX = event.getX(); 
      //抬起的时候的X坐标大于按下的时候就显示上一张图片 
      if(lastX > downX){ 
        if(currentPosition > 0){ 
          //设置动画,这里的动画比较简单,不明白的去网上看看相关内容 
          mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.left_in)); 
          mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.right_out)); 
          currentPosition --; 
          mImageSwitcher.setImageResource(imgIds[currentPosition % imgIds.length]); 
          setImageBackground(currentPosition); 
        }else{ 
          Toast.makeText(getApplication(), "已经是第一张", Toast.LENGTH_SHORT).show(); 
        } 
      }  
       
      if(lastX < downX){ 
        if(currentPosition < imgIds.length - 1){ 
          mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.right_in)); 
          mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.lift_out)); 
          currentPosition ++ ; 
          mImageSwitcher.setImageResource(imgIds[currentPosition]); 
          setImageBackground(currentPosition); 
        }else{ 
          Toast.makeText(getApplication(), "到了最后一张", Toast.LENGTH_SHORT).show(); 
        } 
      } 
      } 
       
      break; 
    } 
 
    return true; 
  } 
 
} 

上面切换图片主要用到的就是动画了,用的是translate移动动画,这里我就不介绍了,接下来我吧动画代码贴出来,在res新建一个anim的目录,如下图

左边进入的动画,left_in.xml

<?xml version="1.0" encoding="UTF-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
  <translate  
    android:fromXDelta="-100%p" 
    android:toXDelta="0" 
    android:duration="500"/> 
</set> 

左边出去的动画,left_out.xml

<?xml version="1.0" encoding="UTF-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
  <translate  
    android:fromXDelta="0" 
    android:toXDelta="-100%p" 
    android:duration="500"/> 
</set> 

右边进入的动画,right_in.xml

<?xml version="1.0" encoding="UTF-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
  <translate  
    android:fromXDelta="100%p" 
    android:toXDelta="0" 
    android:duration="500"/> 
</set> 

右边出去的动画,right_out.xml

<?xml version="1.0" encoding="UTF-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
  <translate  
    android:fromXDelta="0" 
    android:toXDelta="100%p" 
    android:duration="500"/> 
</set> 

好了,介绍完了,代码写的不是很好,写的不好的地方希望大家谅解,小编一定更加努力。

相关文章

  • Kotlin协程flowOn与线程切换超详细示例介绍

    Kotlin协程flowOn与线程切换超详细示例介绍

    这篇文章主要介绍了Kotlin协程flowOn与线程切换,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-09-09
  • Flutter搞定宽高不统一布局开发的方法详解

    Flutter搞定宽高不统一布局开发的方法详解

    我们在开发移动端界面的时候,经常会遇到一组尺寸不一的组件需要作为同一组展示,所以本文就将利用Wrap组件搞定宽高不统一布局开发,需要的可以参考一下
    2023-06-06
  • Android中删除sdcard里文件的命令

    Android中删除sdcard里文件的命令

    这篇文章主要介绍了Android中删除sdcard里文件的命令,本文讲解了删除android模拟器或手机上的sd卡文件的方法,需要的朋友可以参考下
    2015-04-04
  • Android使用百度地图出现闪退及定位时显示蓝屏问题的解决方法

    Android使用百度地图出现闪退及定位时显示蓝屏问题的解决方法

    这篇文章主要介绍了Android使用百度地图出现闪退及定位时显示蓝屏问题的解决方法,需要的朋友可以参考下
    2018-01-01
  • Android中的build.gradle文件深入讲解

    Android中的build.gradle文件深入讲解

    这篇文章主要给大家介绍了关于Android中build.gradle文件的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-10-10
  • Android中RecyclerView拖拽、侧删功能的实现代码

    Android中RecyclerView拖拽、侧删功能的实现代码

    这篇文章主要介绍了Android中RecyclerView拖拽、侧删功能的实现代码,需要的朋友可以参考下
    2017-09-09
  • Android刮刮卡实现原理与代码讲解

    Android刮刮卡实现原理与代码讲解

    这篇文章主要为大家详细介绍了Android刮刮卡实现原理、实现原理步骤以及代码讲解,感兴趣的小伙伴们可以参考一下
    2016-04-04
  • Android仿百度福袋红包界面

    Android仿百度福袋红包界面

    过年了,红包来袭,红包是怎么来的大家知道吗?这篇文章主要介绍了Android仿百度福袋红包界面,感兴趣的小伙伴们可以参考一下
    2016-02-02
  • Android实现加载时提示“正在加载,请稍后”的方法

    Android实现加载时提示“正在加载,请稍后”的方法

    在现在的很多应用中,当在加载的时候,如果页面动态数据较多,会有很长一段时间的空白页面,如果加上这个页面正在加载的提示,使得应用更加人性化。这篇文章就给大家分享了在 Android实现加载时提示“正在加载,请稍后”的方法,有需要的朋友们可以参考借鉴。
    2016-10-10
  • Flutter图片加载与缓存机制的深入探究

    Flutter图片加载与缓存机制的深入探究

    应用开发中经常会碰到网络图片的加载,通常我们会对图片进行缓存,以便下次加载同一张图片时不用再重新下载,下面这篇文章主要给大家介绍了关于Flutter图片加载与缓存机制的相关资料,需要的朋友可以参考下
    2021-11-11

最新评论