package com.app;
import android.content.Context;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
/**
* 继承ImageView 实现了多点触碰的拖动和缩放
* @author Administrator
*
*/
public class TouchView extends ImageView
{
static final int NONE = 0;
static final int DRAG = 1; //拖动中
static final int ZOOM = 2; //缩放中
static final int BIGGER = 3; //放大ing
static final int SMALLER = 4; //缩小ing
private int mode = NONE; //当前的事件
private float beforeLenght; //两触点距离
private float afterLenght; //两触点距离
private float scale = 0.04f; //缩放的比例 X Y方向都是这个值 越大缩放的越快
private int screenW;
private int screenH;
/*处理拖动 变量 */
private int start_x;
private int start_y;
private int stop_x ;
private int stop_y ;
private TranslateAnimation trans; //处理超出边界的动画
public TouchView(Context context,int w,int h)
{
super(context);
this.setPadding(0, 0, 0, 0);
screenW = w;
screenH = h;
}
/**
* 就算两点间的距离
*/
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
/**
* 处理触碰..
*/
@Override
public boolean onTouchEvent(MotionEvent event)
{
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
mode = DRAG;
stop_x = (int) event.getRawX();
stop_y = (int) event.getRawY();
start_x = (int) event.getX();
start_y = stop_y - this.getTop();
if(event.getPointerCount()==2)
beforeLenght = spacing(event);
break;
case MotionEvent.ACTION_POINTER_DOWN:
if (spacing(event) > 10f) {
mode = ZOOM;
beforeLenght = spacing(event);
}
break;
case MotionEvent.ACTION_UP:
/*判断是否超出范围 并处理*/
int disX = 0;
int disY = 0;
if(getHeight()<=screenH || this.getTop()<0)
{
if(this.getTop()<0 )
{
int dis = getTop();
this.layout(this.getLeft(), 0, this.getRight(), 0 + this.getHeight());
disY = dis - getTop();
}
else if(this.getBottom()>screenH)
{
disY = getHeight()- screenH+getTop();
this.layout(this.getLeft(), screenH-getHeight(), this.getRight(), screenH);
}
}
if(getWidth()<=screenW)
{
if(this.getLeft()<0)
{
disX = getLeft();
this.layout(0, this.getTop(), 0+getWidth(), this.getBottom());
}
else if(this.getRight()>screenW)
{
disX = getWidth()-screenW+getLeft();
this.layout(screenW-getWidth(), this.getTop(), screenW, this.getBottom());
}
}
if(disX!=0 || disY!=0)
{
trans = new TranslateAnimation(disX, 0, disY, 0);
trans.setDuration(500);
this.startAnimation(trans);
}
mode = NONE;
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
/*处理拖动*/
if (mode == DRAG) {
if(Math.abs(stop_x-start_x-getLeft())<88 && Math.abs(stop_y - start_y-getTop())<85)
{
this.setPosition(stop_x - start_x, stop_y - start_y, stop_x + this.getWidth() - start_x, stop_y - start_y + this.getHeight());
stop_x = (int) event.getRawX();
stop_y = (int) event.getRawY();
}
}
/*处理缩放*/
else if (mode == ZOOM) {
if(spacing(event)>10f)
{
afterLenght = spacing(event);
float gapLenght = afterLenght - beforeLenght;
if(gapLenght == 0) {
break;
}
else if(Math.abs(gapLenght)>5f)
{
if(gapLenght>0) {
this.setScale(scale,BIGGER);
}else {
this.setScale(scale,SMALLER);
}
beforeLenght = afterLenght;
}
}
}
break;
}
return true;
}
/**
* 实现处理缩放
*/
private void setScale(float temp,int flag) {
if(flag==BIGGER) {
this.setFrame(this.getLeft()-(int)(temp*this.getWidth()),
this.getTop()-(int)(temp*this.getHeight()),
this.getRight()+(int)(temp*this.getWidth()),
this.getBottom()+(int)(temp*this.getHeight()));
}else if(flag==SMALLER){
this.setFrame(this.getLeft()+(int)(temp*this.getWidth()),
this.getTop()+(int)(temp*this.getHeight()),
this.getRight()-(int)(temp*this.getWidth()),
this.getBottom()-(int)(temp*this.getHeight()));
}
}
/**
* 实现处理拖动
*/
private void setPosition(int left,int top,int right,int bottom) {
this.layout(left,top,right,bottom);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
android imageview 多点触碰(Multitouch)实现图片拖拽移动缩放
共40个文件
class:14个
xml:6个
java:6个
5星 · 超过95%的资源 需积分: 9 131 下载量 110 浏览量
2014-01-13
12:35:22
上传
评论
收藏 497KB RAR 举报
温馨提示
android imageview 多点触碰(Multitouch)实现图片拖拽移动缩放 仿照官方版本的图片浏览效果
资源推荐
资源详情
资源评论
收起资源包目录
SecondAndroid.rar (40个子文件)
SecondAndroid
bin
classes.dex 12KB
com
app
TouchView.class 3KB
R$layout.class 361B
ImageViewImp$1.class 1006B
R$id.class 379B
R$drawable.class 590B
R$attr.class 307B
R.class 491B
Main.class 472B
ImageAdapter.class 2KB
ViewScroll.class 2KB
GalleryMain.class 2KB
R$string.class 394B
ImageViewImp.class 3KB
R$style.class 398B
SecondAndroid.apk 165KB
resources.ap_ 157KB
res
drawable-ldpi
icon.png 2KB
values-mdpi
drawable-hdpi
icon.png 4KB
values
colors.xml 210B
strings.xml 168B
style.xml 342B
drawable-mdpi
jpg.png 14KB
imageview_background.xml 403B
kua.png 7KB
icon.png 3KB
pic.jpg 124KB
layout
main.xml 457B
assets
default.properties 362B
gen
com
app
R.java 1KB
proguard.cfg 1KB
AndroidManifest.xml.bak 973B
src
com
app
ImageViewImp.java 2KB
ImageAdapter.java 1KB
ViewScroll.java 2KB
GalleryMain.java 1KB
TouchView.java 6KB
.project 849B
.classpath 280B
AndroidManifest.xml 737B
共 40 条
- 1
aaa939291641
- 粉丝: 3
- 资源: 32
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页