onTouch触摸事件
2016-04-12 20:34
281 查看
public class TouchActivity extends Activity{ private ImageView touch_image; private float currentDistance;//记录首次触摸位置 private float lastDistance=-1;//记录最后一次触摸位置 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_touch); touch_image=(ImageView) findViewById(R.id.touch_image); touch_image.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN://按下 break; case MotionEvent.ACTION_MOVE://移动 if(event.getPointerCount()>=2){//触摸点个数 //父布局控件是什么 这里就定义什么 LinearLayout.LayoutParams lv=(LayoutParams) touch_image.getLayoutParams(); float offsetX=event.getX(0)-event.getX(1);//第一个触摸点到第二个的距离 float offsetY=event.getY(0)-event.getY(1); //勾股定理计算中间长度 currentDistance=(float) Math.sqrt((offsetX*offsetX)+(offsetY*offsetY)); lv.leftMargin=(int) event.getX(); lv.topMargin=(int) event.getY(); if (lastDistance<0) { lastDistance=currentDistance; touch_image.setLayoutParams(lv); }else { if(currentDistance-lastDistance>5){//5为px 距离太小 体验不佳 lastDistance=currentDistance; lv.width=(int) (event.getX()*1.1); lv.height=(int) (event.getY()*1.1); touch_image.setLayoutParams(lv); }else if(lastDistance-currentDistance>5){ lastDistance=currentDistance; lv.width=(int) (event.getX()*0.9); lv.height=(int) (event.getY()*0.9); touch_image.setLayoutParams(lv); } } } break; case MotionEvent.ACTION_UP: break; default: break; } return true; } }); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:swipe="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.mydemo.MainActivity" android:orientation="vertical" > <ImageView android:id="@+id/touch_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> </LinearLayout>
相关文章推荐
- 152. Maximum Product Subarray
- AIDL旅行记之开篇AIDL基本介绍
- C++第三次上机实验—友元函数
- C++作业3
- ZZULI 1864: 炉石传说【数学】
- 1870: 马拉松后记
- 【HDU】 1029 Ignatius and the Princess IV
- C++继承详解之四——is-a接口继承和has-a实现继承
- Android中ExpandableListView中嵌套ListView
- 算法——排序——快速排序
- Linux进程间通信——使用信号量
- Android Fragment生命周期
- Linux常见命令
- 《leetCode》:Kth Smallest Element in a BST
- 迷宫问题
- 行人检测 读书笔记 综述(1)
- LeetCode *** 46. Permutations
- Error C2662
- nginx学习记录
- Thread.isAlive()