一个关于android相册图片浏览的问题!如何实现放大缩小滑动浏览下一张的效果

菜鸟求知 发布于 2013/06/19 09:37
阅读 3K+
收藏 0
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.Display;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ViewFlipper;


@SuppressLint("CutPasteId")
public class MainActivity extends Activity implements OnTouchListener, OnGestureListener {
/** Called when the activity is first created. */
// 放大缩小
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
PointF start = new PointF();
PointF mid = new PointF();
float oldDist;
private static final String TAG = "MainActivity";
private ViewFlipper viewFlipper;
private GestureDetector detector; //手势检测
Animation leftInAnimation;
Animation leftOutAnimation;
Animation rightInAnimation;
Animation rightOutAnimation;
//private ImageView myImageView;
// 模式
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
private int localImages[]={
R.drawable.jgms1, R.drawable.jgms2, R.drawable.jgms3,R.drawable.jgms4,R.drawable.jgms5
,R.drawable.jgms6,R.drawable.jgms7,R.drawable.jgms8,R.drawable.jgms9,R.drawable.jgms10
,R.drawable.jgms11
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.activity_main);
viewFlipper = (ViewFlipper)findViewById(R.id.zoomflipper);
       detector = new GestureDetector(this);
for(int i=0;i<localImages.length;i++){
viewFlipper.addView(addImageView(localImages[i]));
}

       //往viewFlipper添加View
      
       //动画效果
    leftInAnimation = AnimationUtils.loadAnimation(this, R.anim.left_in);
leftOutAnimation = AnimationUtils.loadAnimation(this, R.anim.left_out);
rightInAnimation = AnimationUtils.loadAnimation(this, R.anim.right_in);
rightOutAnimation = AnimationUtils.loadAnimation(this, R.anim.right_out);
}
private View addImageView(int id){
Bitmap bitmap=((BitmapDrawable)getResources().getDrawable(id)).getBitmap();
ImageView myImageView=new ImageView(this);
Display display = getWindowManager().getDefaultDisplay();
//myImageView.set(display.getWidth(), display.getHeight(),bitmap);
myImageView.setImageBitmap(bitmap);
myImageView.setScaleType(ScaleType.MATRIX);
myImageView.setOnTouchListener(this);
return myImageView;
}


@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView myImageView = (ImageView) v;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
// 设置拖拉模式
case MotionEvent.ACTION_DOWN:
matrix.set(myImageView.getImageMatrix());
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
mode = DRAG;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;


// 设置多点触摸模式
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
break;
// 若为DRAG模式,则点击移动图片
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY()
- start.y);
}
// 若为ZOOM模式,则点击触摸缩放
else if (mode == ZOOM) {
float newDist = spacing(event);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
// 设置硕放比例和图片的中点位置
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
myImageView.setImageMatrix(matrix);
return true;
}


// 计算移动距离
private float spacing(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);
}


// 计算中点位置
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}


private ImageView getImageView(int id){
ImageView imageView = new ImageView(this);
imageView.setImageResource(id);
return imageView;
}


@Override
public boolean onTouchEvent(MotionEvent event) {
 
return this.detector.onTouchEvent(event); //touch事件交给手势处理。
}


@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}


@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.i(TAG, "e1="+e1.getX()+" e2="+e2.getX()+" e1-e2="+(e1.getX()-e2.getX()));
if(e1.getX()-e2.getX()>120){
viewFlipper.setInAnimation(leftInAnimation);
viewFlipper.setOutAnimation(leftOutAnimation);
   viewFlipper.showNext();//向右滑动
   return true;
}else if(e1.getX()-e2.getY()<-120){
viewFlipper.setInAnimation(rightInAnimation);
viewFlipper.setOutAnimation(rightOutAnimation);
viewFlipper.showPrevious();//向左滑动
return true;
}
return false;
}


@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub

}


@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}


@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub

}


@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}
这段代码只能实现一张图放大缩小而无法浏览下一张!求大神指点修改一下!
加载中
返回顶部
顶部