您好,登錄后才能下訂單哦!
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); } /** * 設置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()); // 當該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當前的移動位置 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軸的移動距離 // 在沒有移動之前的位置上進行移動 if (dy > 0) { matrix.set(currentMatrix); Log.w(TAG, "onTouchEvent dy:" + dy); scale = ((dy / (displayHeight - startRawY) * (displayHeight - imgHeight)) + imgHeight) / imgHeight; // 得到縮放倍數,當手指移動到屏幕底部時,圖片也達到屏幕底部 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: // 當觸點離開屏幕,圖片還原 mHandler.sendEmptyMessage(BACK_SCALE); case MotionEvent.ACTION_POINTER_UP: // 當兩個手指移動時,取消移動圖片 mode = 0; break; } // 設置的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);// 得到縮放倍數 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(); } }
調用的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的寬高,若不設置則按圖片寬高壓縮至屏幕寬度 */ // 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>
下載:源碼
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。