使用捏缩放实现图库

时间:2011-05-11 08:37:23

标签: java android gallery pinch

我有一个图像库,我正在尝试对其进行缩放缩放。另外,他们工作得很好。问题是,我不能为我的生活把他们两个绑在一起!我试图在ImageAdapter中绑定ImageZoomView,但无济于事。我应该在用户点击图片时尝试这样做吗?有没有人有任何其他想法? 这是ImageAdapter.class中返回gallery元素的代码:

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

这就是我在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();
            }
        });

正如你所看到的,我尝试了一些东西,但我的应用程序总是崩溃。 :(这里的任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

您应该在ImageZoomView方法中创建getView,假设ImageZoomView是您自己子类化ImageView小部件的小部件。

答案 1 :(得分:1)

我处理它的方式是通过子类化库并重写OnScroll,OnFling和OnTouch将事件提供给scaledetector,并在im在图片的边缘时将它们发送到超类,并在im not时翻译图片。 请记住,这仍然在进行中,但这是我目前对工作解决方案最接近的事情。

Aka可行,但它“古怪”。

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

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

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