求助:类似QQ空间中相册中相片的浏览功能(滑动、放大缩小,双击放大再双击回原图)的功能
因公司项目需求,需要做个类似QQ空间中相册的相片浏览功能,我是用ViewFlipper来实现滑动的,但是在捕获ImageView的onTouch事件时,放大缩小功能跟滑动功能起了冲突,哪位GGJJ能帮忙看下!下面是我的源码:package dy.Imgs;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import android.view.View.OnTouchListener;
/**
* Android实现类似QQ空间相册的相片浏览功能
* @author Administrator
*
*/
public class Imgs extends Activity implements OnGestureListener ,OnTouchListener{
private ViewFlipper flipper;
private GestureDetector detector;
private static final String TAG = "Touch";
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
detector = new GestureDetector(this);
flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper1);
flipper.addView(addTextView(R.drawable.one));
flipper.addView(addTextView(R.drawable.two));
flipper.addView(addTextView(R.drawable.four));
flipper.addView(addTextView(R.drawable.five));
}
private View addTextView(int id) {
ImageView iv = new ImageView(this);
iv.setImageResource(id);
iv.setOnTouchListener(this);
return iv;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
//设置初点位置
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG");
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM");
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG, "mode=NONE");
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
// ...
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x,
event.getY() - start.y);
}
else if (mode == ZOOM) {
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
view.setImageMatrix(matrix);
flipper.addView(view);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return this.detector.onTouchEvent(event);
}
@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) {
if (e1.getX() - e2.getX() > 120) {
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
this.flipper.showNext();
return true;
} else if (e1.getX() - e2.getX() < -120) {
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
this.flipper.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;
}
/** Determine the space between the first two fingers */
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);
}
/** Calculate the mid point of the first two fingers */
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);
}
}
--------------------编程问答-------------------- 自己来顶顶,哪位帮忙看下 --------------------编程问答-------------------- --------------------编程问答-------------------- 不能用gallery么?我觉得gallery加上imageswitcher也能实现滑动点击放大啊。 --------------------编程问答-------------------- 哦哦,我尝试下使用gallery,因为刚刚学习android3天,还有很多不懂~~
谢谢提点 --------------------编程问答--------------------
今天做了尝试,通过gallery加上imageswitcher放大的做法貌似是用动画吧,但是用动画的话他变大的时候不能在滑动的时候跟其他的进行同步,会超出跟两边的图片重叠!! --------------------编程问答--------------------
不是使用动画效果实现的,我这有一个demo,可以发给你看看 --------------------编程问答--------------------
可否给我也发一个?871874241@qq.com --------------------编程问答-------------------- 也发我一个啊,刚好要用啊 240299452@qq.com --------------------编程问答--------------------
麻烦也发一份给我行吗?714237458@qq.com --------------------编程问答-------------------- 楼主,你的问题解决没有呀。我也遇到同样的问题,我用ViewFlipper做好了切换,现在老板叫我加上缩放功能,弄了一天了,还是没有头绪……希望得到你的帮助哟。邮箱miaoniancai@sina.com --------------------编程问答-------------------- 有人解决了么 麻烦发一份好么 谢啦 急用 953768578@qq.com --------------------编程问答-------------------- 楼主解决没啊 遇到和你一样的问题 麻烦给我发个demo看看 谢谢 --------------------编程问答-------------------- 可否给我发一个 244399401@qq.com --------------------编程问答-------------------- viewflipper实现图片缩放已经我已经实现了,不过有些bug。http://www.eoeandroid.com/thread-205601-1-1.html这个是我写的功能文章
补充:移动开发 , Android