public class ZoomListView extends ListView implements SwipeListener {

    public static enum Action {
LeftToRight,
RightToLeft,
TopToBottom,
BottomToTop,
None
} private List<SwipeListener> actionListeners = new ArrayList<SwipeListener>();
private boolean SwipeFlag = false; private static final int HORIZONTAL_MIN_DISTANCE = 30; // The minimum
private static final int VERTICAL_MIN_DISTANCE = 80; // The minimum distance
private float downX, downY, upX, upY; // Coordinates private static final int INVALID_POINTER_ID = -1;
private int mActivePointerId = INVALID_POINTER_ID;
private ScaleGestureDetector mScaleDetector; private float mScaleFactor = 1.f;
private float maxWidth = 0.0f;
private float maxHeight = 0.0f;
private float mLastTouchX;
private float mLastTouchY;
private float mPosX;
private float mPosY;
private float width;
private float height; public ZoomListView(Context context) {
super(context);
mScaleDetector = new ScaleGestureDetector(getContext(), new ScaleListener());
} public ZoomListView(Context context, AttributeSet attrs) {
super(context, attrs);
mScaleDetector = new ScaleGestureDetector(getContext(), new ScaleListener());
} public ZoomListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mScaleDetector = new ScaleGestureDetector(getContext(), new ScaleListener());
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
width = MeasureSpec.getSize(widthMeasureSpec);
height = MeasureSpec.getSize(heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
} @Override
public boolean onTouchEvent(@NonNull MotionEvent ev) {
super.onTouchEvent(ev);
final int action = ev.getAction();
mScaleDetector.onTouchEvent(ev);
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY(); mLastTouchX = x;
mLastTouchY = y; mActivePointerId = ev.getPointerId(0); downX = ev.getX();
downY = ev.getY(); SwipeFlag = true; break;
} case MotionEvent.ACTION_MOVE: {
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
final float x = ev.getX(pointerIndex);
final float y = ev.getY(pointerIndex);
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY; float leftMargin = Math.abs(mPosX) / mScaleFactor;
float rightMargin = this.getMeasuredWidth() - Math.abs(mPosX) / mScaleFactor;
rightMargin -= this.getResources().getDisplayMetrics().widthPixels / mScaleFactor ; mPosX += dx;
mPosY += dy; if (mPosX > 0.0f)
mPosX = 0.0f;
else if (mPosX < maxWidth)
mPosX = maxWidth; if (mPosY > 0.0f)
mPosY = 0.0f;
else if (mPosY < maxHeight)
mPosY = maxHeight; mLastTouchX = x;
mLastTouchY = y; invalidate(); upX = ev.getX();
upY = ev.getY(); float deltaX = downX - upX;
float deltaY = downY - upY;
// horizontal swipe detection
if (Math.abs(deltaX) > HORIZONTAL_MIN_DISTANCE) {
// left or right
if (deltaX < 0 && SwipeFlag && leftMargin < 10) {
SwipeFlag = false;
OnAction(Action.LeftToRight);
break;
}
if (deltaX > 0 && SwipeFlag && rightMargin < 10) {
SwipeFlag = false;
OnAction(Action.RightToLeft);
break;
}
} else
{
// vertical swipe detection
if (Math.abs(deltaY) > VERTICAL_MIN_DISTANCE) {
// top or down
if (deltaY < 0 && SwipeFlag) {
SwipeFlag = false;
OnAction(Action.TopToBottom);
break;
}
if (deltaY > 0 && SwipeFlag) {
SwipeFlag = false;
OnAction(Action.BottomToTop);
break;
}
}
} break;
} case MotionEvent.ACTION_UP: {
mActivePointerId = INVALID_POINTER_ID;
break;
} case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
} case MotionEvent.ACTION_POINTER_UP: {
final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = ev.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = ev.getX(newPointerIndex);
mLastTouchY = ev.getY(newPointerIndex);
mActivePointerId = ev.getPointerId(newPointerIndex);
}
break;
}
} return true;
} @Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor);
canvas.restore();
} @Override
protected void dispatchDraw(@NonNull Canvas canvas) {
canvas.save(Canvas.MATRIX_SAVE_FLAG);
if (mScaleFactor == 1.0f) {
mPosX = 0.0f;
mPosY = 0.0f;
}
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor);
super.dispatchDraw(canvas);
canvas.restore();
invalidate();
} private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
mScaleFactor = Math.max(1.0f, Math.min(mScaleFactor, 3.0f));
maxWidth = width - (width * mScaleFactor);
maxHeight = height - (height * mScaleFactor);
invalidate();
return true;
}
} @Override
public void OnAction(Action action) {
for(SwipeListener listener : actionListeners){
listener.OnAction(action);
}
} public void addActionListener(SwipeListener listener)
{
actionListeners.add(listener);
}
}
public interface SwipeListener extends EventListener {
public void OnAction(Action action);
}

如果想ListView中显示图片且自适应,需要设置 android:adjustViewBounds="true"

android ListView 可缩放,支持左右上下手势的更多相关文章

  1. Android viewpager + 可缩放的imageview

    http://files.cnblogs.com/files/liaolandemengxiang/PhotoWallFallsDemo.rar http://files.cnblogs.com/fi ...

  2. Android基础新手教程——3.8 Gestures(手势)

    Android基础新手教程--3.8 Gesture(手势) 标签(空格分隔): Android基础新手教程 本节引言: 周六不歇息,刚剪完了个大平头回来.继续码字~ 好的,本节给大家带来点的是第三章 ...

  3. Android:让WebView支持<input type=”file”…>元素

    最近在做一个活动页面:用户上传一张图片进行缩放.旋转后点击下一步填写内容后生成图片! 做好后经过各种测试是没有问题的,基本没有什么明显BUG,流程都能走通,但是嵌入到APP后,问题就来了! 在IOS上 ...

  4. android ListView 九大重要属性详细分析、

    android ListView 九大重要属性详细分析. 1.android ListView 一些重要属性详解,兄弟朋友可以参考一下. 首先是stackFromBottom属性,这只该属性之后你做好 ...

  5. 【腾讯Bugly干货分享】Android ListView与RecyclerView对比浅析--缓存机制

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/5811d3e3ab10c62013697408 作者:黄宁源 一,背景 Recy ...

  6. 【转】android ListView 几个重要属性

    android ListView 几个重要属性 分类: Android2012-03-08 19:25 19324人阅读 评论(5) 收藏 举报 listviewandroid活动javalistnu ...

  7. Android listview viewpager解决冲突 滑动

    Android listview viewpager滑动 跳动 冲突解决 ListView中嵌套ViewPage有或者滑动手势冲突解决   在listview 上使用 addHeaderView 在第 ...

  8. android listview中item通过viewpager实现《IT蓝豹》

    android listview中item通过viewpager实现 android listview中item通过viewpager实现,每一个item都支持viewpager实现图片切换功能.本项 ...

  9. Android ListView滚动条配置完全解析

    滚动条的相关显示效果 先来看下ListView的滚动条有哪些显示效果. 滚动条自身的外观 这点不用说,就是滚动条自身的颜色,形状等. Track的外观 默认的ListView是没有设置Track的.为 ...

随机推荐

  1. 阿里技术专家详解Dubbo实践,演进及未来规划

    https://mp.weixin.qq.com/s/9rVGHYfeE8yM2qkSVd2yEQ

  2. 【搬运】Wget 命令详解

    用过 Linux 系统的对于 wget 不陌生吧,从网上下载资源等操作都是少不了它,它体积小但功能集全,支持 FTP HTTP HTTPS 协议下载方式,支持断点续传 代理服务器. 现在 Window ...

  3. docker从容器里面拷文件到宿主机或从宿主机拷文件到docker容器里面

    1.从容器里面拷文件到宿主机? 答:在宿主机里面执行以下命令 docker cp 容器名:要拷贝的文件在容器里面的路径       要拷贝到宿主机的相应路径 示例: 假设容器名为testtomcat, ...

  4. ES6的小知识(前半部分)

    一.let与const的使用 let:用来声明一个变量,与var类似 1.用let声明的变量,所声明的变量只在命令所在的代码块内有效 function hander(){ let a = 10; co ...

  5. python实现http get请求

    接口请求方式为get请求,如下图抓包查看 Python实现脚本请求接口并以中文打印接口返回的数据 import urllib.parse import urllib.request url = &qu ...

  6. Struts2中 Action class not found 问题

    刚学Struts2时碰到了以下两个问题,都是没有正确配置struts.xml导致的,自己记录一下: 1.浏览器报404:The origin server did not find a current ...

  7. CSS中的盒子模型与 box-sizing 属性

    盒子模型是css中一个重要的概念,是开发网页必须要用的布局方法.盒子模型有两种,分别是标准 w3c 盒子模型和 ie 盒子模型. 标准 w3c 盒子模型:包括 magin(外边距).border(边框 ...

  8. Attempting to use uninitialized value

    ckpt = tf.train.get_checkpoint_state(FLAGS.model) if ckpt: saver.restore(sess, tf.train.latest_check ...

  9. 从rnn到lstm,再到seq2seq(二)

    从图上可以看出来,decode的过程其实都是从encode的最后一个隐层开始的,如果encode输入过长的话,会丢失很多信息,所以设计了attation机制. attation机制的decode的过程 ...

  10. 为什么会出现container、injection技术?发展历史及未来发展趋势

    container 原因: 随着软件开发的发展,相比于早期的集中式应用部署方式,现在的应用基本都是采用分布式的部署方式,一个应用可能包含多种服务或多个模块,因此多种服务可能部署在多种环境中,如虚拟服务 ...