我想要一个zoom in
和zoom out
功能在GalleryView
android.I 正在尝试很多方法,但它不起作用。我在 android 上找到了另一个教程zoom in
和Zoom out
图像,GalleryView
但尝试不起作用 任何人都可以帮助我如何实现此功能,为什么教程上的代码不起作用?
我正在尝试以下链接的本教程:
http://blog.projapps.com/?p=85
像下面的代码:
MyImageview.java
import android.content.Context;
import android.graphics.Matrix;
import android.view.MotionEvent;
import android.widget.ImageView;
public class MyImageView extends ImageView {
public MyImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
boolean isDraggable = false;
boolean isRight = false;
boolean isLeft = false;
// These matrices will be used to move and zoom image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
public void setMatrix(Matrix m) {
matrix.set(m);
}
public void setSavedMatrix(Matrix m) {
savedMatrix.set(m);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (isDraggable) {
// place pan/zoom codes here
}
return true;
}
}
我的画廊.java
public class MyGallery extends Gallery {
private PointF m_start = new PointF();
private MyImageView imageView;
public MyGallery(Context context) {
super(context);
}
public MyGallery(Context context, AttributeSet attrSet) {
super(context, attrSet);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
imageView = (MyImageView) getSelectedView();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
m_start.set(event.getX(), event.getY());
break;
case MotionEvent.ACTION_POINTER_DOWN:
imageView.isDraggable = true;
if (imageView.getScaleType() != ImageView.ScaleType.MATRIX) {
imageView.setMatrix(imageView.getImageMatrix());
imageView.setScaleType(ImageView.ScaleType.MATRIX);
}
break;
case MotionEvent.ACTION_MOVE:
if ((event.getX() - m_start.x < 0 && imageView.isRight) ||
(event.getX() - m_start.x > 0 && imageView.isLeft))
imageView.isDraggable = false;
break;
}
if (!imageView.isDraggable) {
onTouchEvent(event);
}
return super.onInterceptTouchEvent(event);
}
}
我正在加载图像适配器类,如下代码:
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
MyImageView i = new MyImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
return i;
}
private Context mContext;
}
并在我的画廊类中加载图像适配器,如下面的代码:
g = (MyGallery) findViewById(R.id.gallery);
g.setSpacing(60);
g.setUnselectedAlpha(1);
g.setAdapter(new ImageAdapter(this));
此代码成功加载图像,但无法使用缩放功能!