初学Android,手势缩放图片(四十八)

news/2024/7/3 13:07:43 标签: android, matrix, float, velocity, class, 手机
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views">

下面的例子是用手势来缩放图片,手指往右划是放大,往左是缩小

下面效果在手机模拟器上呈现的只能是鼠标光标的划动了


class="language-java">package WangLi.IO.GestureZoom;

import class="tags" href="/tags/ANDROID.html" title=android>android.app.Activity;
import class="tags" href="/tags/ANDROID.html" title=android>android.graphics.Bitmap;
import class="tags" href="/tags/ANDROID.html" title=android>android.graphics.BitmapFactory;
import class="tags" href="/tags/ANDROID.html" title=android>android.graphics.Matrix;
import class="tags" href="/tags/ANDROID.html" title=android>android.graphics.drawable.BitmapDrawable;
import class="tags" href="/tags/ANDROID.html" title=android>android.os.Bundle;
import class="tags" href="/tags/ANDROID.html" title=android>android.view.GestureDetector;
import class="tags" href="/tags/ANDROID.html" title=android>android.view.GestureDetector.OnGestureListener;
import class="tags" href="/tags/ANDROID.html" title=android>android.view.MotionEvent;
import class="tags" href="/tags/ANDROID.html" title=android>android.widget.ImageView;

public class GestureZoom extends Activity implements OnGestureListener
{
	GestureDetector detector;
	ImageView imageView;
	//初始的图片资源
	Bitmap bitmap;
	//定义图片的宽,高
	int width,height;
	//记录当前的缩放比
	class="tags" href="/tags/FLOAT.html" title=float>float currentScale = 1;
	//控制图片缩放的Matrix对象
	Matrix class="tags" href="/tags/MATRIX.html" title=matrix>matrix;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //创建手势检测器
        detector = new GestureDetector(this);
        imageView = (ImageView)findViewById(R.id.show);
        class="tags" href="/tags/MATRIX.html" title=matrix>matrix = new Matrix();
        //获取被缩放的源图片
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.hotel);
        //获得位图宽
        width = bitmap.getWidth();
        //获得位图高
        height = bitmap.getHeight();
        //设置ImageView初始化时显示的图片
        imageView.setImageBitmap(BitmapFactory.decodeResource(
        		getResources(), R.drawable.hotel));
    }
    @Override
    public boolean onTouchEvent(MotionEvent me)
    {
    	//将Activity上的触碰事件交给GestureDetector处理
    	return detector.onTouchEvent(me);
    }
    
	@Override
	public boolean onDown(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, class="tags" href="/tags/FLOAT.html" title=float>float class="tags" href="/tags/VELOCITY.html" title=velocity>velocityX,
			class="tags" href="/tags/FLOAT.html" title=float>float class="tags" href="/tags/VELOCITY.html" title=velocity>velocityY) {
		// TODO Auto-generated method stub
		class="tags" href="/tags/VELOCITY.html" title=velocity>velocityX = class="tags" href="/tags/VELOCITY.html" title=velocity>velocityX > 4000 ? 4000 : class="tags" href="/tags/VELOCITY.html" title=velocity>velocityX;
		class="tags" href="/tags/VELOCITY.html" title=velocity>velocityX = class="tags" href="/tags/VELOCITY.html" title=velocity>velocityX < -4000 ? -4000 : class="tags" href="/tags/VELOCITY.html" title=velocity>velocityX;
		//根据手势的速度来计算缩放比,如果class="tags" href="/tags/VELOCITY.html" title=velocity>velocity > 0,放大图像,否则缩小图像
		currentScale += currentScale * class="tags" href="/tags/VELOCITY.html" title=velocity>velocityX / 4000.0f;
		//保证currentScale不会等于0
		currentScale = currentScale > 0.01 ? currentScale : 0.01f;
		//重置Matrix
		class="tags" href="/tags/MATRIX.html" title=matrix>matrix.reset();
		//缩放Matrix
		class="tags" href="/tags/MATRIX.html" title=matrix>matrix.setScale(currentScale, currentScale ,160, 200);
		BitmapDrawable tmp = (BitmapDrawable)imageView.getDrawable();
		//如果图片还未回收,先强制回收该图片
		if(!tmp.getBitmap().isRecycled())
		{
			tmp.getBitmap().recycle();
		}
		//根据原始位图和Matrix创建新图片
		Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, width, height, class="tags" href="/tags/MATRIX.html" title=matrix>matrix,true);
		imageView.setImageBitmap(bitmap2);
		return false;
	}
	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
	}
	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, class="tags" href="/tags/FLOAT.html" title=float>float distanceX,
			class="tags" href="/tags/FLOAT.html" title=float>float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
	}
	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
}



http://www.niftyadmin.cn/n/1411332.html

相关文章

Redis学习(一)数据类型、Java中使用redis、缓存概念

文章目录 常用数据结构String类型Hash类型List类型Set类型SortedSet 类型 通用命令key的层级结构 Spring Data Redis快速入门RedisTemplate的序列化方式StringRedisTemplateRedisTemplate的Hash类型操作 实战操作短信登录发送验证码校验登录信息校验登录状态 商家查询缓存缓存更…

Go语言开发分布式聊天室

声明 我是一个刚学go语言的菜鸟&#xff0c;还没有资格谈论什么技术分享&#xff0c;只是为了展示fooking的实际应用&#xff0c;同时把我用go写的聊天室代码贴出来供大家消遣&#xff0c;如果有入不了各位法眼的代码&#xff0c;望轻喷。该聊天室基于fooking&#xff0c;而业务…

初学Android,手势翻页效果(四十九)

下面是一个利用手势功能做的一个翻页效果上图实际是一个ViewFlipper容器&#xff0c;里面放了5张图片&#xff0c;从右向左划动&#xff0c;看下一张图片&#xff0c;从左向右划动&#xff0c;看上一张图片 同时在划动时播放动画(缩放和透明度的改变)&#xff0c;这样看起来更生…

完全理解高阶组件

高阶组件定义 a higher-order component is a function that takes a component and returns a new component翻译&#xff1a;高阶组件就是一个函数&#xff0c;且该函数接受一个组件作为参数&#xff0c;并返回一个新的组件。 理解了吗&#xff1f;看了定义似懂非懂&#x…

初学Android,增加手势到手势库(五十)

增加手势到手势库&#xff0c;我觉得这是触摸屏智能机最有用的一个功能之一&#xff0c;自己画一个手势保存下来&#xff0c;然后定义它执行的相应反应 Android使用GestureLibrary来代表手势库&#xff0c;并使用GestureLibraries工具类来创建手势库[html] view plaincopyprint…

java 远程调试(ij idea)

2019独角兽企业重金招聘Python工程师标准>>> 遇见一个怪异问题&#xff0c;无奈线上数据库有限制&#xff0c;只能远程调试下代码。突然发现&#xff0c;远程调试代码真的好简单&#xff0c;简单记录下操作步骤。 1、在idea里创建一个Remote&#xff0c;远程连接的入…

Mockjs

什么是Mockjs 在Mockjs的官网中&#xff0c;就直接能看到这样的一句话&#xff1a;生成随机数据&#xff0c;拦截Ajax请求。 这句话集中体现了Mock.js能够干什么&#xff01; Mockjs的使用场景 如今&#xff0c;前端所承载的东西越来越多&#xff0c;前后端分离开发已经渐渐…

yii2 数据库操作详解(转载)

开始使用数据库首先需要配置数据库连接组件&#xff0c;通过添加 db 组件到应用配置实现&#xff08;"基础的" Web 应用是 config/web.php&#xff09;&#xff0c;DSN( Data Source Name )是数据源名称&#xff0c;用于指定数据库信息.如下所示&#xff1a; 1 retur…