网上整理的自定义ImageView控件,因为对Android的内核不是太了解,,,做出来的这个还有点小问题,,,不过可以用;唉,很多方法的执行都跟我想的不太一样啊,郁闷,,,在这里备忘了。
ZoomImageView类
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.widget.ImageView;
/**
* 手势缩放
*
* @Title: ZoomImageView.java
* @Depict: TODO
* @Author: objectX
* @Data: 2014-3-6
* @Version V1.0
*
*/
public class ZoomImageView extends ImageView {
private Activity mActivity;
private int screen_W, screen_H;// 屏幕宽高
private int bitmap_W, bitmap_H;// 图片宽高
private int MAX_W, MAX_H, MIN_W, MIN_H;// 图片可以的最大最小宽高
private int current_Top, current_Right, current_Bottom, current_Left;// 图片要缩放的各个坐标
private int start_Top = -1, start_Right = -1, start_Bottom = -1, start_Left = -1;// 开始图片的位置离边框的各个距离
private int start_x, start_y, current_x, current_y;
private float beforeLenght, afterLenght;// beforeLenght之前的长度
// afterLenght之后的长度 ——两指之间的距离
private int before = 0;
private int after = 0;
private float scale_temp;// 缩放的比例
private enum MODE {
NONE, DRAG, ZOOM
};
private MODE mode = MODE.NONE;
private boolean isControl_V = false;// 垂直监控
private boolean isControl_H = false;// 水平监控
private boolean isScaleAnim = false;// 是否执行回缩放
private MyAsyncTask myAsyncTask;
private MyImageViewPager viewPager;
public ZoomImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public void setViewPager(MyImageViewPager viewPager) {
this.viewPager = viewPager;
}
public boolean isControl_H() {
return isControl_H;
}
public void setmActivity(Activity mActivity) {
this.mActivity = mActivity;
}
public void setScreen_W(int screen_W) {
this.screen_W = screen_W;
}
public void setScreen_H(int screen_H) {
this.screen_H = screen_H;
}
public ZoomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/***
* 设置显示图片
*/
@Override
public void setImageBitmap(Bitmap bm) {
super.setImageBitmap(bm);
/** 获取图片宽高 **/
bitmap_W = bm.getWidth();
bitmap_H = bm.getHeight();
MAX_W = bitmap_W * 3;
MAX_H = bitmap_H * 3;
MIN_W = bitmap_W / 2;
MIN_H = bitmap_H / 2;
after++;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (start_Top == -1 || before != after) {
// before!=after: 为了确保初始化的l,t,r,b的值的正确性,因为第三方图片插件第一次插入的图片并不是我们要显示的图片
before = after;
start_Top = top;
start_Left = left;
start_Bottom = bottom;
start_Right = right;
}
// 返回时重载数据
if (viewPager != null) {
viewPager.setZoomImageView(this);
}
if (isControl_H) {
if (left >= 0) {
viewPager.setPagerLeft(true);// viewpager可以左滑动
} else {
viewPager.setPagerLeft(false);
}
if (right <= screen_W) {
viewPager.setPagerRight(true);// viewpager可以右滑动
} else {
viewPager.setPagerRight(false);
}
} else {
viewPager.setPagerRight(true);// viewpager可以右滑动
viewPager.setPagerLeft(true);// viewpager可以左滑动
}
}
/***
* touch 事件
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
/** 处理单点、多点触摸 **/
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
onTouchDown(event);
break;
// 多点触摸
case MotionEvent.ACTION_POINTER_DOWN:
onPointerDown(event);
break;
case MotionEvent.ACTION_MOVE:
// 拖动
onTouchMove(event);
break;
case MotionEvent.ACTION_UP:
mode = MODE.NONE;
break;
// 多点松开
case MotionEvent.ACTION_POINTER_UP:
mode = MODE.NONE;
/** 执行缩放还原 **/
if (isScaleAnim) {
doScaleAnim();
}
break;
}
return true;
}
public void doScaleAnim() {
myAsyncTask = new MyAsyncTask(screen_W, this.getWidth(), this.getHeight());
myAsyncTask.setLTRB(this.getLeft(), this.getTop(), this.getRight(), this.getBottom());
myAsyncTask.execute();
isScaleAnim = false;
}
/** 按下,可能会执行拖动 **/
void onTouchDown(MotionEvent event) {
mode = MODE.DRAG;
// 相对于屏幕位置坐标
current_x = (int) event.getRawX();
current_y = (int) event.getRawY();
// 相对于容器位置坐标
start_x = (int) event.getX();
start_y = current_y - this.getTop();
}
/** 两个手指 只能放大缩小 **/
void onPointerDown(MotionEvent event) {
if (event.getPointerCount() == 2) {
mode = MODE.ZOOM;
beforeLenght = getDistance(event);// 获取两点开始的距离
}
}
float getDistance(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
/** 移动的处理 **/
void onTouchMove(MotionEvent event) {
int left = 0, top = 0, right = 0, bottom = 0;
/** 处理拖动 **/
if (mode == MODE.DRAG) {
/** 在这里要进行判断处理,防止在drag时候越界 **/
/** 获取相应的l,t,r ,b **/
left = current_x - start_x;
right = current_x + this.getWidth() - start_x;
top = current_y - start_y;
bottom = current_y - start_y + this.getHeight();
direction();
/** 水平进行判断 **/
if (isControl_H) {
if (left >= 0) {
left = 0;
right = this.getWidth();
}
if (right <= screen_W) {
left = screen_W - this.getWidth();
right = screen_W;
}
} else {
left = this.getLeft();
right = this.getRight();
}
/** 垂直判断 **/
if (isControl_V) {
if (top >= 0) {
top = 0;
bottom = this.getHeight();
}
if (bottom <= screen_H) {
top = screen_H - this.getHeight();
bottom = screen_H;
}
} else {
top = this.getTop();
bottom = this.getBottom();
}
if (isControl_H || isControl_V) this.setPosition(left, top, right, bottom);
current_x = (int) event.getRawX();
current_y = (int) event.getRawY();
}
/** 处理缩放 **/
else if (mode == MODE.ZOOM) {
afterLenght = getDistance(event);// 获取两点的距离
float gapLenght = afterLenght - beforeLenght;// 变化的长度
if (Math.abs(gapLenght) > 5f) {
scale_temp = afterLenght / beforeLenght;// 求的缩放的比例
this.setScale(scale_temp);
beforeLenght = afterLenght;
}
}
}
public void direction() {
if (current_x > start_x) {
// 手指向右滑
viewPager.setRight(true);
viewPager.setLeft(false);
} else {
// 手指向左滑
viewPager.setRight(false);
viewPager.setLeft(true);
}
}
private void setPosition(int left, int top, int right, int bottom) {
this.layout(left, top, right, bottom);
}
/** 处理缩放 **/
void setScale(float scale) {
int disX = (int) (this.getWidth() * Math.abs(1 - scale)) / 4;// 获取缩放水平距离
int disY = (int) (this.getHeight() * Math.abs(1 - scale)) / 4;// 获取缩放垂直距离
// 放大
if (scale > 1 && this.getWidth() <= MAX_W) {
current_Left = this.getLeft() - disX;
current_Top = this.getTop() - disY;
current_Right = this.getRight() + disX;
current_Bottom = this.getBottom() + disY;
this.setFrame(current_Left, current_Top, current_Right, current_Bottom);
/***
* 此时因为考虑到对称,所以只做一遍判断就可以了。
*/
if (current_Top <= 0 && current_Bottom >= screen_H) {
isControl_V = true;// 开启垂直监控
} else {
isControl_V = false;
}
if (current_Left <= 0 && current_Right >= screen_W) {
isControl_H = true;// 开启水平监控
} else {
isControl_H = false;
}
}
// 缩小
else if (scale < 1 && this.getWidth() >= MIN_W) {
current_Left = this.getLeft() + disX;
current_Top = this.getTop() + disY;
current_Right = this.getRight() - disX;
current_Bottom = this.getBottom() - disY;
/***
* 在这里要进行缩放处理
*/
// 上边越界
if (isControl_V && current_Top > 0) {
current_Top = 0;
current_Bottom = this.getBottom() - 2 * disY;
if (current_Bottom < screen_H) {
current_Bottom = screen_H;
isControl_V = false;// 关闭垂直监听
}
}
// 下边越界
if (isControl_V && current_Bottom < screen_H) {
current_Bottom = screen_H;
current_Top = this.getTop() + 2 * disY;
if (current_Top > 0) {
current_Top = 0;
isControl_V = false;// 关闭垂直监听
}
}
// 左边越界
if (isControl_H && current_Left >= 0) {
current_Left = 0;
current_Right = this.getRight() - 2 * disX;
if (current_Right <= screen_W) {
current_Right = screen_W;
isControl_H = false;// 关闭
viewPager.setPagerRight(true);// viewpager可以右滑动
viewPager.setPagerLeft(true);// viewpager可以左滑动
}
}
// 右边越界
if (isControl_H && current_Right <= screen_W) {
current_Right = screen_W;
current_Left = this.getLeft() + 2 * disX;
if (current_Left >= 0) {
current_Left = 0;
isControl_H = false;// 关闭
viewPager.setPagerRight(true);// viewpager可以右滑动
viewPager.setPagerLeft(true);// viewpager可以左滑动
}
}
if(!isControl_H){
viewPager.setPagerRight(true);// viewpager可以右滑动
viewPager.setPagerLeft(true);// viewpager可以左滑动
}
if (isControl_H || isControl_V) {
this.setFrame(current_Left, current_Top, current_Right, current_Bottom);
} else {
this.setFrame(current_Left, current_Top, current_Right, current_Bottom);
isScaleAnim = true;// 开启缩放动画
}
}
}
/**
* 还原image
*/
public void initImage() {
this.setFrame(start_Left, start_Top, start_Right, start_Bottom);
// 复原关闭监控
isControl_V = false;
isControl_H = false;
}
/***
* 回缩动画執行
*/
class MyAsyncTask extends AsyncTask<Void, Integer, Void> {
private int screen_W, current_Width, current_Height;
private int left, top, right, bottom;
private float scale_WH;// 宽高的比例
/** 当前的位置属性 **/
public void setLTRB(int left, int top, int right, int bottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
private float STEP = 5f;// 步伐
private float step_H, step_V;// 水平步伐,垂直步伐
public MyAsyncTask(int screen_W, int current_Width, int current_Height) {
super();
this.screen_W = screen_W;
this.current_Width = current_Width;
this.current_Height = current_Height;
scale_WH = (float) current_Height / current_Width;
step_H = STEP;
step_V = scale_WH * STEP;
}
@Override
protected Void doInBackground(Void... params) {
while (current_Width <= screen_W) {
left -= step_H;
top -= step_V;
right += step_H;
bottom += step_V;
current_Width += 2 * step_H;
left = Math.max(left, start_Left);
top = Math.max(top, start_Top);
right = Math.min(right, start_Right);
bottom = Math.min(bottom, start_Bottom);
// 有可能current_Width>screen_W时,bottom取得的值小于start_Bottom,step_V为浮点型转成int类型时会有些差异
if (left == start_Left) {
bottom = start_Bottom;
top = start_Top;
}
onProgressUpdate(new Integer[] { left, top, right, bottom });
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(final Integer... values) {
super.onProgressUpdate(values);
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
setFrame(values[0], values[1], values[2], values[3]);
}
});
}
}
}
MyImageViewPager类
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
public class MyImageViewPager extends ViewPager {
public MyImageViewPager(Context context) {
super(context);
}
public MyImageViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
private ZoomImageView zoomImageView;
private boolean isLeft = false;// 是否手指向左右滑动
private boolean isRight = false;// 是否手指向右左滑动
private boolean isPagerLeft = true;// 是否可以执行viewPager的左滑动
private boolean isPagerRight = true;// 是否可以执行viewPager的右滑动
private boolean isCanScroll = true;// 是否可以翻页
public boolean isLeft() {
return isLeft;
}
public ZoomImageView getZoomImageView() {
return zoomImageView;
}
public void setZoomImageView(ZoomImageView zoomImageView) {
this.zoomImageView = zoomImageView;
}
public void setLeft(boolean isLeft) {
this.isLeft = isLeft;
}
public boolean isRight() {
return isRight;
}
public void setRight(boolean isRight) {
this.isRight = isRight;
}
public boolean isPagerLeft() {
return isPagerLeft;
}
public void setPagerLeft(boolean isPagerLeft) {
this.isPagerLeft = isPagerLeft;
}
public boolean isPagerRight() {
return isPagerRight;
}
public void setPagerRight(boolean isPagerRight) {
this.isPagerRight = isPagerRight;
}
public boolean isCanScroll() {
return isCanScroll;
}
public void setCanScroll(boolean isCanScroll) {
this.isCanScroll = isCanScroll;
}
// 不重写这个方法,会有诡异现象
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (isCanScroll == false) {
return false;
} else {
return super.onTouchEvent(ev);
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if(isLeft){
if(isPagerRight){
// 是否可以执行viewPager的右滑动
isCanScroll = true;
}else {
isCanScroll = false;
}
}else if(isRight){
if(isPagerLeft){
// 是否可以执行viewPager的左滑动
isCanScroll = true;
}else {
isCanScroll = false;
}
}
if (isCanScroll == false) {
return false;
} else {
return super.onInterceptTouchEvent(ev);
}
}
/**
* 还原image
*/
public void initImage() {
zoomImageView.initImage();
}
}
使用:Adapter:
WindowManager manager = activity.getWindowManager();
window_width = manager.getDefaultDisplay().getWidth();
window_height = manager.getDefaultDisplay().getHeight();
holder.zoomImg.setmActivity(activity);
holder.zoomImg.setViewPager(viewPager);
// state_height状态栏高度
holder.zoomImg.setScreen_H(window_height - state_height);
holder.zoomImg.setScreen_W(window_width);
loader.displayImage(mImageUrl[position], holder.zoomImg);
Activity:
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
viewPager.initImage();
tvPage.setText(arg0+1+"/"+imageUrl.length);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
特别的:获取有效的状态栏高度
/**
* 获取状态栏高度
*
* @return
*/
public int getStatusBarHeight()
{
Class<?> c = null;
Object obj = null;
java.lang.reflect.Field field = null;
int x = 0;
int statusBarHeight = 0;
try
{
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
statusBarHeight = getResources().getDimensionPixelSize(x);
return statusBarHeight;
}
catch (Exception e)
{
e.printStackTrace();
}
return statusBarHeight;
}
希望能有更好的建议!