Android DragImageView實現(xiàn)下拉拖動圖片放大效果
DragImageView下拉拖動圖片放大,先上圖:
主要的類:繼承了RelativeLayout,再在RelativeLayout里面添加ImageView,通過Touch事件來改變ImageView的縮放,縮放時計算scale,使其在手指移動到屏幕底部時,圖片底部也剛好到達屏幕底部,手指松開時,圖片逐步回彈。
package com.example.dragimagescale; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.PointF; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.util.Log; import android.view.MotionEvent; import android.view.WindowManager; import android.widget.ImageView; import android.widget.ImageView.ScaleType; import android.widget.RelativeLayout; public class DragScaleImageView extends RelativeLayout { private String TAG = "DragScaleImageView"; private static final int BACK_SCALE = 1010; private Context mContext; private AttributeSet attrs; private int displayWidth = 0; private int displayHeight = 0; private int mImageId; private Bitmap bmp; private ImageView imageView; /** 是否處在回彈狀態(tài) */ private boolean isBacking = false; /** 用于記錄拖拉圖片移動的坐標位置 */ private Matrix matrix = new Matrix(); /** 用于記錄圖片要進行拖拉時候的坐標位置 */ private Matrix currentMatrix = new Matrix(); private Matrix defaultMatrix = new Matrix(); /** 圖片的寬高 */ private float imgHeight, imgWidth; /** 初始狀態(tài) */ private int mode = 0; /** 拖拉照片模式 */ private final int MODE_DRAG = 1; private float scaleY = 0; /** 用于記錄開始時候的坐標位置 */ private PointF startPoint = new PointF(); /** 用于記錄開始時候的在整個屏幕中的Y坐標位置 */ private float startRawY = 0; float scale = 1; private TouchEventListener touchEventListener = null; private BackScaleListener backScaleListener = null; public DragScaleImageView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub this.mContext = context; this.attrs = attrs; initView(); } public DragScaleImageView(Context context) { super(context); // TODO Auto-generated constructor stub this.mContext = context; initView(); } public DragScaleImageView(Activity activity, Bitmap resBitmap, int width, int height) { super(activity); } /** * 初始化圖片 */ private void initView() { /* 取得屏幕分辨率大小 */ DisplayMetrics dm = new DisplayMetrics(); WindowManager mWm = (WindowManager) mContext .getSystemService(Context.WINDOW_SERVICE); mWm.getDefaultDisplay().getMetrics(dm); displayWidth = dm.widthPixels; displayHeight = dm.heightPixels; TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.DragScaleImageView); mImageId = a.getResourceId(R.styleable.DragScaleImageView_scale_image, 0); a.recycle(); if (null == bmp && mImageId != 0) { bmp = BitmapFactory.decodeResource(getResources(), mImageId); float scale = (float) displayWidth / (float) bmp.getWidth();// 1080/1800 matrix.postScale(scale, scale, 0, 0); imgHeight = scale * bmp.getHeight(); imgWidth = scale * bmp.getWidth(); } else { imgHeight = displayWidth; imgWidth = displayWidth; } initImageView(); } private void initImageView() { imageView = new ImageView(mContext); imageView.setImageMatrix(matrix); defaultMatrix.set(matrix); Log.w(TAG, "imgWidth :" + imgWidth); Log.w(TAG, "imgHeight :" + imgHeight); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( (int) imgWidth, (int) imgHeight); imageView.setLayoutParams(layoutParams); imageView.setImageBitmap(bmp); imageView.setScaleType(ScaleType.CENTER_CROP); this.addView(imageView); } /** * 設(shè)置ImageView的寬高 * * @param width * @param height */ public void setImageWidthAndHeight(int width, int height) { imgWidth = width; imgHeight = height; RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( (int) imgWidth, (int) imgHeight); imageView.setLayoutParams(layoutParams); } public boolean onTouchEvent(MotionEvent event) { Log.w(TAG, "onTouchEvent :" + event.getAction()); // 當(dāng)該View放置在ScrollView里面時,會與父控件Touch事件沖突,所以touch該控件區(qū)域時,父控件不可用 if (event.getAction() == MotionEvent.ACTION_UP) { getParent().requestDisallowInterceptTouchEvent(false); } else { getParent().requestDisallowInterceptTouchEvent(true);// true表示父類的不可用; } switch (event.getAction() & MotionEvent.ACTION_MASK) { // 手指壓下屏幕 case MotionEvent.ACTION_DOWN: if (isBacking) { return super.onTouchEvent(event); } int[] location = new int[2]; imageView.getLocationInWindow(location); if (location[1] >= 0) { mode = MODE_DRAG; // 記錄ImageView當(dāng)前的移動位置 currentMatrix.set(imageView.getImageMatrix()); startPoint.set(event.getX(), event.getY()); startRawY = event.getRawY(); Log.w(TAG, "onTouchEvent startRawY:" + startRawY); } break; // 手指在屏幕上移動,改事件會被不斷觸發(fā) case MotionEvent.ACTION_MOVE: // 拖拉圖片 if (mode == MODE_DRAG) { // float dx = event.getX() - startPoint.x; // 得到x軸的移動距離 float dy = event.getY() - startPoint.y; // 得到y(tǒng)軸的移動距離 // 在沒有移動之前的位置上進行移動 if (dy > 0) { matrix.set(currentMatrix); Log.w(TAG, "onTouchEvent dy:" + dy); scale = ((dy / (displayHeight - startRawY) * (displayHeight - imgHeight)) + imgHeight) / imgHeight; // 得到縮放倍數(shù),當(dāng)手指移動到屏幕底部時,圖片也達到屏幕底部 Log.w(TAG, "onTouchEvent scale:" + scale); scaleY = dy; RelativeLayout.LayoutParams relativeLayout = new RelativeLayout.LayoutParams( (int) (scale * imgWidth), (int) (scale * imgHeight)); imageView.setLayoutParams(relativeLayout); matrix.postScale(scale, scale, imgWidth / 2, 0); imageView.setImageMatrix(matrix); } } break; // 手指離開屏幕 case MotionEvent.ACTION_UP: // 當(dāng)觸點離開屏幕,圖片還原 mHandler.sendEmptyMessage(BACK_SCALE); case MotionEvent.ACTION_POINTER_UP: // 當(dāng)兩個手指移動時,取消移動圖片 mode = 0; break; } // 設(shè)置的Touch監(jiān)聽事件 if (touchEventListener != null) { touchEventListener.onTouchEvent(event); } return true; } /** 逐步回彈 */ @SuppressLint("HandlerLeak") private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch (msg.what) { case BACK_SCALE: scale = (scaleY / 2 + imgHeight) / (imgHeight);// 得到縮放倍數(shù) if (scaleY > 0) { isBacking = true; matrix.set(currentMatrix); RelativeLayout.LayoutParams relativeLayout = new RelativeLayout.LayoutParams( (int) (scale * imgWidth), (int) (scale * imgHeight)); imageView.setLayoutParams(relativeLayout); matrix.postScale(scale, scale, imgWidth / 2, 0); imageView.setImageMatrix(matrix); scaleY = (float) (scaleY / 2 - 1); mHandler.sendEmptyMessageDelayed(BACK_SCALE, 20);// 逐步回彈 } else { scaleY = 0; RelativeLayout.LayoutParams relativeLayout = new RelativeLayout.LayoutParams( (int) imgWidth, (int) imgHeight); imageView.setLayoutParams(relativeLayout); matrix.set(defaultMatrix); imageView.setImageMatrix(matrix); isBacking = false; } if (backScaleListener != null) { backScaleListener.onBackScale(); } break; default: break; } super.handleMessage(msg); } }; public void setTouchEventListener(TouchEventListener touchEventListener) { this.touchEventListener = touchEventListener; } public void setBackScaleListener(BackScaleListener backScaleListener) { this.backScaleListener = backScaleListener; } /** Touch事件監(jiān)聽 */ public interface TouchEventListener { public void onTouchEvent(MotionEvent event); } /** 回彈事件監(jiān)聽 */ public interface BackScaleListener { public void onBackScale(); } }
調(diào)用的Activity:
package com.example.dragimagescale; import com.example.dragimagescale.DragScaleImageView.BackScaleListener; import com.example.dragimagescale.DragScaleImageView.TouchEventListener; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; public class MainActivity extends Activity { DragScaleImageView mDragScaleImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDragScaleImageView = (DragScaleImageView) findViewById(R.id.dragScaleImageView); /** 自定義ImageView的寬高,若不設(shè)置則按圖片寬高壓縮至屏幕寬度 */ // mDragScaleImageView.setImageWidthAndHeight(720, 300); // Touch事件監(jiān)聽 mDragScaleImageView.setTouchEventListener(new TouchEventListener() { @Override public void onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub // do something here } }); // 回彈事件監(jiān)聽 mDragScaleImageView.setBackScaleListener(new BackScaleListener() { @Override public void onBackScale() { // TODO Auto-generated method stub // do something here } }); } }
xml 布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:dragscaleimageview="http://schemas.android.com/apk/res/com.example.dragimagescale" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" > <com.example.dragimagescale.DragScaleImageView android:id="@+id/dragScaleImageView" android:layout_width="match_parent" android:layout_height="wrap_content" dragscaleimageview:scale_image="@drawable/image" > </com.example.dragimagescale.DragScaleImageView> </RelativeLayout>
下載:源碼
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實現(xiàn)ImageView圖片縮放和拖動
- Android實現(xiàn)跟隨手指拖動并自動貼邊的View樣式(實例demo)
- Android自定義View實現(xiàn)拖動選擇按鈕
- Android實現(xiàn)單頁面浮層可拖動view的一種方法
- Android通過自定義ImageView控件實現(xiàn)圖片的縮放和拖動的實現(xiàn)代碼
- Android開發(fā)實現(xiàn)可拖動排序的ListView功能【附源碼下載】
- Android RecyclerView滑動刪除和拖動排序
- Android ViewDragHelper仿淘寶拖動加載效果
- Android自定義View圓形和拖動圓、跟隨手指拖動效果
- android實現(xiàn)可拖動的浮動view
相關(guān)文章
Android使用listview實現(xiàn)分頁刷新(線程休眠模擬)
這篇文章主要為大家詳細介紹了Android使用listview實現(xiàn)分頁刷新,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11利用Jetpack Compose實現(xiàn)主題切換功能
這篇文章主要介紹了如何利用Android中的Jetpack Compose實現(xiàn)主題切換功能,文中的示例代碼講解詳細,對我們學(xué)習(xí)有一定幫助,需要的可以參考一下2022-01-01android 監(jiān)聽SD卡文件變化的實現(xiàn)代碼
這篇文章主要介紹了android 監(jiān)聽SD卡文件變化的實現(xiàn)代碼,需要的朋友可以參考下2017-11-11Android?Kotlin全面詳細類使用語法學(xué)習(xí)指南
這篇文章主要為大家介紹了Android?Kotlin全面詳細類使用語法學(xué)習(xí)指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06android?studio實現(xiàn)上傳圖片到j(luò)ava服務(wù)器
這篇文章主要為大家詳細介紹了android?studio實現(xiàn)上傳圖片到j(luò)ava服務(wù)器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08android中Bitmap用法(顯示,保存,縮放,旋轉(zhuǎn))實例分析
這篇文章主要介紹了android中Bitmap用法,以實例形式較為詳細的分析了android中Bitmap操作圖片的顯示、保存、縮放、旋轉(zhuǎn)等相關(guān)技巧,需要的朋友可以參考下2015-09-09Android中使用imageviewswitcher 實現(xiàn)圖片切換輪播導(dǎo)航的方法
ImageSwitcher是Android中控制圖片展示效果的一個控件。本文給大家介紹Android中使用imageviewswitcher 實現(xiàn)圖片切換輪播導(dǎo)航的方法,需要的朋友參考下吧2016-12-12Android Flutter實現(xiàn)視頻上滑翻頁效果的示例代碼
我們在短視頻應(yīng)用中經(jīng)常會看到不停上滑瀏覽下一條視頻的沉浸式交互效果,這種交互能夠讓用戶不停地翻頁,直到找到喜歡的視頻內(nèi)容。本文將通過Flutter中的PageView組件實現(xiàn),感興趣的可以了解一下2022-10-10