Gallery平滑移动
看了些网上的方法弄了下平滑移动的效果,虽说最后是实现了,实现后发现也不是我想要的效果,对于我幸苦写过的代码先存放在这上面了
package com.layout;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.animation.Transformation;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Scroller;
public class GalleryFlow extends Gallery{
private Camera mCamera = new Camera();
private int mMaxRotationAngle = 60;
private int mMaxZoom = -300;
private int mCoveflowCenter;
//test
private Scroller mScroller;
private int mTouchSlop;
private int mMinimumVelocitx;
private int mMaximumVelocitx;
private VelocityTracker mVelocityTracker;
private float mLastMotionX;
private boolean mIsInEdge = false;
private Context context;
public GalleryFlow(Context context) {
super(context);
this.context = context;
this.setStaticTransformationsEnabled(true);
}
public GalleryFlow(Context context, AttributeSet attrs) {
super(context, attrs);
this.setStaticTransformationsEnabled(true);
this.context = context;
init(context);
}
public void init(Context context) {
mScroller = new Scroller(context);
setFocusable(true);
setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
setWillNotDraw(false);
final ViewConfiguration configuration = ViewConfiguration.get(context);
mTouchSlop = configuration.getScaledTouchSlop();
mMinimumVelocitx = configuration.getScaledMinimumFlingVelocity();
mMaximumVelocitx = configuration.getScaledMaximumFlingVelocity();
}
public void fling(int velocityX) {
if (getChildCount() > 0) {
mScroller.fling(getScrollX(), getScrollY(), velocityX, 0, 0, 20, 0,
0);
final boolean movingDown = velocityX > 0;
awakenScrollBars(mScroller.getDuration());
invalidate();
}
}
private void obtainVelocityTracker(MotionEvent event) {
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);
}
private void releaseVelocityTracker() {
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN
&& event.getEdgeFlags() != 0) {
return false;
}
obtainVelocityTracker(event);
final int action = event.getAction();
final float x = event.getX();
final float y = event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
mLastMotionX = x;
break;
case MotionEvent.ACTION_MOVE:
final int deltaX = (int) (mLastMotionX - x);
mLastMotionX = x;
if (deltaX < 0) {
if (getScrollX() > 0) {
// scrollBy(0,deltaX);
scrollBy(deltaX,0);
}
}else if(deltaX > 0) {
int childTotalWidth = 0;
int width = 0;
if(getChildAt(0) != null){
width = getChildAt(0).getWidth();
}
for (int i = 0; i < getChildCount(); i++) {
childTotalWidth += this.getChildAt(i).getWidth();
}
mIsInEdge = getScrollX() <= childTotalWidth - 20;//width
if (mIsInEdge) {
// scrollBy(0, deltaX);
scrollBy(deltaX, 0);
}
}
break;
case MotionEvent.ACTION_UP:
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000, mMaximumVelocitx);
int initialVelocity = (int) velocityTracker.getYVelocity();
if ((Math.abs(initialVelocity) > mMinimumVelocitx)
&& getChildCount() > 0) {
fling(-initialVelocity);
}
releaseVelocityTracker();
break;
}
return true;
}
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
int scrollX = getScrollX();
int scrollY = getScrollY();
int oldX = scrollX;
int oldY = scrollY;
int x = mScroller.getCurrX();
int y = mScroller.getCurrY();
scrollX = x ;
scrollX = scrollX + 10;
scrollY = y;
// scrollY = scrollY + 10;
scrollTo(scrollX, scrollY);
postInvalidate();
}
}
@Override
public int getChildCount() {
// TODO Auto-generated method stub
return super.getChildCount();
}
public int getMaxRotationAngle() {
return mMaxRotationAngle;
}
public void setMaxRotationAngle(int maxRotationAngle) {
mMaxRotationAngle = maxRotationAngle;
}
public int getMaxZoom() {
return mMaxZoom;
}
public void setMaxZoom(int maxZoom) {
mMaxZoom = maxZoom;
}
private int getCenterOfCoverflow() {
return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2
+ getPaddingLeft();
}
private static int getCenterOfView(View view) {
System.out.println("view left :"+view.getLeft());
System.out.println("view width :"+view.getWidth());
return view.getLeft() + view.getWidth() / 2;
}
@Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
/*/
final int childCenter = getCenterOfView(child);
final int childWidth = child.getWidth();
int rotationAngle = 0;
t.clear();
t.setTransformationType(Transformation.TYPE_MATRIX);
if (childCenter == mCoveflowCenter) {
transformImageBitmap((LinearLayout) child, t, 0);
} else {
rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
if (Math.abs(rotationAngle) > mMaxRotationAngle) {
rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle
: mMaxRotationAngle;
}
transformImageBitmap((LinearLayout) child, t, rotationAngle);
}
//*/
return true;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mCoveflowCenter = getCenterOfCoverflow();
super.onSizeChanged(w, h, oldw, oldh);
}
private void transformImageBitmap(LinearLayout child, Transformation t,
int rotationAngle) {
mCamera.save();
final Matrix imageMatrix = t.getMatrix();
final int imageHeight = child.getLayoutParams().height;
final int imageWidth = child.getLayoutParams().width;
final int rotation = Math.abs(rotationAngle);
mCamera.translate(0.0f, 0.0f, 100.0f);
if (rotation < mMaxRotationAngle) {
float zoomAmount = (float) (mMaxZoom + (rotation * 1));//1.5
mCamera.translate(0.0f, 0.0f, 0);
}
mCamera.rotateY(rotationAngle);
mCamera.getMatrix(imageMatrix);
imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
mCamera.restore();
}
}
Gallery平滑移动的更多相关文章
- S Gallery – 很有特色的响应式 jQuery 相册插件
S Gallery 是一款响应式的 jQuery 相册插件.使用了 HTML5 全屏 API 以及 CSS3 动画 和 CSS3 转换,所以只能在支持这些功能的浏览器中使用. 这款插件它有一个特色功能 ...
- 如何安全的将VMware vCenter Server使用的SQL Server Express数据库平滑升级到完整版
背景: 由于建设初期使用的vSphere vCenter for Windows版,其中安装自动化过程中会使用SQL Server Express的免费版数据库进行基础环境构建.而此时随着业务量的增加 ...
- 使用图片视频展示插件blueimp Gallery改造网站的视频图片展示
在很多情况下,我们网站可能会展示我们的产品图片.以及教程视频等内容,结合一个比较好的图片.视频展示插件,能够使得我们的站点更加方便使用,也更加酷炫,在Github上有很多相关的处理插件可以找来使用,有 ...
- Nginx在线服务状态下平滑升级或新增模块的详细操作
今天应开发的需求,需要在Nginx增加一个模块,并不能影响现有的业务,所以就必须要平滑升级Nginx,好了,不多说了 1:查看现有的nginx编译参数 /usr/local/nginx/sbin/ng ...
- yarn关于app max attempt深度解析,针对长服务appmaster平滑重启
在YARN上开发长服务,需要注意fault-tolerance,本篇文章对appmaster的平滑重启的一个参数做了解析,如何设置可以有助于达到appmaster平滑重启. 在yarn-site.xm ...
- 备忘-Android ViewPager 与Gallery滑动冲突解决方法
解决方法,重新定义gallery,禁止触发pager的触摸事件 1 public class UserGallery extends Gallery implements OnGestureListe ...
- 让你的网站免费支持 HTTPS 及 Nginx 平滑升级
为什么要使用 HTTPS ? 首先来说一下 HTTP 与 HTTPS 协议的区别吧,他们的根本区别就是 HTTPS 在 HTTP 协议的基础上加入了 SSL 层,在传输层对网络连接进行加密.简单点说在 ...
- [修正] 移动平台曲线不平滑的问题(如:TRectangle, TPath...等)
问题:从 XE4 以来,Firemonkey 曲线绘图在移动平台不平滑的问题一直令人诟病,提交到官方的 QC 也是族繁不及备载,官方似乎有意的避开这个问题,迟迟没有修正. 适用版本:XE4 ~ Ber ...
- jQuery实现页面内锚点平滑跳转
平时我们做导航滚动到内容都是通过锚点来做,刷的一下就直接跳到内容了,没有一丝的滚动效果,而且 url 链接最后会有“小尾巴”,就像#keleyi,今天我就介绍一款 jquery 做的滚动的特效,既可以 ...
随机推荐
- struts2笔记11-OGNL
1.OGNL Object-Graph Navigation Language,对象-图 导航语言,可以方便的操作struts2值栈对象 2.对象栈操作方法 (1)action普通属性的访问方法 &l ...
- ctype.h 字符分类与转换
函数及说明 1 int isalnum(int c)该函数检查传递的字符是否是字母数字. 2 int isalpha(int c)该函数是否传递的字符是字母. 3 int iscntrl(int ...
- C# 堆栈的数据结构 (二)
堆栈是一种常用的数据结构,并且是线性表操作的子集,即操作受限的线性表.因此需要用到Clist 线性表类 public class CStack { private Clist m_List;//创建链 ...
- 联想企业网盘:SaaS服务集群化持续交付实践
1 前言 当代信息技术飞速发展,软件和系统的代码规模都变得越来越大,而且组件众多,依赖繁复,每次新版本的发布都仿佛是乘坐一次无座的绿皮车长途夜行,疲惫不堪.软件交付是一个复杂的工程,涉及到软 ...
- Balls Rearrangement(HDU)
Problem Description Bob has N balls and A boxes. He numbers the balls from 0 to N-1, and numbers the ...
- 网易云课堂_程序设计入门-C语言_第二周:判断_2信号报告
2 信号报告(5分) 题目内容: 无线电台的RS制信号报告是由三两个部分组成的: R(Readability) 信号可辨度即清晰度. S(Strength) 信号强度即大小. 其中R位于报告第一 ...
- 不同版本的 IIS 中使用 ASP.NET MVC(C#)【转】
由微软 ASP.NET 团队|2008 年 8 月 19 日 推特 在本教程中,您将学习在不同版本的 Internet Information Services 中如何使用 ASP.NET MVC 和 ...
- C#核编之System.Environment类
在前面的例子中用来了Environment.GetCommandLineArgs()这个方法,这个方法就是获取用户的命令行输入,是Environment类的方法之一,该方法的返回值是string[] ...
- oracle中查找执行效率低下的SQL
v$sqltext:存储的是完整的SQL,SQL被分割 v$sqlarea:存储的SQL 和一些相关的信息,比如累计的执行次数,逻辑读,物理读等统计信息(统计) v$sql:内存共享SQL区域中已经解 ...
- MJExtension
MJExtension 长话短说下面我们通过一个列子来看下怎么使用 1. 先把框架拉进去你的项目 2. 首先我这次用到的json最外层是一个字典,根据数据的模型我们可以把这个归类为字典中有数组,数组中 ...