最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)多點(diǎn)觸摸操作

 更新時(shí)間:2022年05月18日 09:40:57   作者:風(fēng)風(fēng)風(fēng)一樣  
這篇文章主要介紹了Android實(shí)現(xiàn)多點(diǎn)觸摸操作,實(shí)現(xiàn)圖片的放大、縮小和旋轉(zhuǎn)等處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Android中的多點(diǎn)觸摸可以實(shí)現(xiàn)圖片的放大、縮小和旋轉(zhuǎn)等處理,供大家參考,具體內(nèi)容如下

主要通過setOnTouchListener方法來監(jiān)聽用戶的觸摸事件,通過event.getX(0)和 event.getX(1)來獲取第一個(gè)觸控點(diǎn)和第二個(gè)觸控點(diǎn)的x軸(或者y軸)坐標(biāo),接下來在MotionEvent.ACTION_DOWN、MotionEvent.ACTION_MOVE、MotionEvent.ACTION_UP這幾種情況中來對獲取到的x軸或者y軸進(jìn)行處理,就能實(shí)現(xiàn)我們想要的效果了。

下面這個(gè)小Demo實(shí)現(xiàn)了對圖片的放大和縮小處理:

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) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //沒有縮放
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 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>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android自定義控件實(shí)現(xiàn)時(shí)鐘效果

    Android自定義控件實(shí)現(xiàn)時(shí)鐘效果

    這篇文章主要介紹了Android自定義控件實(shí)現(xiàn)時(shí)鐘效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • Android自定義view之圍棋動畫效果的實(shí)現(xiàn)

    Android自定義view之圍棋動畫效果的實(shí)現(xiàn)

    這篇文章主要介紹了Android自定義view之圍棋動畫效果的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • java萬年歷,獲取該年月日歷表

    java萬年歷,獲取該年月日歷表

    這篇文章主要介紹了java獲取對應(yīng)年月分日歷表有需要的朋友可以來參考下
    2015-07-07
  • Android App中自定義View視圖的實(shí)例教程

    Android App中自定義View視圖的實(shí)例教程

    這篇文章主要介紹了Android App中自定義View視圖的實(shí)例教程,詳細(xì)講解了如何在創(chuàng)建View中定義各種鎖需要的樣式屬性,需要的朋友可以參考下
    2016-04-04
  • android自定義窗口標(biāo)題示例分享

    android自定義窗口標(biāo)題示例分享

    這篇文章主要介紹了android自定義窗口標(biāo)題示例,需要的朋友可以參考下
    2014-03-03
  • android電源信息查看(電量、溫度、電壓)實(shí)例代碼

    android電源信息查看(電量、溫度、電壓)實(shí)例代碼

    這篇文章主要介紹了android電源信息查看方法,以實(shí)例形式較為詳細(xì)的分析了Android實(shí)現(xiàn)電源電量、電壓、溫度等信息查看的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • 一步步教你寫Slack的Loading動畫

    一步步教你寫Slack的Loading動畫

    這篇文章主要為大家詳細(xì)手摸手教你寫Slack的Loading動畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • flutter中build.gradle倉庫的配置(解決外網(wǎng)下載速度過慢失敗的問題)

    flutter中build.gradle倉庫的配置(解決外網(wǎng)下載速度過慢失敗的問題)

    這篇文章主要介紹了flutter中build.gradle倉庫的配置,解決外網(wǎng)下載速度過慢,失敗的問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • android商戶掃碼槍讀取手機(jī)二維碼

    android商戶掃碼槍讀取手機(jī)二維碼

    這篇文章主要為大家詳細(xì)介紹了android商戶掃碼槍讀取手機(jī)二維碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 一文講解Kotlin中的contract到底有什么用

    一文講解Kotlin中的contract到底有什么用

    我們在開發(fā)中肯定會經(jīng)常用Kotlin提供的一些通用拓展函數(shù),當(dāng)我們進(jìn)去看源碼的時(shí)候會發(fā)現(xiàn)許多函數(shù)里面有contract{}包裹的代碼塊,那么這些代碼塊到底有什么作用呢?下面這篇文章主要給大家介紹了關(guān)于Kotlin中contract到底有什么用的相關(guān)資料,需要的朋友可以參考下
    2022-01-01

最新評論

安阳市| 光泽县| 山丹县| 鹿泉市| 昆明市| 芜湖市| 台安县| 南召县| 永登县| 资兴市| 黑水县| 泽普县| 民县| 门源| 封丘县| 宜宾县| 昭苏县| 八宿县| 昌乐县| 阜阳市| 黎川县| 滁州市| 舟山市| 长沙县| 鄱阳县| 桃源县| 苏州市| 卓尼县| 通海县| 涪陵区| 敖汉旗| 阿巴嘎旗| 泉州市| 松潘县| 崇义县| 甘南县| 高台县| 广饶县| 金昌市| 无锡市| 谢通门县|