欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android通過單點(diǎn)觸摸移動(dòng)圖片

 更新時(shí)間:2022年04月23日 17:02:44   作者:doubleview  
這篇文章主要為大家詳細(xì)介紹了Android通過單點(diǎn)觸摸移動(dòng)圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android通過單點(diǎn)觸摸移動(dòng)圖片的具體代碼,供大家參考,具體內(nèi)容如下

編寫布局資源文件

先準(zhǔn)備一張圖片放入drawable內(nèi)

這里主要就是將圖片顯示出來并設(shè)置id(android:scaleType="fitXY"表示圖片按原比例設(shè)置大?。?/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是為了看移動(dòng)圖片時(shí)的數(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);
? ? ? ? //利用布局資源文件設(shè)置用戶界面
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? //通過資源標(biāo)識(shí)符獲取控件實(shí)例
? ? ? ? ivImages = findViewById(R.id.ivImages);
? ? ? ? root = findViewById(R.id.root);

? ? ? ? //設(shè)置根布局可以獲取焦點(diǎn)
? ? ? ? root.setFocusable(true);
? ? ? ? //讓布局獲取焦點(diǎn)
? ? ? ? root.requestFocus();


? ? ? ? //給根布局注冊(cè)完觸摸監(jiān)聽器,實(shí)現(xiàn)觸摸監(jiān)聽器接口,編寫觸摸事件代碼
? ? ? ? root.setOnTouchListener(new View.OnTouchListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onTouch(View view, MotionEvent event) {
? ? ? ? ? ? ? ? //根據(jù)觸摸動(dòng)作執(zhí)行不同的操作
? ? ? ? ? ? ? ? switch (event.getAction()) {
? ? ? ? ? ? ? ? ? ? case MotionEvent.ACTION_DOWN: ?//觸點(diǎn)按下
? ? ? ? ? ? ? ? ? ? ? ? Log.d(TAG, "ACTION_DOWN"+event.getX() + "," + event.getY());
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case MotionEvent.ACTION_MOVE: //觸點(diǎn)移動(dòng)
? ? ? ? ? ? ? ? ? ? ? ? Log.d(TAG, "ACTION_MOVE"+event.getX() + "," + event.getY());
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case MotionEvent.ACTION_UP: //觸點(diǎn)放開
? ? ? ? ? ? ? ? ? ? ? ? Log.d(TAG, "ACTION_UP"+event.getX() + "," + event.getY());
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //設(shè)置圖像控件坐標(biāo)
? ? ? ? ? ? ? ? ivImages.setX(event.getX()-ivImages.getWidth()/2);
? ? ? ? ? ? ? ? ivImages.setY(event.getY()-ivImages.getHeight()/2);
? ? ? ? ? ? ? ? return true;//設(shè)置為真,三個(gè)事件:down-->move-->up依次執(zhí)行
? ? ? ? ? ? }
? ? ? ? });
? ? }
}

效果

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

相關(guān)文章

最新評(píng)論