前几天,公司项目有一个功能要做成滑动图片的淡入淡出,要一边滑动一边改变,所以ViewFlipper左右滑动效果就不能了。网上找了很久,也找不到资料,所以自己写了一个,通过滑动改变imageView的透明度。当按下图片时,先记下imageView的位置,图片滑动时,位置发生变化,就可以算出移动的距离,从而可以算出alpha的值。当图片向左滑动时,设置imageView的Alpha即imageView.setAlpha(255-alpha),设置下一个nextView的Alpha即nextView.setAlpha(alpha);当图片向右滑动时,设置imageView的Alpha即imageView.setAlpha(255-alpha),设置上一个lastView的Alpha即lastView.setAlpha(alpha);效果如图所示

      

废话就不多说了,上代码哈

package com.gallery.gradient;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView; public class MyGallery extends Gallery { private ImageAdapter adapter;
private int position ;
private ImageView imageView;
private int[] viewLocation;
private ImageView nextView;
private ImageView lastView;
private int winWeight;
private Context context; public MyGallery(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
} public MyGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
} public MyGallery(Context context, int[] residList) {
super(context);
this.context = context;
adapter = new ImageAdapter(context, residList);
setAdapter(adapter);
} @Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
position = getSelectedItemPosition();
imageView = (ImageView) findViewWithTag(position);
viewLocation = new int[2];
imageView.getLocationInWindow(viewLocation);
WindowManager wm = (WindowManager) context.getApplicationContext().getSystemService(
Context.WINDOW_SERVICE);
winWeight = wm.getDefaultDisplay().getWidth();
break;
case MotionEvent.ACTION_MOVE: break;
case MotionEvent.ACTION_UP:
// int position = getSelectedItemPosition();
// ImageView imageView = (ImageView) findViewWithTag(position);
// if (imageView != null) {
// System.out.println("imageView");
// imageView.setAlpha(255);
// int[] location = new int[2];
// imageView.getLocationInWindow(location);
// }
break; default:
break;
} return super.onTouchEvent(event);
} // @Override
// public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
// float distanceY) {
// if (e1.getX() > e2.getX()) {
// lastView = (ImageView) findViewWithTag(position+1);
// }else {
// nextView = (ImageView) findViewWithTag(position-1);
// }
//
//
// return super.onScroll(e1, e2, distanceX, distanceY);
// } @Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
int[] location = new int[2];
imageView.getLocationInWindow(location); if (location[0] != 0) {
if (location[0] > viewLocation[0]) {
int alpha = location[0] - viewLocation[0];
alpha = alpha * 255 / winWeight;
imageView.setAlpha(255-alpha);
lastView = (ImageView) findViewWithTag(position-1);
if (lastView != null) {
lastView.setAlpha((alpha));
}
}else {
int alpha = viewLocation[0] - location[0];
alpha = alpha * 255 / winWeight;
imageView.setAlpha(255-alpha);
nextView = (ImageView) findViewWithTag(position+1);
if (nextView != null) {
nextView.setAlpha(alpha);
}
}
}
if (location[0] == 0) {
if (lastView != null) {
lastView.setAlpha((255));
}
if (nextView != null) {
nextView.setAlpha(255);
}
}
} @Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
int kEvent;
if(isScrollingLeft(e1, e2)){ //Check if scrolling left
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
}
else{ //Otherwise scrolling right
kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
}
onKeyDown(kEvent, null);
return true;
} private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
return e2.getX() > e1.getX();
} public class ImageAdapter extends BaseAdapter {
private Context mcontext;
private int[] residList; public ImageAdapter(Context context, int[] residList) {
this.residList = residList;
mcontext = context;
} @Override
public int getCount() {
return residList.length;
} @Override
public Object getItem(int position) {
return residList[position];
} @Override
public long getItemId(int position) {
return position;
} public int getResId(int position) {
return residList[position];
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView img;
if (convertView == null) {
img = new ImageView(mcontext);
img.setScaleType(ImageView.ScaleType.FIT_XY);
img.setLayoutParams(new Gallery.LayoutParams(-1, -1)); } else {
img = (ImageView) convertView;
}
img.setTag(position);
img.setImageResource(residList[position]);
return img;
} }
}

代码下载链接 http://pan.baidu.com/s/1hqxaYTu

gallery左右滑动时图片淡入淡出的更多相关文章

  1. [Android]异步加载图片,内存缓存,文件缓存,imageview显示图片时增加淡入淡出动画

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3574131.html  这个可以实现ImageView异步加载 ...

  2. [转]Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡出效果)

    http://blog.csdn.net/yanzi1225627/article/details/22439119 众所周知,想要让ImageView旋转的话,可以用setRotation()让其围 ...

  3. React-Native ListView加载图片淡入淡出效果的组件

    今天练习项目中需要给listview在加载图片时增加一个淡入淡出的效果,因此干脆就自己封装了一个组件: 'use strict' import React from 'react-native' va ...

  4. javascript 图片淡入淡出效果 实例源代码

    代码说明:把代码粘贴好之后,需要更改html代码中的图片路径,即可执行成功.后面还有对js代码的详细说明,希望大家好好消化,好好理解. html源代码: <head> <title& ...

  5. js实现多个图片淡入淡出,框架

    单个淡入淡出已经写过,可以看看上几遍的博文 <style> *{ margin:0; padding:0; } div{ height:100px; width:100px; backgr ...

  6. jQuery实现简单的图片淡入淡出效果

    整体思路: 1.实现页面布局,设置css样式 2.用jQuery获取需要用到的变量 3.用jQuery为两个按钮绑定事件 一.页面布局: <div class="d1"> ...

  7. Android RecyclerView 滑动时图片加载的优化

    RecyclerView 滑动时的优化处理 在滑动时停止加载图片,在滑动停止时开始加载图片,这里用了Glide.pause 和Glide.resume.这里为了避免重复设置增加开销,设置了一个标志变量 ...

  8. JQuery--基础动画、滑动动画、淡入淡出动画、自定义动画

    /** * [JQ基础动画] * show() 显示 * hide() 隐藏 * toggle() 切换 * 默认无动画,如果要产生动画 * 在括号内,添加毫秒数,可产生动画和控制动画的快慢 * * ...

  9. iOS-CALayer图片淡入淡出动画

    ]; } - (.f;          CABasicAnimation *boundsAnimation = [CABasicAnimation animationWithKeyPath:, ,  ...

随机推荐

  1. asp.net控件(1)Repeater

    1. 通过Repeater和数据源创建表格 <AlternatingItemTemplate>属性可以控制单元格交替显示不同的背景颜色 <table width=" sty ...

  2. IPSEC实现

    IPSEC介绍与实现 一.介绍 IPSec 协议不是一个单独的协议,它给出了应用于IP层上网络数据安全的一整套体系结构,包括网络认证协议 Authentication Header(AH).封装安全载 ...

  3. 【HDOJ】3957 Street Fighter

    一定要注意审题啊,题目说的是选出做少的英雄打败其余处在任何模式下的英雄.共有Sigma(num of model)个方案,每个方案有Sigma(num of model)+n个决策.挺不错的一道精确覆 ...

  4. FORM Save : ORA-01403 FRM-40735 ORA-06502

    症状: FORM开发后挂上服务器后,运行保存按键提示: ORA-01403: 未找到任何数据 ----------------------------------------------------- ...

  5. 3 Financial Services Social Media Success Storie

    As financial services firms step-up their use of social media, we’ve been looking for some early suc ...

  6. SQL Server 地理数据库中的系统表

    转自:http://resources.arcgis.com/zh-cn/help/main/10.1/index.html#/na/002q00000080000000/ 地理数据库的系统表可以强制 ...

  7. Codeforces 302D

    思路:最短路,map[i][j] = d*(|x[i]-x[j]| + |y[i]-y[j]|) - add[i] #include<iostream> #include<cstdi ...

  8. oracle 创建索引

    一.索引简介 1.索引相当于目录 2.索引是通过一组排序后的索引键来取代默认的全表扫描检索方式,从而提高检索效率. 3.索引的创建要适度,多了会影响增删改的效率,少了会影响查询的效率,索引最好创建在取 ...

  9. WCF入门(一)——基本知识

    构建一个WCF程序通常分为三个部分:服务类(Server).宿主(Host).客户程序(Client).有一个很重要的程序集(System.ServeiceModel)要引用,它包含WCF的核心功能. ...

  10. A Tour of Go Variables with initializers

    A var declaration can include initializers, one per variable. If an initializer is present, the type ...