前几天,公司项目有一个功能要做成滑动图片的淡入淡出,要一边滑动一边改变,所以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. crtmpserver系列之一:流媒体概述

    阅读目录 概述 流媒体系统的组成 媒体文件封装 传输协议 回到顶部 概述 所谓流媒体按照字面意思理解就是像流一样的媒体,看起来像是废话.流媒体现在司空见惯,所以一般人大概不会有疑问.事实上在流媒体还没 ...

  2. 【HDOJ】3275 Light

    这就是个简单线段树+延迟标记.因为对bool使用了~而不是!,wa了一下午找不到原因. /* 3275 */ #include <iostream> #include <sstrea ...

  3. bzoj3514

    好题+数据结构神题+感人肺腑pascal被卡系列,我下面的代码几乎写到最优可怎耐bzoj上pascal开的是O1,c++开的是O2,这怎么可能跑得过!!!还是说说方法吧,这是一道算贡献的好题,因为我们 ...

  4. 修改UISearchBar的Cancel按钮为中文等本地化问题

    正确方法: 1:创建本地化strings. InfoPlist.strings 2:对InfoPlist.strings添加本地化语言. 即可. 只要添加文件, 系统会根据语言来调整显示文字 常见的搜 ...

  5. c#自定义液晶数字控件

    c#自定义液晶数字控件   项目中经常与硬件打交道,LED显示屏上面的液晶数字很好看,QT中直接就有数码管这个控件,C#中自己来写一个,其实掌握了一种自定义控件的写法,其他的都是浮云,举一反三,最笨的 ...

  6. POJ 1847 Tram dij

    分析:d[i]表示到i点,最少的操作数 #include<cstdio> #include<cstring> #include<queue> #include< ...

  7. 【HTML】Intermediate4:Tables:rowspan and colspan

    1.</th> header cell As with td elements,these must be enclosed inside tr elements 2.</tr co ...

  8. 【HTML】Intermediate2:Text: AbbreviationsQuotations Code

    1.</abbr> attribute:title 2.Quotations blockquote :standalone often multi-line quotations-cite ...

  9. nand flash 和nor flash 区别

    NOR和NAND是现在市场上两种主要的非易失闪存技术.Intel于1988年首先开发出NOR flash技术,彻底改变了原先由EPROM和EEPROM一统天下的局面.紧接着,1989年,东芝公司发表了 ...

  10. linux进程的几种状态

    Linux是一个多用户,多任务的系统,可以同时运行多个用户的多个程序,就必然会产生很多的进程,而每个进程会有不同的状态. Linux进程状态:R (TASK_RUNNING),可执行状态. 只有在该状 ...