多点触摸缩放图片

自定义ImageView
package com.example.test;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.AsyncTask;
import android.text.method.MovementMethod;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

/****
 * 这里你要明白几个方法执行的流程: 首先ImageView是继承自View的子类.
 * onLayout方法:是一个回调方法.该方法会在在View中的layout方法中执行,在执行layout方法前面会首先执行setFrame方法.
 * layout方法:
 * setFrame方法:判断我们的View是否发生变化,如果发生变化,那么将最新的l,t,r,b传递给View,然后刷新进行动态更新UI.
 * 并且返回ture.没有变化返回false.
 * 
 * invalidate方法:用于刷新当前控件,
 */
public class DragImageView extends ImageView {

	private Activity mActivity;

	private int screen_W, screen_H;// 可见屏幕的宽高度

	private int bitmap_W, bitmap_H;// 当前图片宽高

	private int MAX_W, MAX_H, MIN_W, MIN_H;// 极限值

	private int current_Top, current_Right, current_Bottom, current_Left;// 当前图片上下左右坐标

	private int start_Top = -1, start_Right = -1, start_Bottom = -1,
			start_Left = -1;// 初始化默认位置.

	private int start_x, start_y, current_x, current_y;// 触摸位置

	private float beforeLenght, afterLenght;// 两触点距离

	private float scale_temp;// 缩放比例

	/**
	 * 模式 NONE:无 DRAG:拖拽. ZOOM:缩放
	 * 
	 * @author zhangjia
	 * 
	 */
	private enum MODE {
		NONE, DRAG, ZOOM

	};

	private MODE mode = MODE.NONE;// 默认模式

	private boolean isControl_V = false;// 垂直监控

	private boolean isControl_H = false;// 水平监控

	private ScaleAnimation scaleAnimation;// 缩放动画

	private boolean isScaleAnim = false;// 缩放动画

	private MyAsyncTask myAsyncTask;// 异步动画

	/** 构造方法 **/
	public DragImageView(Context context) {
		super(context);
	}

	public void setmActivity(Activity mActivity) {
		this.mActivity = mActivity;
	}

	/** 可见屏幕宽度 **/
	public void setScreen_W(int screen_W) {
		this.screen_W = screen_W;
	}

	/** 可见屏幕高度 **/
	public void setScreen_H(int screen_H) {
		this.screen_H = screen_H;
	}

	public DragImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	/***
	 * 设置显示图片
	 */
	@Override
	public void setImageBitmap(Bitmap bm) {
		super.setImageBitmap(bm);
		/** 获取图片宽高 **/
		bitmap_W = bm.getWidth();
		bitmap_H = bm.getHeight();

		MAX_W = bitmap_W * 3;
		MAX_H = bitmap_H * 3;

		MIN_W = bitmap_W / 2;
		MIN_H = bitmap_H / 2;

	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		if (start_Top == -1) {
			start_Top = top;
			start_Left = left;
			start_Bottom = bottom;
			start_Right = right;
		}

	}

	/***
	 * touch 事件
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		/** 处理单点、多点触摸 **/
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			onTouchDown(event);
			break;
		// 多点触摸
		case MotionEvent.ACTION_POINTER_DOWN:
			onPointerDown(event);
			break;

		case MotionEvent.ACTION_MOVE:
			onTouchMove(event);
			break;
		case MotionEvent.ACTION_UP:
			mode = MODE.NONE;
			break;

		// 多点松开
		case MotionEvent.ACTION_POINTER_UP:
			mode = MODE.NONE;
			/** 执行缩放还原 **/
			if (isScaleAnim) {
				doScaleAnim();
			}
			break;
		}

		return true;
	}

	/** 按下 **/
	void onTouchDown(MotionEvent event) {
		mode = MODE.DRAG;

		current_x = (int) event.getRawX();
		current_y = (int) event.getRawY();

		start_x = (int) event.getX();
		start_y = current_y - this.getTop();

	}

	/** 两个手指 只能放大缩小 **/
	void onPointerDown(MotionEvent event) {
		if (event.getPointerCount() == 2) {
			mode = MODE.ZOOM;
			beforeLenght = getDistance(event);// 获取两点的距离
		}
	}

	/** 移动的处理 **/
	void onTouchMove(MotionEvent event) {
		int left = 0, top = 0, right = 0, bottom = 0;
		/** 处理拖动 **/
		if (mode == MODE.DRAG) {

			/** 在这里要进行判断处理,防止在drag时候越界 **/

			/** 获取相应的l,t,r ,b **/
			left = current_x - start_x;
			right = current_x + this.getWidth() - start_x;
			top = current_y - start_y;
			bottom = current_y - start_y + this.getHeight();

			/** 水平进行判断 **/
			if (isControl_H) {
				if (left >= 0) {
					left = 0;
					right = this.getWidth();
				}
				if (right <= screen_W) {
					left = screen_W - this.getWidth();
					right = screen_W;
				}
			} else {
				left = this.getLeft();
				right = this.getRight();
			}
			/** 垂直判断 **/
			if (isControl_V) {
				if (top >= 0) {
					top = 0;
					bottom = this.getHeight();
				}

				if (bottom <= screen_H) {
					top = screen_H - this.getHeight();
					bottom = screen_H;
				}
			} else {
				top = this.getTop();
				bottom = this.getBottom();
			}
			if (isControl_H || isControl_V)
				this.setPosition(left, top, right, bottom);

			current_x = (int) event.getRawX();
			current_y = (int) event.getRawY();

		}
		/** 处理缩放 **/
		else if (mode == MODE.ZOOM) {
			afterLenght = getDistance(event);// 获取两点的距离
			float gapLenght = afterLenght - beforeLenght;// 变化的长度
			if (Math.abs(gapLenght) > 5f) {
				scale_temp = afterLenght / beforeLenght;// 求的缩放的比例
				this.setScale(scale_temp);
				beforeLenght = afterLenght;
			}
		}
	}

	/** 获取两点的距离 **/
	float getDistance(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);
	}

	/** 实现处理拖动 **/
	private void setPosition(int left, int top, int right, int bottom) {
		this.layout(left, top, right, bottom);
	}

	/** 处理缩放 **/
	void setScale(float scale) {
		int disX = (int) (this.getWidth() * Math.abs(1 - scale)) / 4;// 获取缩放水平距离
		int disY = (int) (this.getHeight() * Math.abs(1 - scale)) / 4;// 获取缩放垂直距离

		// 放大
		if (scale > 1 && this.getWidth() <= MAX_W) {
			current_Left = this.getLeft() - disX;
			current_Top = this.getTop() - disY;
			current_Right = this.getRight() + disX;
			current_Bottom = this.getBottom() + disY;
			this.setFrame(current_Left, current_Top, current_Right,
					current_Bottom);
			/***
			 * 此时因为考虑到对称,所以只做一遍判断就可以了。
			 */
			if (current_Top <= 0 && current_Bottom >= screen_H) {
		//		Log.e("jj", "屏幕高度=" + this.getHeight());
				isControl_V = true;// 开启垂直监控
			} else {
				isControl_V = false;
			}
			if (current_Left <= 0 && current_Right >= screen_W) {
				isControl_H = true;// 开启水平监控
			} else {
				isControl_H = false;
			}
		}
		// 缩小
		else if (scale < 1 && this.getWidth() >= MIN_W) {
			current_Left = this.getLeft() + disX;
			current_Top = this.getTop() + disY;
			current_Right = this.getRight() - disX;
			current_Bottom = this.getBottom() - disY;
			/***
			 * 在这里要进行缩放处理
			 */
			// 上边越界
			if (isControl_V && current_Top > 0) {
				current_Top = 0;
				current_Bottom = this.getBottom() - 2 * disY;
				if (current_Bottom < screen_H) {
					current_Bottom = screen_H;
					isControl_V = false;// 关闭垂直监听
				}
			}
			// 下边越界
			if (isControl_V && current_Bottom < screen_H) {
				current_Bottom = screen_H;
				current_Top = this.getTop() + 2 * disY;
				if (current_Top > 0) {
					current_Top = 0;
					isControl_V = false;// 关闭垂直监听
				}
			}
			// 左边越界
			if (isControl_H && current_Left >= 0) {
				current_Left = 0;
				current_Right = this.getRight() - 2 * disX;
				if (current_Right <= screen_W) {
					current_Right = screen_W;
					isControl_H = false;// 关闭
				}
			}
			// 右边越界
			if (isControl_H && current_Right <= screen_W) {
				current_Right = screen_W;
				current_Left = this.getLeft() + 2 * disX;
				if (current_Left >= 0) {
					current_Left = 0;
					isControl_H = false;// 关闭
				}
			}
			if (isControl_H || isControl_V) {
				this.setFrame(current_Left, current_Top, current_Right,
						current_Bottom);
			} else {
				this.setFrame(current_Left, current_Top, current_Right,
						current_Bottom);
				isScaleAnim = true;// 开启缩放动画
			}
		}
	}

	/***
	 * 缩放动画处理
	 */
	public void doScaleAnim() {
		myAsyncTask = new MyAsyncTask(screen_W, this.getWidth(),
				this.getHeight());
		myAsyncTask.setLTRB(this.getLeft(), this.getTop(), this.getRight(),
				this.getBottom());
		myAsyncTask.execute();
		isScaleAnim = false;// 关闭动画
	}

	/***
	 * 回缩动画執行
	 */
	class MyAsyncTask extends AsyncTask<Void, Integer, Void> {
		private int screen_W, current_Width, current_Height;

		private int left, top, right, bottom;

		private float scale_WH;// 宽高的比例

		/** 当前的位置属性 **/
		public void setLTRB(int left, int top, int right, int bottom) {
			this.left = left;
			this.top = top;
			this.right = right;
			this.bottom = bottom;
		}

		private float STEP = 8f;// 步伐
		private float step_H, step_V;// 水平步伐,垂直步伐
		public MyAsyncTask(int screen_W, int current_Width, int current_Height) {
			super();
			this.screen_W = screen_W;
			this.current_Width = current_Width;
			this.current_Height = current_Height;
			scale_WH = (float) current_Height / current_Width;
			step_H = STEP;
			step_V = scale_WH * STEP;
		}

		@Override
		protected Void doInBackground(Void... params) {
			while (current_Width <= screen_W) {
				left -= step_H;
				top -= step_V;
				right += step_H;
				bottom += step_V;

				current_Width += 2 * step_H;

				left = Math.max(left, start_Left);
				top = Math.max(top, start_Top);
				right = Math.min(right, start_Right);
				bottom = Math.min(bottom, start_Bottom);
                Log.e("jj", "top="+top+",bottom="+bottom+",left="+left+",right="+right);
				onProgressUpdate(new Integer[] { left, top, right, bottom });
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			return null;
		}

		@Override
		protected void onProgressUpdate(final Integer... values) {
			super.onProgressUpdate(values);
			mActivity.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					setFrame(values[0], values[1], values[2], values[3]);
				}
			});
		}
	}
}

执行调用Activity
package com.example.test;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageView;

public class Test extends Activity {

	private int state_height;
	private int screenWidth;
	private int screenHeight;
	private ViewTreeObserver viewTreeObserver;
	private DragImageView dragImageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.test);
		dragImageView = (DragImageView) findViewById(R.id.img);
		//获取屏幕宽高
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		screenWidth = dm.widthPixels;
		screenHeight = dm.heightPixels;
		//读取图片并且按宽度比例压缩
		Bitmap bitmap = BitmapUtil.ReadBitmapById(this, R.drawable.ke,screenWidth, screenHeight);
		dragImageView.setImageBitmap(bitmap);
		// 传入Activity以更新缩放画面
		dragImageView.setmActivity(this);
		/** 测量状态栏高度 **/
		viewTreeObserver = dragImageView.getViewTreeObserver();
		viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

				@Override
				public void onGlobalLayout() {
					if (state_height == 0) {
						// 获取状态栏高度
						Rect frame = new Rect();
						getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
						state_height = frame.top;
						//去掉状态栏高度
						dragImageView.setScreen_H(screenHeight- state_height);
						dragImageView.setScreen_W(screenWidth);
					}
				}
		});
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要实现两个触摸手指缩放图片,你可以使用 `kivy` 框架来实现。下面是一个简单的示例代码,演示了如何通过两个手指的缩放来调整图片的大小: ``` from kivy.app import App from kivy.uix.image import Image from kivy.uix.scatter import Scatter class MyScatter(Scatter): def __init__(self, **kwargs): super(MyScatter, self).__init__(**kwargs) self.last_pinch_distance = 0 def on_touch_down(self, touch): if len(self.touches) == 2: x1, y1 = self.touches[0].pos x2, y2 = self.touches[1].pos self.last_pinch_distance = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 return super(MyScatter, self).on_touch_down(touch) def on_touch_move(self, touch): if len(self.touches) == 2: x1, y1 = self.touches[0].pos x2, y2 = self.touches[1].pos current_pinch_distance = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 pinch_distance_delta = current_pinch_distance - self.last_pinch_distance if pinch_distance_delta < 0: self.scale -= 0.01 elif pinch_distance_delta > 0: self.scale += 0.01 self.last_pinch_distance = current_pinch_distance return super(MyScatter, self).on_touch_move(touch) class ImageApp(App): def build(self): scatter = MyScatter() image = Image(source='image.jpg') scatter.add_widget(image) return scatter if __name__ == '__main__': ImageApp().run() ``` 在这个程序中,我们创建了一个 `MyScatter` 类,继承自 `Scatter`,并重写了 `on_touch_down` 和 `on_touch_move` 方法,以实现手势缩放功能。当第一个手指按下屏幕时,记录下当前两个手指之间的距离;当第二个手指移动时,计算当前两个手指之间的距离与上一次的距离之差,从而确定手势缩放的方向。如果距离之差小于 0,那么就缩小图片;如果距离之差大于 0,那么就放大图片。请注意,这里我们使用了 `Scatter` 类来实现图片的平移和缩放。 在 `build` 方法中,我们创建了一个 `MyScatter` 对象,并将一个 `Image` 对象作为其子控件添加到其中。最后,我们运行了 `ImageApp` 应用程序。 注意,在运行这个程序之前,你需要安装 `kivy` 框架,并正确配置你的环境。如果你使用的是 Android 设备,你还需要安装 `Kivy Launcher` 应用程序,并将上述代码保存为 `main.py` 文件,然后将 `image.jpg` 文件复制到 `/sdcard/kivy/examples/image_app` 目录下,最后在 `Kivy Launcher` 中运行这个程序即可。如果你使用的是其他平台,可以参考 `kivy` 官方文档进行配置。 ### 回答2: Python可以通过使用处理图像的库,如PIL或OpenCV,来实现两个触摸手指缩放图片的功能。以下是一个简单的实现示例: 首先,您需要安装PIL或OpenCV库。如果您使用的是Anaconda环境,可以在终端中运行以下命令来安装PIL和OpenCV: ``` conda install pillow opencv ``` 接下来,您需要加载图片并创建一个画布来显示图片。这可以使用PIL库中的Image和ImageTk模块来完成。以下是一个创建画布并显示图片的示例: ```python from PIL import Image, ImageTk import tkinter as tk # 创建一个窗口 window = tk.Tk() # 加载图片 image = Image.open('path_to_your_image.jpg') # 创建一个画布来显示图片 canvas = tk.Canvas(window, width=image.width, height=image.height) canvas.pack() # 在画布上绘制图片 image_tk = ImageTk.PhotoImage(image) canvas.create_image(0, 0, image=image_tk, anchor=tk.NW) # 运行窗口的主循环 window.mainloop() ``` 然后,您可以为画布添加触摸事件,以便在触摸手指缩放时处理图片缩放。以下是一个添加触摸事件并实现图片缩放的示例: ```python from PIL import Image, ImageTk import tkinter as tk # 创建一个窗口 window = tk.Tk() # 加载图片 image = Image.open('path_to_your_image.jpg') # 创建一个画布来显示图片 canvas = tk.Canvas(window, width=image.width, height=image.height) canvas.pack() # 在画布上绘制图片 image_tk = ImageTk.PhotoImage(image) canvas.create_image(0, 0, image=image_tk, anchor=tk.NW) # 定义触摸手指缩放函数 def zoom(event): # 获取触摸手指的数量和位置 fingers = len(event.touches) x1, y1, x2, y2 = canvas.bbox("all") cx = (x1 + x2) / 2 cy = (y1 + y2) / 2 # 计算缩放比例 scale = 1.0 + fingers * 0.1 # 缩放图片 new_width = int(image.width * scale) new_height = int(image.height * scale) resized_image = image.resize((new_width, new_height)) resized_image_tk = ImageTk.PhotoImage(resized_image) # 更新画布上的图片 canvas.delete("all") canvas.create_image(cx, cy, image=resized_image_tk, anchor=tk.CENTER) # 绑定触摸事件到画布 canvas.bind("<B1-Motion>", zoom) # 运行窗口的主循环 window.mainloop() ``` 以上代码将在窗口中显示加载的图片,并允许您用两个触摸手指缩放图片。当您用两个手指在画布上滑动时,图片将根据手指的移动进行缩放。 ### 回答3: 要实现用python实现两个触摸手指缩放图片,可以使用OpenCV和numpy库来处理图像,以及使用pyautogui库来模拟鼠标点击和滚轮操作。 首先,需要读取原始图片,并将其转换为OpenCV的图像对象。可以使用OpenCV的imread函数来读取图片。 接下来,创建一个窗口来显示图片,并将图像对象显示在窗口中。 然后,可以使用pyautogui库来检测鼠标点击和滚轮滚动事件。在每次点击事件中,记录下鼠标点击的坐标。 接着,使用numpy库来处理图像缩放。根据两个触摸手指的点击坐标,计算手指间的距离。将这个距离与原始距离比较,计算缩放比例。 最后,根据计算得到的缩放比例,使用OpenCV的resize函数来调整图像的大小,并将调整后的图像显示在窗口中。 整个过程可以通过一个循环来实现,不断监听鼠标点击和滚轮滚动事件,实时更新图像的缩放比例和显示结果。 需要注意的是,该实现方法仅适用于在支持多点触控且具备缩放功能的设备上运行。对于没有相应硬件支持的设备,无法实现此功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值