網站首頁 編程語言 正文
Android中的多點觸摸可以實現圖片的放大、縮小和旋轉等處理,供大家參考,具體內容如下
主要通過setOnTouchListener方法來監聽用戶的觸摸事件,通過event.getX(0)和 event.getX(1)來獲取第一個觸控點和第二個觸控點的x軸(或者y軸)坐標,接下來在MotionEvent.ACTION_DOWN、MotionEvent.ACTION_MOVE、MotionEvent.ACTION_UP這幾種情況中來對獲取到的x軸或者y軸進行處理,就能實現我們想要的效果了。
下面這個小Demo實現了對圖片的放大和縮小處理:
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:
? ? ? ? ? ? ? ? ? ? ? ? //判斷幾個觸控點
? ? ? ? ? ? ? ? ? ? ? ? if (event.getPointerCount() >= 2) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? //兩點之間x的坐標差
? ? ? ? ? ? ? ? ? ? ? ? ? ? float offsetX = event.getX(0) - event.getX(1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? //兩點之間y的坐標差
? ? ? ? ? ? ? ? ? ? ? ? ? ? float offsetY = event.getY(0) - event.getY(1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? //兩點之間的距離
? ? ? ? ? ? ? ? ? ? ? ? ? ? currentDistance = (float) Math.sqrt(offsetX * offsetX + offsetY * offsetY);
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (lastDistance < 0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //沒有縮放
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 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>
原文鏈接:https://blog.csdn.net/GEM_yaorao/article/details/49127561
相關推薦
- 2023-06-18 C#?整數轉二進制字符串方式_C#教程
- 2022-10-15 Nginx如何配置加密證書訪問實現_nginx
- 2022-09-23 flutter實現切換頁面緩存_Android
- 2022-05-15 Python?matplotlib?seaborn繪圖教程詳解_python
- 2023-06-20 Python實用技巧之臨時文件的妙用_python
- 2022-04-24 Redis?整數集合的具體使用(intset)_Redis
- 2023-10-12 form組件的封裝(element ui ) 簡單版本
- 2022-06-02 關于Python使用turtle庫畫任意圖的問題_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支