Android實(shí)現(xiàn)view拖動(dòng)到任意位置
本文實(shí)現(xiàn):將圖片任意拖動(dòng),如果拖動(dòng)到正確位置則成功,若抬起手時(shí)時(shí)錯(cuò)誤位置則自動(dòng)回到原位。
定義
private ImageView img; private ImageView imageView; //容器的寬高,需要在屏幕繪制好之后才能獲取 private int containerWidth; private int containerHeight; private float lastX, lastY; //獲取需要拖動(dòng)到的正確位置 private int[] imgPosition = new int[2]; private int height; private int width;
在屏幕繪制好之后獲取寬高和位置
@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); // 這里來(lái)獲取容器的寬和高 if (hasFocus) { containerHeight = sunPage.getHeight(); containerWidth = sunPage.getWidth(); } //獲取需要拖動(dòng)到的正確位置,img處 img.getLocationInWindow(imgPosition); height = img.getMeasuredHeight(); width = img.getMeasuredWidth(); }
手勢(shì)事件
imageView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: //手指在屏幕位置getRawX() getRawY() lastX = event.getRawX(); lastY = event.getRawY(); break; case MotionEvent.ACTION_MOVE: // 不要直接用getX和getY,這兩個(gè)獲取的數(shù)據(jù)已經(jīng)是經(jīng)過處理的,容易出現(xiàn)圖片抖動(dòng)的情況 float distanceX = xx - event.getRawX(); float distanceY = yy - event.getRawY(); float nextY = imageView.getY() - distanceY; float nextX = imageView.getX() - distanceX; // 不能移出屏幕 if (nextY < 0) { nextY = 0; } else if (nextY > containerHeight - imageView.getHeight()) { nextY = containerHeight - imageView.getHeight(); } if (nextX < 0) nextX = 0; else if (nextX > containerWidth - imageView.getWidth()) nextX = containerWidth - imageView.getWidth(); // 屬性動(dòng)畫移動(dòng) ObjectAnimator y = ObjectAnimator.ofFloat(imageView, "y", imageView.getY(), nextY); ObjectAnimator x = ObjectAnimator.ofFloat(imageView, "x", imageView.getX(), nextX); animatorSet.playTogether(x, y); animatorSet.setDuration(0); animatorSet.start(); lastX = event.getRawX(); lastY = event.getRawY(); if (correct(lastX, lastY, imgPosition, m)) { //正確后的事情 } break; case MotionEvent.ACTION_UP: //抬起手后自動(dòng)彈回原處,監(jiān)聽animatorSet animatorSet.cancel(); break; } return true; } });
判斷拖動(dòng)是否正確,m是img的半徑,拖動(dòng)結(jié)合的難易可以自己調(diào)劑算法
//判斷拖動(dòng)是否正確,m是img的半徑,拖動(dòng)結(jié)合的難易可以自己調(diào)劑算法 private boolean correct(float x, float y, int[] a, int m) { float s = (float) Math.sqrt((x - a[0]) * (x - a[0]) + (y - a[1]) * (y - a[1]) - (x - a[0]) * (y - a[1])); if (s <= ActivityUtils.dip2px(HomeGameActivity.this, m)) { return true; } return false; }
監(jiān)聽animatorSet,cancle時(shí)imageview返回原處
animatorSet.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { } @Override public void onAnimationCancel(Animator animation) { imageView.setTranslationY(0); imageView.setTranslationX(0); } @Override public void onAnimationRepeat(Animator animation) { } });
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)雙向滑動(dòng)選擇器范圍SeekBar實(shí)現(xiàn)
這篇文章主要為大家介紹了Android開發(fā)雙向滑動(dòng)范圍選擇器SeekBar實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06android Bitmap圓角與倒影的具體實(shí)現(xiàn)代碼
android Bitmap圓角與倒影的具體實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-06-06Android自定義控件之創(chuàng)建可復(fù)用的組合控件
這篇文章主要為大家詳細(xì)介紹了Android自定義控件之創(chuàng)建可復(fù)用的組合控件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Android Internet應(yīng)用實(shí)現(xiàn)獲取天氣預(yù)報(bào)的示例代碼
這篇文章主要介紹了Android網(wǎng)絡(luò)編程及Internet應(yīng)用-獲取天氣,小編覺得挺不錯(cuò)的,一起跟隨小編過來(lái)看看吧2018-05-05Android開發(fā)之獲取SD卡及手機(jī)ROM容量的方法
這篇文章主要介紹了Android開發(fā)之獲取SD卡及手機(jī)ROM容量的方法,結(jié)合實(shí)例形式分析了Android針對(duì)SD卡的讀取及屬性操作相關(guān)技巧,需要的朋友可以參考下2016-04-04Android實(shí)現(xiàn)Service下載文件,Notification顯示下載進(jìn)度的示例
本篇文章主要介紹了Android實(shí)現(xiàn)Service下載文件,Notification顯示下載進(jìn)度,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01深入android Unable to resolve target ''android-XX''詳解
本篇文章是對(duì)android Unable to resolve target 'android-XX'錯(cuò)誤的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06android實(shí)現(xiàn)音樂播放器進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)音樂播放器進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04AndroidStudio升級(jí)4.1后啟動(dòng)失敗Plugin問題解決
這篇文章主要介紹了AndroidStudio升級(jí)4.1后啟動(dòng)失敗Plugin問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10