Android DragImageView實(shí)現(xiàn)下拉拖動(dòng)圖片放大效果
DragImageView下拉拖動(dòng)圖片放大,先上圖:

主要的類:繼承了RelativeLayout,再在RelativeLayout里面添加ImageView,通過(guò)Touch事件來(lái)改變ImageView的縮放,縮放時(shí)計(jì)算scale,使其在手指移動(dòng)到屏幕底部時(shí),圖片底部也剛好到達(dá)屏幕底部,手指松開時(shí),圖片逐步回彈。
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;
/** 用于記錄拖拉圖片移動(dòng)的坐標(biāo)位置 */
private Matrix matrix = new Matrix();
/** 用于記錄圖片要進(jìn)行拖拉時(shí)候的坐標(biāo)位置 */
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;
/** 用于記錄開始時(shí)候的坐標(biāo)位置 */
private PointF startPoint = new PointF();
/** 用于記錄開始時(shí)候的在整個(gè)屏幕中的Y坐標(biāo)位置 */
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里面時(shí),會(huì)與父控件Touch事件沖突,所以touch該控件區(qū)域時(shí),父控件不可用
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)前的移動(dòng)位置
currentMatrix.set(imageView.getImageMatrix());
startPoint.set(event.getX(), event.getY());
startRawY = event.getRawY();
Log.w(TAG, "onTouchEvent startRawY:" + startRawY);
}
break;
// 手指在屏幕上移動(dòng),改事件會(huì)被不斷觸發(fā)
case MotionEvent.ACTION_MOVE:
// 拖拉圖片
if (mode == MODE_DRAG) {
// float dx = event.getX() - startPoint.x; // 得到x軸的移動(dòng)距離
float dy = event.getY() - startPoint.y; // 得到y(tǒng)軸的移動(dòng)距離
// 在沒(méi)有移動(dòng)之前的位置上進(jìn)行移動(dòng)
if (dy > 0) {
matrix.set(currentMatrix);
Log.w(TAG, "onTouchEvent dy:" + dy);
scale = ((dy / (displayHeight - startRawY) * (displayHeight - imgHeight)) + imgHeight)
/ imgHeight; // 得到縮放倍數(shù),當(dāng)手指移動(dòng)到屏幕底部時(shí),圖片也達(dá)到屏幕底部
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)觸點(diǎn)離開屏幕,圖片還原
mHandler.sendEmptyMessage(BACK_SCALE);
case MotionEvent.ACTION_POINTER_UP:
// 當(dāng)兩個(gè)手指移動(dòng)時(shí),取消移動(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>
下載:源碼
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)ImageView圖片縮放和拖動(dòng)
- Android實(shí)現(xiàn)跟隨手指拖動(dòng)并自動(dòng)貼邊的View樣式(實(shí)例demo)
- Android自定義View實(shí)現(xiàn)拖動(dòng)選擇按鈕
- Android實(shí)現(xiàn)單頁(yè)面浮層可拖動(dòng)view的一種方法
- Android通過(guò)自定義ImageView控件實(shí)現(xiàn)圖片的縮放和拖動(dòng)的實(shí)現(xiàn)代碼
- Android開發(fā)實(shí)現(xiàn)可拖動(dòng)排序的ListView功能【附源碼下載】
- Android RecyclerView滑動(dòng)刪除和拖動(dòng)排序
- Android ViewDragHelper仿淘寶拖動(dòng)加載效果
- Android自定義View圓形和拖動(dòng)圓、跟隨手指拖動(dòng)效果
- android實(shí)現(xiàn)可拖動(dòng)的浮動(dòng)view
相關(guān)文章
Android CardView詳解及使用方法和實(shí)例
這篇文章主要介紹了Android CardView詳解及使用方法和實(shí)例的相關(guān)資料,這里附有實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-12-12
PullToRefreshListView實(shí)現(xiàn)多條目加載上拉刷新和下拉加載
這篇文章主要為大家詳細(xì)介紹了PullToRefreshListView實(shí)現(xiàn)多條目加載上拉刷新和下拉加載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Android開發(fā)之組件GridView簡(jiǎn)單使用方法示例
這篇文章主要介紹了Android開發(fā)之組件GridView簡(jiǎn)單使用方法,涉及Android GridView組件圖片瀏覽及保存圖片等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
Android程序開發(fā)之使用Design包實(shí)現(xiàn)QQ動(dòng)畫側(cè)滑效果和滑動(dòng)菜單導(dǎo)航
這篇文章主要介紹了Android程序開發(fā)之使用Design包實(shí)現(xiàn)QQ動(dòng)畫側(cè)滑效果和滑動(dòng)菜單導(dǎo)航的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
詳解Android Studio無(wú)法檢測(cè)新版本問(wèn)題解決
這篇文章主要介紹了詳解Android Studio無(wú)法檢測(cè)新版本問(wèn)題解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
深入理解與運(yùn)行Android Jetpack組件之ViewModel
ViewModel是Android Jetpack組件之一,是一種用于管理UI相關(guān)數(shù)據(jù)的架構(gòu)組件,它能夠幫助開發(fā)者實(shí)現(xiàn)優(yōu)雅的數(shù)據(jù)驅(qū)動(dòng)和生命周期管理,本文將深入淺出地介紹ViewModel的使用和原理,帶你一步步掌握這個(gè)強(qiáng)大的組件2023-08-08
android操作SQLite增刪改減實(shí)現(xiàn)代碼
android操作SQLite增刪改減實(shí)現(xiàn)代碼,學(xué)習(xí)android的朋友可以參考下。2010-11-11
Android實(shí)現(xiàn)關(guān)機(jī)與重啟的幾種方式(推薦)
這篇文章主要介紹了Android實(shí)現(xiàn)關(guān)機(jī)與重啟的幾種方式(推薦)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07

