在Android中使用HttpUrlConnection进行无限制的缩放缩放

时间:2012-10-03 17:08:14

标签: android httpurlconnection pinchzoom

我使用以下代码通过HttpUrlConnection获取我的图片文件。我想添加图像放大和缩小(pinch zoom)。我还从网上搜索了一些代码,但我发现图像可以无限制地放大和缩小。我不知道如何解决它。

这是我的Httpurlconnection

public class MainActivity extends Activity {
    private ImageView m_imgView;
    private String m_strImgURL = "http://wptrafficanalyzer.in/blog/wp-content/uploads/2012/09/http_url_connection_download_image_new_android_project.png";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        m_imgView = (ImageView) findViewById(R.id.myImageView);
        m_imgView.setImageBitmap(GetURLBitmap());
        m_imgView.setOnTouchListener(new zoom ());
    }
    private Bitmap GetURLBitmap() {
        URL url = null;
        Bitmap bmp = null;


    try {
        url = new URL(m_strImgURL);         
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    try {
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.connect();

        InputStream in = conn.getInputStream();
        bmp = BitmapFactory.decodeStream(in);
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bmp;
}
}

这是我的Zoom

public class zoom implements OnTouchListener {   
        Matrix matrix = new Matrix();   
        Matrix savedMatrix = new Matrix();   
        static final int NONE = 0;   
        static final int DRAG = 1;   
        static final int ZOOM = 2;   
        int mode = NONE;   
        PointF start = new PointF();   
        PointF mid = new PointF();   
        float oldDist = 1f;   
        @Override   
        public boolean onTouch(View v, MotionEvent event) {   
                ImageView view = (ImageView) v;   
                switch (event.getAction() & MotionEvent.ACTION_MASK) {   
                case MotionEvent.ACTION_DOWN:   
                        matrix.set(view.getImageMatrix());   
                        savedMatrix.set(matrix);   
                        start.set(event.getX(), event.getY());   
                        mode = DRAG;   

                    break;   
            case MotionEvent.ACTION_POINTER_DOWN:   
                    oldDist = spacing(event);   
                    if (oldDist > 10f) {   
                            savedMatrix.set(matrix);   
                            midPoint(mid, event);   
                            mode = ZOOM;   
                    }   
                    break;   
            case MotionEvent.ACTION_UP:   
            case MotionEvent.ACTION_POINTER_UP:   
                    mode = NONE;   
                    break;   
            case MotionEvent.ACTION_MOVE:   
                    if (mode == DRAG) {   
                            matrix.set(savedMatrix);   
                            matrix.postTranslate(event.getX() - start.x, event.getY()   
                                            - start.y);   
                    } else if (mode == ZOOM) {   
                            float newDist = spacing(event);   
                            if (newDist > 10f) {   
                                    matrix.set(savedMatrix);   
                                    float scale = newDist / oldDist;   
                                    matrix.postScale(scale, scale, mid.x, mid.y);   
                            }   
                    }   
                    break;   
            }   
            view.setImageMatrix(matrix);   
            return true;   
    }   


    private float spacing(MotionEvent event) {   
            float x = event.getX(0) - event.getX(1);   
            float y = event.getY(0) - event.getY(1);   
            return FloatMath.sqrt(x * x + y * y);   
    }   

    private void midPoint(PointF point, MotionEvent event) {   
            float x = event.getX(0) + event.getX(1);   
            float y = event.getY(0) + event.getY(1);   
            point.set(x / 2, y / 2);   
    }   
}

0 个答案:

没有答案