Android通過單點觸摸移動圖片
更新時間:2022年04月23日 17:02:44 作者:doubleview
這篇文章主要為大家詳細介紹了Android通過單點觸摸移動圖片,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android通過單點觸摸移動圖片的具體代碼,供大家參考,具體內(nèi)容如下
編寫布局資源文件
先準備一張圖片放入drawable內(nèi)
這里主要就是將圖片顯示出來并設置id(android:scaleType="fitXY"表示圖片按原比例設置大?。?/p>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:id="@+id/root" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:background="@drawable/bk019" ? ? android:gravity="center" ? ? android:orientation="vertical" ? ? tools:context=".MainActivity"> ? ? <ImageView ? ? ? ? android:id="@+id/ivImages" ? ? ? ? android:layout_width="100dp" ? ? ? ? android:layout_height="120dp" ? ? ? ? android:scaleType="fitXY" ? ? ? ? android:src="@drawable/bk031" /> </LinearLayout>
編寫主布局文件
(tag是為了看移動圖片時的數(shù)據(jù))
import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { ? ? private static final String TAG = "move_images_by_touch"; ? ? private ImageView ivImages; ? ? private LinearLayout root; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? //利用布局資源文件設置用戶界面 ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? //通過資源標識符獲取控件實例 ? ? ? ? ivImages = findViewById(R.id.ivImages); ? ? ? ? root = findViewById(R.id.root); ? ? ? ? //設置根布局可以獲取焦點 ? ? ? ? root.setFocusable(true); ? ? ? ? //讓布局獲取焦點 ? ? ? ? root.requestFocus(); ? ? ? ? //給根布局注冊完觸摸監(jiān)聽器,實現(xiàn)觸摸監(jiān)聽器接口,編寫觸摸事件代碼 ? ? ? ? root.setOnTouchListener(new View.OnTouchListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public boolean onTouch(View view, MotionEvent event) { ? ? ? ? ? ? ? ? //根據(jù)觸摸動作執(zhí)行不同的操作 ? ? ? ? ? ? ? ? switch (event.getAction()) { ? ? ? ? ? ? ? ? ? ? case MotionEvent.ACTION_DOWN: ?//觸點按下 ? ? ? ? ? ? ? ? ? ? ? ? Log.d(TAG, "ACTION_DOWN"+event.getX() + "," + event.getY()); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case MotionEvent.ACTION_MOVE: //觸點移動 ? ? ? ? ? ? ? ? ? ? ? ? Log.d(TAG, "ACTION_MOVE"+event.getX() + "," + event.getY()); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case MotionEvent.ACTION_UP: //觸點放開 ? ? ? ? ? ? ? ? ? ? ? ? Log.d(TAG, "ACTION_UP"+event.getX() + "," + event.getY()); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //設置圖像控件坐標 ? ? ? ? ? ? ? ? ivImages.setX(event.getX()-ivImages.getWidth()/2); ? ? ? ? ? ? ? ? ivImages.setY(event.getY()-ivImages.getHeight()/2); ? ? ? ? ? ? ? ? return true;//設置為真,三個事件:down-->move-->up依次執(zhí)行 ? ? ? ? ? ? } ? ? ? ? }); ? ? } }
效果
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Flutter 封裝一個 Banner 輪播圖效果的實例代碼
這篇文章主要介紹了Flutter 封裝一個 Banner 輪播圖效果,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07Android使用KeyStore對數(shù)據(jù)進行加密的示例代碼
這篇文章主要介紹了Android使用KeyStore對數(shù)據(jù)進行加密的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01