Implementing a Gallery with pinch zoom
I have a gallery of images and I'm trying to enable pinch zoom on it. Separately, they work just fine. The problem is, I can't for the life of me bind the two of them together! I tried to bind the ImageZoomView in the ImageAdapter, to no avail. Should I try doing it when the user clicks the image? Does anyone have any other ideas? This is the code in ImageAdapter.class that returns the gallery elements:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(),mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(300, 450));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
i.setImageBitmap(bitmap);
return i;
}
And this is how I handle it in MainActiv开发者_如何学JAVAity.class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
Log.i("blah","e ok");
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// mBitmap = ImageAdapter.mImageIds[position];
// mZoomView = (ImageZoomView)findViewById(R.id.zoomview);
// mZoomView.setZoomState(mZoomControl.getZoomState());
// mZoomView.setImage(mBitmap);
// Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
As you can see, I tried something, but my app always crashes. :( Any help here would be appreciated.
You should create the ImageZoomView
in the getView
method, assuming the ImageZoomView is a widget of your own subclassing the ImageView widget.
They way I have handled it is by subclassing gallery and overriding OnScroll, OnFling and OnTouch feeding the events to scaledetector and sending them on to the super-class when im on the edge of the pictures and translating the picture when im not. Bear in mind this is still work in progress but it's the closest thing I have to a working solution at this moment.
Aka it works but it's "quirky".
private class ScaleListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor = detector.getScaleFactor();
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
Log.d(TAG, "" + mScaleFactor);
scale(mScaleFactor, detector.getFocusX(), detector.getFocusY());
return true;
}
}
And
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (matrix == null)
matrix = new Matrix();
matrix.set(getSelectedImageView().getImageMatrix());
} else if (event.getAction() == MotionEvent.ACTION_UP
&& event.getPointerCount() == 0) {
scrolling = 0;
}
mScaleDetector.onTouchEvent(event);
mGestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
And
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
if (mScaleDetector.isInProgress()) {
scrolling = 0;
return true;
} else if (isAtRightEdge() && distanceX > 1) {
matrix.postTranslate(0, -distanceY);
getSelectedImageView().setImageMatrix(matrix);
scrolling += distanceX;
return super.onScroll(e1, e2, distanceX, distanceY);
} else if (isAtLeftEdge() && distanceX < -1) {
matrix.postTranslate(0, -distanceY);
getSelectedImageView().setImageMatrix(matrix);
scrolling += distanceX;
return super.onScroll(e1, e2, distanceX, distanceY);
} else {
if (scrolling < 0.1 && scrolling > -0.1) {
matrix.postTranslate(-distanceX, -distanceY);
getSelectedImageView().setImageMatrix(matrix);
return false;
} else {
matrix.postTranslate(0, -distanceY);
getSelectedImageView().setImageMatrix(matrix);
scrolling += distanceX;
return super.onScroll(e1, e2, distanceX, distanceY);
}
}
}
精彩评论