• Android多点触控(图片的缩放Demo)


    本文主要介绍Android的多点触控,使用了一个图片缩放的实例,来更好的说明其原理。须要实现OnTouchListener接口,重写当中的onTouch方法。

    实现效果图:

         

    源码:

    布局文件:

    activity_main:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
    
    </RelativeLayout>
    代码:

    package com.multitouch;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    import android.widget.ImageView;
    import android.widget.RelativeLayout;
    import android.widget.RelativeLayout.LayoutParams;
    
    /**
     * 多点触控Demo实例: 图片的缩放。
     * 
     */
    public class MainActivity extends Activity {
    	private RelativeLayout layout;
    	protected String TAG = "zhongyao";
    	private ImageView imageView;
    	private float currentDistance;
    	private float lastDistance = -1;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		layout = (RelativeLayout) findViewById(R.id.layout);
    		imageView = (ImageView) findViewById(R.id.imageView);
    
    		layout.setOnTouchListener(new OnTouchListener() {
    
    			@Override
    			public boolean onTouch(View v, MotionEvent event) {
    				switch (event.getAction()) {
    					/**
    					 * 手指按下
    					 */
    				case MotionEvent.ACTION_DOWN:
    					Log.d(TAG, "down!!!");
    					break;
    				/**
    				 * 手指移动
    				 */
    				case MotionEvent.ACTION_MOVE:
    					Log.d(TAG, "move!!!");
    					/**
    					 * 首先推断按下手指的个数是不是大于两个。
    					 * 假设大于两个则运行下面操作(即图片的缩放操作)。
    					 */
    					if (event.getPointerCount() >= 2) {
    
    						float offsetX = event.getX(0) - event.getX(1);
    						float offsetY = event.getY(0) - event.getY(1);
    						/**
    						 * 原点和滑动后点的距离差
    						 */
    						currentDistance = (float) Math.sqrt(offsetX * offsetX
    								+ offsetY * offsetY);
    						if (lastDistance < 0) {
    							lastDistance = currentDistance;
    						} else {
    								/**
    								 * 假设当前滑动的距离(currentDistance)比最后一次记录的距离(lastDistance)相比大于5英寸(也能够为其它尺寸),
    								 * 那么现实图片放大
    								 */
    							if (currentDistance - lastDistance > 5) {
    								Log.d(TAG, "放大!!!");
    								RelativeLayout.LayoutParams lp = (LayoutParams) imageView
    										.getLayoutParams();
    								/**
    								 * 图片宽高一次放大为原来图片的1.1倍(当然,也能够为其它数值)。
    								 */
    								lp.width = (int) (imageView.getWidth() * 1.1);
    								lp.height = (int) (imageView.getHeight() * 1.1);
    								imageView.setLayoutParams(lp);
    								lastDistance = currentDistance;
    								/**
    								 * 假设最后的一次记录的距离(lastDistance)与当前的滑动距离(currentDistance)相比小于5英寸,
    								 * 那么图片缩小。
    								 */
    							} else if (lastDistance - currentDistance > 5) {
    								Log.d(TAG, "缩小!!!");
    								RelativeLayout.LayoutParams lp = (LayoutParams) imageView
    										.getLayoutParams();
    								/**
    								 * 图片宽高一次缩小为原来图片的0.9倍。
    								 */
    								lp.width = (int) (imageView.getWidth() * 0.9);
    								lp.height = (int) (imageView.getHeight() * 0.9);
    								imageView.setLayoutParams(lp);
    								lastDistance = currentDistance;
    							}
    						}
    					}
    					break;
    				/**
    				 * 手指抬起
    				 */
    				case MotionEvent.ACTION_UP:
    					Log.d(TAG, "up!!!");
    					break;
    				}
    				return true;
    			}
    		});
    	}
    
    }
    

    源码下载:

    点击下载源代码

  • 相关阅读:
    致歉
    [公告]博客园正在对网站程序进行性能优化
    [公告]调整默认发布选项
    网站情况继续汇报
    定制“Server Too Busy”错误信息
    可恶的垃圾广告
    博客园分站服务器故障
    很值得期待—SharePoint "V3.0"新特性
    安装Vistual Studio 2005的小问题
    安装智能陈桥五笔时请小心
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4003179.html
Copyright © 2020-2023  润新知