Android實(shí)現(xiàn)多點(diǎn)觸摸操作
Android中的多點(diǎn)觸摸可以實(shí)現(xiàn)圖片的放大、縮小和旋轉(zhuǎn)等處理,供大家參考,具體內(nèi)容如下
主要通過(guò)setOnTouchListener方法來(lái)監(jiān)聽(tīng)用戶(hù)的觸摸事件,通過(guò)event.getX(0)和 event.getX(1)來(lái)獲取第一個(gè)觸控點(diǎn)和第二個(gè)觸控點(diǎn)的x軸(或者y軸)坐標(biāo),接下來(lái)在MotionEvent.ACTION_DOWN、MotionEvent.ACTION_MOVE、MotionEvent.ACTION_UP這幾種情況中來(lái)對(duì)獲取到的x軸或者y軸進(jìn)行處理,就能實(shí)現(xiàn)我們想要的效果了。
下面這個(gè)小Demo實(shí)現(xiàn)了對(duì)圖片的放大和縮小處理:
package com.example.administrator.translation; ? import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import android.widget.RelativeLayout; ? ? public class MainActivity extends ActionBarActivity { ? ? ? private RelativeLayout layout; ? ? private ImageView iv; ? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? ? layout = (RelativeLayout) findViewById(R.id.layout); ? ? ? ? iv = (ImageView) findViewById(R.id.iv); ? ? ? ? ? ? layout.setOnTouchListener(new View.OnTouchListener() { ? ? ? ? ? ? ? float currentDistance; ? ? ? ? ? ? float lastDistance = -1; ? ? ? ? ? ? ? @Override ? ? ? ? ? ? public boolean onTouch(View v, MotionEvent event) { ? ? ? ? ? ? ? ? switch (event.getAction()) { ? ? ? ? ? ? ? ? ? ? case MotionEvent.ACTION_DOWN: ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case MotionEvent.ACTION_MOVE: ? ? ? ? ? ? ? ? ? ? ? ? //判斷幾個(gè)觸控點(diǎn) ? ? ? ? ? ? ? ? ? ? ? ? if (event.getPointerCount() >= 2) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? //兩點(diǎn)之間x的坐標(biāo)差 ? ? ? ? ? ? ? ? ? ? ? ? ? ? float offsetX = event.getX(0) - event.getX(1); ? ? ? ? ? ? ? ? ? ? ? ? ? ? //兩點(diǎn)之間y的坐標(biāo)差 ? ? ? ? ? ? ? ? ? ? ? ? ? ? float offsetY = event.getY(0) - event.getY(1); ? ? ? ? ? ? ? ? ? ? ? ? ? ? //兩點(diǎn)之間的距離 ? ? ? ? ? ? ? ? ? ? ? ? ? ? currentDistance = (float) Math.sqrt(offsetX * offsetX + offsetY * offsetY); ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (lastDistance < 0) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //沒(méi)有縮放 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? lastDistance = currentDistance; ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (currentDistance - lastDistance > 5) {//放大 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) iv.getLayoutParams(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? lp.width = (int) (1.1f * iv.getWidth()); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? lp.height = (int) (1.1f * iv.getHeight()); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? iv.setLayoutParams(lp); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? lastDistance = currentDistance; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else if (currentDistance - lastDistance < -5) {//縮小 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int currentIvWidth = iv.getWidth(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int currentIvHeight = iv.getHeight(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (currentIvWidth > 50 && currentIvHeight >50) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) iv.getLayoutParams(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? lp.width = (int) (0.9f * iv.getWidth()); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? lp.height = (int) (0.9f * iv.getHeight()); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? iv.setLayoutParams(lp); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? lastDistance = currentDistance; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case MotionEvent.ACTION_UP: ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? return true; ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? } ? ? ? ? @Override ? ? public boolean onCreateOptionsMenu(Menu menu) { ? ? ? ? // Inflate the menu; this adds items to the action bar if it is present. ? ? ? ? getMenuInflater().inflate(R.menu.menu_main, menu); ? ? ? ? return true; ? ? } ? ? ? @Override ? ? public boolean onOptionsItemSelected(MenuItem item) { ? ? ? ? // Handle action bar item clicks here. The action bar will ? ? ? ? // automatically handle clicks on the Home/Up button, so long ? ? ? ? // as you specify a parent activity in AndroidManifest.xml. ? ? ? ? int id = item.getItemId(); ? ? ? ? ? //noinspection SimplifiableIfStatement ? ? ? ? if (id == R.id.action_settings) { ? ? ? ? ? ? return true; ? ? ? ? } ? ? ? ? ? return super.onOptionsItemSelected(item); ? ? } }
xml代碼:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:id="@+id/layout" ? ? xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" ? ? android:layout_height="match_parent" tools:context=".MainActivity"> ? ? ? <ImageView ? ? ? ? android:id="@+id/iv" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:src="@mipmap/a" ? ? ? ? android:layout_height="wrap_content" /> ? </RelativeLayout>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解析Android開(kāi)發(fā)中多點(diǎn)觸摸的實(shí)現(xiàn)方法
- android 多點(diǎn)觸摸圖片縮放的具體實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)手勢(shì)滑動(dòng)多點(diǎn)觸摸放大縮小圖片效果
- Android實(shí)現(xiàn)手勢(shì)滑動(dòng)多點(diǎn)觸摸縮放平移圖片效果
- Android實(shí)現(xiàn)手勢(shì)滑動(dòng)多點(diǎn)觸摸縮放平移圖片效果(二)
- Android實(shí)現(xiàn)手機(jī)多點(diǎn)觸摸畫(huà)圓
- Android檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)的方法
- Android實(shí)現(xiàn)檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)
- android實(shí)現(xiàn)多點(diǎn)觸摸效果
- android實(shí)現(xiàn)多點(diǎn)觸摸應(yīng)用
相關(guān)文章
Android采用雙緩沖技術(shù)實(shí)現(xiàn)畫(huà)板
這篇文章主要為大家詳細(xì)介紹了Android采用雙緩沖技術(shù)實(shí)現(xiàn)畫(huà)板的相關(guān)資料,思路清晰,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05Kotlin設(shè)計(jì)模式之委托模式使用方法詳解
Kotlin提供了兩個(gè)本機(jī)功能來(lái)實(shí)現(xiàn)委托模式,第一個(gè)是接口委托(例如策略模式),另一種是屬性委托,它專(zhuān)注于類(lèi)成員/屬性(例如延遲加載、observable等),它們共同提供了一組豐富而簡(jiǎn)潔的功能,通過(guò)本博客,您將了解在什么情況下使用此模式2023-09-09基于Android開(kāi)發(fā)支持表情的實(shí)現(xiàn)詳解
本篇文章是對(duì)在Android開(kāi)發(fā)中支持表情的實(shí)現(xiàn)代碼進(jìn)行了介紹。需要的朋友參考下2013-05-05Android中Service和Activity相互通信示例代碼
在android中Activity負(fù)責(zé)前臺(tái)界面展示,service負(fù)責(zé)后臺(tái)的需要長(zhǎng)期運(yùn)行的任務(wù)。下面這篇文章主要給大家介紹了關(guān)于Android中Service和Activity相互通信的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-09-09Android開(kāi)發(fā)手冊(cè)TextInputLayout樣式使用示例
這篇文章主要為大家介紹了Android開(kāi)發(fā)手冊(cè)TextInputLayout樣式使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06