Implementing a Gallery with pinch zoom

泄露秘密 提交于 2020-01-03 05:36:52

问题


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 MainActivity.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.


回答1:


You should create the ImageZoomView in the getView method, assuming the ImageZoomView is a widget of your own subclassing the ImageView widget.




回答2:


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);
        }
    }
}


来源:https://stackoverflow.com/questions/5961445/implementing-a-gallery-with-pinch-zoom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!