Android基礎(chǔ)知識(shí)之單點(diǎn)觸摸
相對(duì)于多點(diǎn)觸摸,單點(diǎn)觸摸還是很簡單的。
新建一個(gè)工程,先看看布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.touchevent.MainActivity" > <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/jiafeimao" android:scaleType="matrix" /> </RelativeLayout>
就一個(gè)簡單的ImageView,一會(huì)我們將在Activity中移動(dòng)這個(gè)ImageView:
public class MainActivity extends Activity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) this.findViewById(R.id.iv);
iv.setOnTouchListener(new OnTouchListener() {
private float x;
private float y;
// 用來操作圖片的模型
private Matrix oldMatrix = new Matrix();
private Matrix newMatrix = new Matrix();
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) { // 判斷操作類型
case MotionEvent.ACTION_DOWN:
//按下時(shí)記住x,y的坐標(biāo)
x = event.getX();
y = event.getY();
oldMatrix.set(iv.getImageMatrix());
break;
case MotionEvent.ACTION_MOVE://移動(dòng)時(shí)
//用另一個(gè)模型記住按下時(shí)的位置
newMatrix.set(oldMatrix);
//移動(dòng)模型
newMatrix.setTranslate(event.getX()-x, event.getY()-y);
break;
}
//把圖片放入移動(dòng)后的模型中
iv.setImageMatrix(newMatrix);
return true;
}
});
}
}
就是這么簡單。
源碼下載:單點(diǎn)觸摸
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android中的SQL查詢語句LIKE綁定參數(shù)問題解決辦法(sqlite數(shù)據(jù)庫)
這篇文章主要介紹了Android中的SQL查詢語句LIKE綁定參數(shù)問題解決辦法,本文使用的是sqlite數(shù)據(jù)庫,需要的朋友可以參考下2014-06-06
Android StickListView實(shí)現(xiàn)懸停效果
這篇文章主要介紹了Android StickListView實(shí)現(xiàn)懸停效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
Android創(chuàng)建外部lib庫及自定義View的圖文教程
這篇文章主要給大家介紹了關(guān)于Android創(chuàng)建外部lib庫及自定義View的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
Android使用URLConnection提交請(qǐng)求的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android使用URLConnection提交請(qǐng)求的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android編程之ListPreference用法實(shí)例分析
這篇文章主要介紹了Android編程之ListPreference用法,結(jié)合實(shí)例形式較為詳細(xì)的分析說明了ListPreference的功能、用法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2015-12-12
Android View源碼解讀 DecorView與ViewRootImpl淺談
這篇文章主要解讀了Android View源碼,為大家詳細(xì)介紹DecorView與ViewRootImpl,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android 使用Vitamio打造自己的萬能播放器(1)——準(zhǔn)備
本文主要介紹Android Vitamio,在Android開發(fā)視頻播放器的時(shí)候,大家經(jīng)常會(huì)遇到系統(tǒng)版本和不同的Android手機(jī)不同導(dǎo)致開發(fā)的軟件不能完美適用,這里給大家介紹個(gè)播放器插件可以適應(yīng)所有Android設(shè)備2016-07-07
flutter 屏幕尺寸適配和字體大小適配的實(shí)現(xiàn)
這篇文章主要介紹了flutter 屏幕尺寸適配和字體大小適配的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Android BottomNavigationBar底部導(dǎo)航控制器使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android BottomNavigationBar底部導(dǎo)航控制器使用方法,感興趣的小伙伴們可以參考一下2016-03-03

