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

Android基礎(chǔ)知識之單點(diǎn)觸摸

 更新時(shí)間:2022年04月23日 17:03:59   作者:_江南一點(diǎn)雨  
這篇文章主要為大家詳細(xì)介紹了Android基礎(chǔ)知識之單點(diǎn)觸摸,很簡單的操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

相對于多點(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,一會我們將在Activity中移動這個(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://移動時(shí)
   //用另一個(gè)模型記住按下時(shí)的位置
   newMatrix.set(oldMatrix);
   //移動模型
   newMatrix.setTranslate(event.getX()-x, event.getY()-y);
   break;
  }
  //把圖片放入移動后的模型中
  iv.setImageMatrix(newMatrix);
  return true;
  }
 });
 }
}

就是這么簡單。

源碼下載:單點(diǎn)觸摸

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

相關(guān)文章

最新評論