Android實現(xiàn)圖片拖動效果
要求:
1.通過手指移動來拖動圖片
2.控制圖片不能超出屏幕顯示區(qū)域
技術(shù)點:
1.MotionEvent處理
2.對View進(jìn)行動態(tài)定位(layout)
activity_main.xml:
<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" > <ImageView android:id="@+id/iv_main" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/test"/> </RelativeLayout>
MainActivity:
public class MainActivity extends Activity implements OnTouchListener { private ImageView iv_main; private RelativeLayout parentView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv_main = (ImageView) findViewById(R.id.iv_main); parentView = (RelativeLayout) iv_main.getParent(); /* int right = parentView.getRight(); //0 int bottom = parentView.getBottom(); //0 Toast.makeText(this, right+"---"+bottom, 1).show(); */ //設(shè)置touch監(jiān)聽 iv_main.setOnTouchListener(this); } private int lastX; private int lastY; private int maxRight; private int maxBottom; @Override public boolean onTouch(View v, MotionEvent event) { //得到事件的坐標(biāo) int eventX = (int) event.getRawX(); int eventY = (int) event.getRawY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: //得到父視圖的right/bottom if(maxRight==0) {//保證只賦一次值 maxRight = parentView.getRight(); maxBottom = parentView.getBottom(); } //第一次記錄lastX/lastY lastX =eventX; lastY = eventY; break; case MotionEvent.ACTION_MOVE: //計算事件的偏移 int dx = eventX-lastX; int dy = eventY-lastY; //根據(jù)事件的偏移來移動imageView int left = iv_main.getLeft()+dx; int top = iv_main.getTop()+dy; int right = iv_main.getRight()+dx; int bottom = iv_main.getBottom()+dy; //限制left >=0 if(left<0) { right += -left; left = 0; } //限制top if(top<0) { bottom += -top; top = 0; } //限制right <=maxRight if(right>maxRight) { left -= right-maxRight; right = maxRight; } //限制bottom <=maxBottom if(bottom>maxBottom) { top -= bottom-maxBottom; bottom = maxBottom; } iv_main.layout(left, top, right, bottom); //再次記錄lastX/lastY lastX = eventX; lastY = eventY; break; default: break; } return true;//所有的motionEvent都交給imageView處理 } }
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
- android Matrix實現(xiàn)圖片隨意放大縮小或拖動
- Android實現(xiàn)ImageView圖片縮放和拖動
- Android編程實現(xiàn)圖片的瀏覽、縮放、拖動和自動居中效果
- Android如何創(chuàng)建可拖動的圖片控件
- Android通過自定義ImageView控件實現(xiàn)圖片的縮放和拖動的實現(xiàn)代碼
- Android RecyclerView多類型布局卡片解決方案
- Android實現(xiàn)簡單卡片布局
- Android控件CardView實現(xiàn)卡片布局
- Android編程重寫ViewGroup實現(xiàn)卡片布局的方法
- Android實現(xiàn)可拖動層疊卡片布局
相關(guān)文章
Android 組合控件實現(xiàn)布局的復(fù)用的方法
本篇文章主要介紹了Android 組合控件實現(xiàn)布局的復(fù)用的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08Android賬號注冊實現(xiàn)點擊獲取驗證碼倒計時效果
這篇文章主要為大家詳細(xì)介紹了Android賬號注冊過程中實現(xiàn)點擊獲取驗證碼倒計時效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05Android實現(xiàn)圖像灰度化、線性灰度變化和二值化處理方法
這篇文章主要介紹了Android實現(xiàn)圖像灰度化、線性灰度變化和二值化處理方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10android中在Activity中響應(yīng)ListView內(nèi)部按鈕的點擊事件的兩種方法
本篇文章主要介紹了android中在Activity中響應(yīng)ListView內(nèi)部按鈕的點擊事件的兩種方法,有需要的可以了解一下。2016-11-11Android viewpager在最后一頁滑動之后跳轉(zhuǎn)到主頁面的實例代碼
這篇文章主要介紹了Android viewpager在最后一頁滑動之后跳轉(zhuǎn)到主頁面的實例代碼的相關(guān)資料,需要的朋友可以參考下2016-08-08