我希望在zoom in
上使用zoom out
和GalleryView
功能。我正在尝试很多方法,但它不起作用。我在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;
}
}
MyGallery.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));
此代码成功加载图片,但不起作用缩放功能!