android 多点触摸应用
2017-02-07 18:46
330 查看
package com.example.touchdemo;
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.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private FrameLayout flt_touch;
private ImageView iv_img;
//当前的距离
float correntDistance;
//最后上次的距离
float lastDistance=-1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flt_touch=(FrameLayout) findViewById(R.id.flt_touch);
iv_img=(ImageView) findViewById(R.id.iv_img);
flt_touch.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//触摸按下
Log.d("touch", "触摸按下");
// Toast.makeText(MainActivity.this, "触摸按下", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_MOVE:
//触摸移动
Log.d("touch", "触摸移动");
//获取触摸点打印出来
Log.d("touch", "x: "+event.getX()+", y: "+event.getY());
// Toast.makeText(MainActivity.this, "触摸移动", Toast.LENGTH_SHORT).show();
// 重新设置控件布局
FrameLayout.LayoutParams lp=(LayoutParams) iv_img.getLayoutParams();
lp.leftMargin=(int) event.getX();
lp.topMargin=(int) event.getY();
iv_img.setLayoutParams(lp);
//获取多个触摸点 event.getX(1) event.getX(2)
Log.d("touch", "x1: "+event.getX(0)+", y1: "+event.getY(0));
if(event.getPointerCount()==2){
Log.d("touch", "x2: "+event.getX(1)+", y2: "+event.getY(1));
}
//获取触摸点总数量
Log.d("touch", String.valueOf(event.getPointerCount()));
//放大或缩小图片
if(event.getPointerCount()>=2){
float offsetX=event.getX(0)-event.getX(1);
float offsetY=event.getY(0)-event.getY(1);
correntDistance=(float)Math.sqrt(offsetX*offsetX+offsetY*offsetY);
Log.d("touch", correntDistance+"");
if(lastDistance<0){
lastDistance=correntDistance;
}else{
//理论是0,但是安卓手指按下,总有一些偏大偏小变化,所以这里值是5以上比较合试
if(correntDistance-lastDistance>5){
Log.d("touch", "放大");
FrameLayout.LayoutParams lpImg=(LayoutParams) iv_img.getLayoutParams();
lpImg.width=(int) (iv_img.getWidth()*1.1f);
lpImg.height=(int) (iv_img.getHeight()*1.1f);
iv_img.setLayoutParams(lp);
lastDistance=correntDistance;
}else if(lastDistance-correntDistance>5){
Log.d("touch", "缩小");
FrameLayout.LayoutParams lpImg=(LayoutParams) iv_img.getLayoutParams();
lpImg.width=(int) (iv_img.getWidth()*0.9f);
lpImg.height=(int) (iv_img.getHeight()*0.9f);
iv_img.setLayoutParams(lp);
lastDistance=correntDistance;
}
}
}
break;
case MotionEvent.ACTION_UP:
//触摸弹起
Log.d("touch", "触摸弹起");
// Toast.makeText(MainActivity.this, "触摸弹起", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
});
}
}
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.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private FrameLayout flt_touch;
private ImageView iv_img;
//当前的距离
float correntDistance;
//最后上次的距离
float lastDistance=-1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flt_touch=(FrameLayout) findViewById(R.id.flt_touch);
iv_img=(ImageView) findViewById(R.id.iv_img);
flt_touch.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//触摸按下
Log.d("touch", "触摸按下");
// Toast.makeText(MainActivity.this, "触摸按下", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_MOVE:
//触摸移动
Log.d("touch", "触摸移动");
//获取触摸点打印出来
Log.d("touch", "x: "+event.getX()+", y: "+event.getY());
// Toast.makeText(MainActivity.this, "触摸移动", Toast.LENGTH_SHORT).show();
// 重新设置控件布局
FrameLayout.LayoutParams lp=(LayoutParams) iv_img.getLayoutParams();
lp.leftMargin=(int) event.getX();
lp.topMargin=(int) event.getY();
iv_img.setLayoutParams(lp);
//获取多个触摸点 event.getX(1) event.getX(2)
Log.d("touch", "x1: "+event.getX(0)+", y1: "+event.getY(0));
if(event.getPointerCount()==2){
Log.d("touch", "x2: "+event.getX(1)+", y2: "+event.getY(1));
}
//获取触摸点总数量
Log.d("touch", String.valueOf(event.getPointerCount()));
//放大或缩小图片
if(event.getPointerCount()>=2){
float offsetX=event.getX(0)-event.getX(1);
float offsetY=event.getY(0)-event.getY(1);
correntDistance=(float)Math.sqrt(offsetX*offsetX+offsetY*offsetY);
Log.d("touch", correntDistance+"");
if(lastDistance<0){
lastDistance=correntDistance;
}else{
//理论是0,但是安卓手指按下,总有一些偏大偏小变化,所以这里值是5以上比较合试
if(correntDistance-lastDistance>5){
Log.d("touch", "放大");
FrameLayout.LayoutParams lpImg=(LayoutParams) iv_img.getLayoutParams();
lpImg.width=(int) (iv_img.getWidth()*1.1f);
lpImg.height=(int) (iv_img.getHeight()*1.1f);
iv_img.setLayoutParams(lp);
lastDistance=correntDistance;
}else if(lastDistance-correntDistance>5){
Log.d("touch", "缩小");
FrameLayout.LayoutParams lpImg=(LayoutParams) iv_img.getLayoutParams();
lpImg.width=(int) (iv_img.getWidth()*0.9f);
lpImg.height=(int) (iv_img.getHeight()*0.9f);
iv_img.setLayoutParams(lp);
lastDistance=correntDistance;
}
}
}
break;
case MotionEvent.ACTION_UP:
//触摸弹起
Log.d("touch", "触摸弹起");
// Toast.makeText(MainActivity.this, "触摸弹起", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
});
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/flt_touch" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/iv_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/>h </FrameLayout>
相关文章推荐
- Android音频开发之尝试音频混合
- android:single和android:maxLines的区别
- Android JNI基础
- Android简介
- Android LayoutParams用法解析
- Android仿ios对话框
- Android 属性动画(Property Animation) 完全解析
- android studio 代码上传到码云git@osc
- Android仿支付宝信用分仪表盘控件
- Android之 RecyclerView,CardView 详解和相对应的上拉刷新下拉加载
- Android Studio 常用快捷键
- Android+H5混合开发
- 开源分享三(炫酷的Android Loading动画)
- Android如何与JavaScript交互
- Android中使用软引用和弱引用避免OOM的方法
- [置顶] AndroidStudio生成自己的Compile依赖
- 3des加密Android和iOS的加密算法有时候位数是不同的。解决办法
- Android Rom
- android 上传图片(压缩) Bitmap 转File
- 大神总结的Android小经验