- 浏览: 110846 次
- 性别:
- 来自: 北京
最新评论
-
饭更稀:
饭更稀 写道楼主这里写错了吧,第一部分:高16位表示MODE, ...
android绘制view的过程之一---------计算view大小(measure)(转) -
饭更稀:
楼主这里写错了吧,第一部分:高16位表示MODE,不应该是高3 ...
android绘制view的过程之一---------计算view大小(measure)(转) -
hovei:
...
http协议 -
liufang205:
...
http协议
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; //处理超出边界的动画
/**
* 默认构造函数
* @param context
*/
public TouchView(Context context){
super(context);
}
/**
* 该构造方法在静态引入XML文件中是必须的
* @param context
* @param paramAttributeSet
*/
public TouchView(Context context,AttributeSet paramAttributeSet){
super(context,paramAttributeSet);
}
/**
* 该构造函数在动态创建时,指定图片的初始高宽
* @param context
* @param w
* @param h
*/
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);
}
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; //处理超出边界的动画
/**
* 默认构造函数
* @param context
*/
public TouchView(Context context){
super(context);
}
/**
* 该构造方法在静态引入XML文件中是必须的
* @param context
* @param paramAttributeSet
*/
public TouchView(Context context,AttributeSet paramAttributeSet){
super(context,paramAttributeSet);
}
/**
* 该构造函数在动态创建时,指定图片的初始高宽
* @param context
* @param w
* @param h
*/
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绘制view的过程之一---------计算view大小(measure)(转)
2013-01-16 11:40 34020http://www.eoeandroid.com/forum ... -
[转]Android开发学习笔记:数据存取之SQLite浅析
2012-11-28 10:58 990一.SQLite的介绍 1.SQLite ... -
android 背景改变
2012-07-20 14:35 1064一:在xml里面设置 <?xml version=&qu ... -
转 android view构造函数研究
2012-07-16 13:37 2389上周遇到了SurfaceView的constructor的问题 ... -
ImageView 上显示文字
2012-07-16 09:59 1369@Override protected void onDra ... -
android中ansycTask的用法
2012-06-11 17:09 1777在开发Android应用时必须遵守单线程模型的原则: Andr ... -
Android 使用AsyncTask 后监听异步加载完毕的动作
2012-06-11 17:07 1286AsyncTask 的使用方法网上有很多例子,使用起来也非常的 ... -
android九宫格
2012-06-11 10:31 11231:打开android-sdk文件夹下的tool工具中的dra ... -
androlid 自定义字体
2012-06-07 11:08 10481、Android系统默认支持三种字体,分别为:“sans”, ... -
android 完全退出程序
2012-06-07 10:39 839android.os.Process.killProcess( ... -
[Android] 如何将应用程序转移到SD卡
2012-06-04 17:14 2553其实也只是将程序里的. ... -
android中用到的一些属性
2012-06-04 11:58 0如何防止Android应用中的Service被系统回收? 很 ... -
关键字飞入飞出效果
2012-06-04 10:47 1358一关键字样式及动画 pac ... -
android 传感器 SensorManager
2012-05-28 17:10 923传感器是一种物理装置,能够探测、感受外界的信号、物理条件(如光 ... -
android listview内容向上滚动
2012-05-25 14:54 39371:调用类 public class MainActivity ... -
android ui
2012-05-25 09:32 1253Android ui 一、 图片处理。 a) Drawable ... -
安卓开发中Bitmap/Drawable/byte[]互换 及Bitmap.Config里的一些参数
2012-05-25 09:17 1202在网上搜了一些其它的方法,在这里分享下 1.Drawab ... -
转 android 内容提供者contentprovider
2012-04-12 14:30 0内容提供器用来存放和获取数据并使这些数据可以被所有的应用程序访 ... -
activity onSaveInstanceState
2012-04-09 10:21 0先看Application Fundamentals ... -
转 androidopengl学习
2012-03-30 11:40 0ApiDemos 的Graphics示例中 ...
相关推荐
android imageview 多点触碰(Multitouch)实现图片拖拽移动缩放 RT 仿照官方版本的图片浏览效果 新手
Android imageview 多点触碰(MultiTouch)实现图片拖拽移动缩放,下载即可运行。
使用自定义的ImageView实现了多点触控,图片的自由移动和缩放,而且解决了与ViewPager的兼容性问题。可以直接将这个ImageView控件使用到你的项目中去。
自定义控件继承Imageview,可随意调用,附调用实例。可实现空间中图片的多种操作,拖拽,缩放,旋转
Android开发之ImageView通过matrix实现两点缩放和图片拖动,内有较为详细的注释
自定义ImageView(可单点缩放回弹、拖拽、多点缩放)
本文通过一个自定义控件ZoomImageView,实现多点触控(移动,多点缩放),双击放大缩小,并在放大缩小过程自解决边界白边的情况,同时结合ViewPager的使用,并解决存在的事件冲突情况。
可手势缩放平移及旋转的ImageView。可以直接拿来用。参看博客http://blog.csdn.net/BigBoySunshine/article/details/78256008
自定义ImageView控件,具有缩放和拖拽功能,适合图片的浏览,方便复用
自定义ImageView实现图片的缩放功能源码,具体实现细节见博文:http://blog.csdn.net/l1028386804/article/details/50062231
支持多点触控缩放。移动。解决了与父容器的touch时间冲突问题。
实现了ImageView多点缩放效果,并使用gallery进行选择,通过多点触摸实现图片改变。
自定义可拖动可缩放的ImageView,通过Matrix和TouchListener实现,随后会加旋转。
android自定义ImageView实现缩放,回弹效果。
手势缩放的Imageview
该自定义控件实现的主要功能是控件的拖动和缩放(注意:不是对控件中的图片进行操作,话说很多帖子都把这两个混了),其中缩放可以按照三个方向进行,就是水平、竖直和等比例。双击操作只做了一个提示,长按加上了一...
Android多点触控涉及到的知识点 ...1、 ImageView的基础上继承 2、因为要在图片加载完成就获取到相关的属性,所以实现OnGlobalLayoutListener接口,并实现方法onGlobalLayout 注册OnGlobalLayoutLis
自定义imageview实现拖动、旋转、缩放等功能;
Android ImageView自定义实现,支持缩放,由不同的触控手势
NULL 博文链接:https://iaiai.iteye.com/blog/1931412