如何在触摸图像时通过放大/缩小来全屏查看图像

时间:2015-03-13 09:10:24

标签: android android-layout android-activity

我有以下imagedetails.xml布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/inLay"
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:orientation="horizontal" >

        <HorizontalScrollView
            android:id="@+id/hsv"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:fillViewport="true"
            android:measureAllChildren="false"
            android:scrollbars="none" >

            <LinearLayout
                android:id="@+id/innerLay"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <LinearLayout
                    android:id="@+id/controlled_medication"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <RelativeLayout
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent" >

                        <ImageView
                            android:id="@+id/image1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" />
                    </RelativeLayout>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/as_needed_medication"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <RelativeLayout
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal" >

                        <ImageView
                            android:id="@+id/image2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" />
                    </RelativeLayout>
                </LinearLayout>
            </LinearLayout>
        </HorizontalScrollView>
    </LinearLayout>
</RelativeLayout>

对于这个xml文件,我写了以下代码:

public class ImageDetails extends Activity {

    LinearLayout asthmaActionPlan, controlledMedication, asNeededMedication,
            rescueMedication, yourSymtoms, yourTriggers, wheezeRate, peakFlow;
    LayoutParams params;
    LinearLayout next, prev;
    int viewWidth;
    GestureDetector gestureDetector = null;
    HorizontalScrollView horizontalScrollView;
    ArrayList<LinearLayout> layouts;
    int parentLeft, parentRight;
    int mWidth;
    int currPosition, prevPosition;


    ImageView image1, image2;


    String pid;


    private ProgressDialog pDialog;


    JSONParser jsonParser = new JSONParser();


    private static final String url_detials = "http://www.example.com/details.php";


    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PRODUCT = "product";
    private static final String TAG_PID = "id";
    Private static final String TAG_IMAGE1 = "image1";
    private static final String TAG_IMAGE2 = "image2";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.productdetails);


        image1 = (ImageView) findViewById(R.id.image1);
        image2 = (ImageView) findViewById(R.id.image2);

        Intent i = getIntent();


        pid = i.getStringExtra(TAG_PID);


        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);

        horizontalScrollView = (HorizontalScrollView) findViewById(R.id.hsv);
        gestureDetector = new GestureDetector(new MyGestureDetector());

        controlledMedication = (LinearLayout) findViewById(R.id.controlled_medication);
        asNeededMedication = (LinearLayout) findViewById(R.id.as_needed_medication);

        Display display = getWindowManager().getDefaultDisplay();
        mWidth = display.getWidth(); // deprecated
        viewWidth = mWidth;
        layouts = new ArrayList<LinearLayout>();
        params = new LayoutParams(viewWidth, LayoutParams.WRAP_CONTENT);
        controlledMedication.setLayoutParams(params);
        asNeededMedication.setLayoutParams(params);

        layouts.add(controlledMedication);
        layouts.add(asNeededMedication);

        horizontalScrollView.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        });

        new GetDetails().execute();



    }



    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            if (e1.getX() < e2.getX()) {
                currPosition = getVisibleViews("left");
            } else {
                currPosition = getVisibleViews("right");
            }

            horizontalScrollView.smoothScrollTo(layouts.get(currPosition)
                    .getLeft(), 0);
            return true;
        }
    }

    public int getVisibleViews(String direction) {
        Rect hitRect = new Rect();
        int position = 0;
        int rightCounter = 0;
        for (int i = 0; i < layouts.size(); i++) {
            if (layouts.get(i).getLocalVisibleRect(hitRect)) {
                if (direction.equals("left")) {
                    position = i;
                    break;
                } else if (direction.equals("right")) {
                    rightCounter++;
                    position = i;
                    if (rightCounter == 2)
                        break;
                }
            }
        }
        return position;
    }

    class GetDetails extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ImageDetails.this);
            pDialog.setMessage("Loading Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            // pDialog.show();
        }

        @Override
        protected String doInBackground(String... arg) {


            runOnUiThread(new Runnable() {
                public void run() {

                    int success;
                    try {

                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("id", pid));


                        JSONObject json = jsonParser.makeHttpRequest(
                                url_detials, "GET", params);



                        success = json.getInt(TAG_SUCCESS);

                        if (success == 1) {

                            JSONArray productObj = json
                                    .getJSONArray(TAG_PRODUCT); 


                            JSONObject product = productObj.getJSONObject(0);

                            Bitmap bitmap = null;
                            Bitmap bitmap2 = null;
                            try {
                                // Download Image from URL
                                InputStream input = new java.net.URL(
                                        product.getString(TAG_IMAGE1))
                                        .openStream();
                                // Decode Bitmap
                                bitmap = BitmapFactory.decodeStream(input);
                                image1.setImageBitmap(bitmap);

                                // Download Image from URL
                                InputStream input2 = new java.net.URL(
                                        product.getString(TAG_IMAGE2))
                                        .openStream();
                                // Decode Bitmap
                                bitmap2 = BitmapFactory.decodeStream(input2);
                                image2.setImageBitmap(bitmap2);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }



                        } else {

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
            return null;
        }
    }

    protected void onPostExecute(String file_url) {
        // dismiss the dialog once got all details
        pDialog.dismiss();

    }
}

现在它将在水平视图中为我提供输出,我可以通过手势触摸更改图像。

现在当我选择任何图像时,它将以全屏显示,并带有缩放/缩小选项。

我尝试了以下方法:

    image1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            image1.buildDrawingCache();
            Bitmap bitmap = image1.getDrawingCache();

            Intent intent = new Intent(getApplicationContext(),
                    Imagesview.class);
            intent.putExtra("BitmapImage", bitmap);
        }
    });

我正在将图片发送到其他活动,但它没有打开新活动。

我收到以下错误日志:

FATAL EXCEPTION: main
Process: com.novumlogic.ideal, PID: 18636
  java.lang.NullPointerException
    at com.novumlogic.ideal.ProductDetails$MyGestureDetector.onFling(ProductDetails.java:213)
    at android.view.GestureDetector.onTouchEvent(GestureDetector.java:610)
    at com.novumlogic.ideal.ProductDetails$1.onTouch(ProductDetails.java:133)
    at android.view.View.dispatchTouchEvent(View.java:7772)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2316)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2013)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2322)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2027)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2322)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2027)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2322)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2027)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2322)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2027)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2322)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2027)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2145)
    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1577)
    at android.app.Activity.dispatchTouchEvent(Activity.java:2508)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2093)
    at android.view.View.dispatchPointerEvent(View.java:7973)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4384)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4255)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3801)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3851)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3820)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3927)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3828)
    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3984)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3801)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3851)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3820)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3828)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3801)
    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6116)
    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6096)
    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6050)
    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6246)
    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
    at android.os.MessageQueue.nativePollOnce(Native Method)
    at android.os.MessageQueue.next(MessageQueue.java:138)
    at android.os.Looper.loop(Looper.java:150)
    at android.app.ActivityThread.main(ActivityThread.java:5333)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
    at dalvik.system.NativeStart.main(Native Method)

那么,如何使用放大/缩小选项在全屏幕上显示图像?

3 个答案:

答案 0 :(得分:5)

下面的类可以用作放大/缩小的ImageView以及DRAG ...

public class TouchImageView extends ImageView {

Matrix matrix;

// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;

// Remember some things for zooming
PointF last = new PointF();
PointF start = new PointF();
float minScale = 1f;
float maxScale = 3f;
float[] m;


int viewWidth, viewHeight;
static final int CLICK = 3;
float saveScale = 1f;
protected float origWidth, origHeight;
int oldMeasuredWidth, oldMeasuredHeight;


ScaleGestureDetector mScaleDetector;

Context context;

public TouchImageView(Context context) {
    super(context);
    sharedConstructing(context);
}

public TouchImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    sharedConstructing(context);
}

private void stopInterceptEvent()
{
    getParent().requestDisallowInterceptTouchEvent(true);
}

private void startInterceptEvent()
{
getParent().requestDisallowInterceptTouchEvent(false);
}

private void sharedConstructing(Context context) {
    super.setClickable(true);
    this.context = context;
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
    matrix = new Matrix();
    m = new float[9];
    setImageMatrix(matrix);
    setScaleType(ScaleType.MATRIX);

    setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mScaleDetector.onTouchEvent(event);
            PointF curr = new PointF(event.getX(), event.getY());

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    last.set(curr);
                    start.set(last);
                    mode = DRAG;
                    stopInterceptEvent();
                    break;

                case MotionEvent.ACTION_MOVE:
                    if (mode == DRAG) {
                        float deltaX = curr.x - last.x;
                        float deltaY = curr.y - last.y;
                        float fixTransX = getFixDragTrans(deltaX, viewWidth, origWidth * saveScale);
                        float fixTransY = getFixDragTrans(deltaY, viewHeight, origHeight * saveScale);
                        matrix.postTranslate(fixTransX, fixTransY);
                        fixTrans();
                        last.set(curr.x, curr.y);

                        float transX = m[Matrix.MTRANS_X];

                        if((int) (getFixTrans(transX, viewWidth, origWidth * saveScale) + fixTransX) == 0)
                            startInterceptEvent();
                        else
                            stopInterceptEvent();
                    }
                    break;

                case MotionEvent.ACTION_UP:
                    mode = NONE;
                    int xDiff = (int) Math.abs(curr.x - start.x);
                    int yDiff = (int) Math.abs(curr.y - start.y);
                    if (xDiff < CLICK && yDiff < CLICK)
                        performClick();
                    startInterceptEvent();
                    break;

                case MotionEvent.ACTION_POINTER_UP:
                    mode = NONE;
                    break;
            }

            setImageMatrix(matrix);
            invalidate();
            return true; // indicate event was handled
        }

    });
}

public void setMaxZoom(float x) {
    maxScale = x;
}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        mode = ZOOM;
        return true;
    }

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        float mScaleFactor = detector.getScaleFactor();
        float origScale = saveScale;
        saveScale *= mScaleFactor;
        if (saveScale > maxScale) {
            saveScale = maxScale;
            mScaleFactor = maxScale / origScale;
        } else if (saveScale < minScale) {
            saveScale = minScale;
            mScaleFactor = minScale / origScale;
        }

        if (origWidth * saveScale <= viewWidth || origHeight * saveScale <= viewHeight)
            matrix.postScale(mScaleFactor, mScaleFactor, viewWidth / 2, viewHeight / 2);
        else
            matrix.postScale(mScaleFactor, mScaleFactor, detector.getFocusX(), detector.getFocusY());

        fixTrans();
        return true;
    }
}

void fixTrans() {
    matrix.getValues(m);
    float transX = m[Matrix.MTRANS_X];
    float transY = m[Matrix.MTRANS_Y];

    float fixTransX = getFixTrans(transX, viewWidth, origWidth * saveScale);
    float fixTransY = getFixTrans(transY, viewHeight, origHeight * saveScale);

    if (fixTransX != 0 || fixTransY != 0)
        matrix.postTranslate(fixTransX, fixTransY);
}

float getFixTrans(float trans, float viewSize, float contentSize) {
    float minTrans, maxTrans;

    if (contentSize <= viewSize) {
        minTrans = 0;
        maxTrans = viewSize - contentSize;
    } else {
        minTrans = viewSize - contentSize;
        maxTrans = 0;
    }

    if (trans < minTrans)
        return -trans + minTrans;
    if (trans > maxTrans)
        return -trans + maxTrans;
    return 0;
}

float getFixDragTrans(float delta, float viewSize, float contentSize) {
    if (contentSize <= viewSize) {
        return 0;
    }
    return delta;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    viewWidth = MeasureSpec.getSize(widthMeasureSpec);
    viewHeight = MeasureSpec.getSize(heightMeasureSpec);

    //
    // Rescales image on rotation
    //
    if (oldMeasuredHeight == viewWidth && oldMeasuredHeight == viewHeight
            || viewWidth == 0 || viewHeight == 0)
        return;
    oldMeasuredHeight = viewHeight;
    oldMeasuredWidth = viewWidth;

    if (saveScale == 1) {
        //Fit to screen.
        float scale;

        Drawable drawable = getDrawable();
        if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0)
            return;
        int bmWidth = drawable.getIntrinsicWidth();
        int bmHeight = drawable.getIntrinsicHeight();

        Log.d("bmSize", "bmWidth: " + bmWidth + " bmHeight : " + bmHeight);

        float scaleX = (float) viewWidth / (float) bmWidth;
        float scaleY = (float) viewHeight / (float) bmHeight;
        scale = Math.min(scaleX, scaleY);
        matrix.setScale(scale, scale);

        // Center the image
        float redundantYSpace = (float) viewHeight - (scale * (float) bmHeight);
        float redundantXSpace = (float) viewWidth - (scale * (float) bmWidth);
        redundantYSpace /= (float) 2;
        redundantXSpace /= (float) 2;

        matrix.postTranslate(redundantXSpace, redundantYSpace);

        origWidth = viewWidth - 2 * redundantXSpace;
        origHeight = viewHeight - 2 * redundantYSpace;
        setImageMatrix(matrix);
    }


    fixTrans();
}
}

替换你的ImageView

<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

这一个:

<com.pkg.TouchImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
  

您可以在此处使用RecyclingImageView作为超类,以提高您的应用效率。它也适用于Android原生ImageView

我希望这对你有所帮助。

答案 1 :(得分:3)

Android不支持imageViews开箱即用的缩放功能。所以你必须做一些自定义实现来缩放(放大/缩小)imageView(你所做的)。或者您可以使用第三方库来解除繁重的工作。他们可以处理所有场景。我通常更喜欢Chris Bane的Photoview

答案 2 :(得分:-1)

public class ZoomableImageView extends View {
    private static final String TAG = "ZoomableImageView";     float trans=80; 

    public static Bitmap imgBitmap = null;

  Boolean zom=false;
    private static WeakReference<Context> mContext;

    //Matrices will be used to move and zoom image
    Matrix matrix = new Matrix();    
    Matrix savedMatrix = new Matrix();   

    PointF start = new PointF();      



    //We can be in one of these 3 states
    static final int NONE = 0;
    static final int DRAG = 1;
    static final int ZOOM = 2;
    int mode = NONE;

    //For animating stuff  


    float easing = 0.2f;  
    boolean isAnimating = false;int flag=0;

    float scaleDampingFactor = 0.5f;
    private WeakReference<Bitmap> resizedBitmap,mBitmap;
    //For pinch and zoom
    float oldDist = 1f;  
    PointF mid = new PointF();Bitmap bms;

    private Handler mHandler = new Handler();      

    float minScale;
    float maxScale = 8.0f;
    public static int index=1000; 
    float wpRadius = 25.0f;
    float wpInnerRadius = 20.0f;


    public static final int DEFAULT_SCALE_FIT_INSIDE = 0;
    public static final int DEFAULT_SCALE_ORIGINAL = 1;  



    public int getDefaultScale() {
        return defaultScale;
    }

    public void setDefaultScale(int defaultScale) {
        this.defaultScale = defaultScale;
    }

    public ZoomableImageView(Context context) {

        super(context); 

        con=context;
        setFocusable(true);
        setFocusableInTouchMode(true);



      File file = new File(HomePage.sdfile + "/" + index+".jpg");  


  Options options = new BitmapFactory.Options();
  try{

      options.inScaled = false;
      options.inDither = false;
      options.inPreferredConfig = Bitmap.Config.ARGB_8888;

   imgBitmap=decodeFile(file);  
              }
   catch(OutOfMemoryError e){ 
       System.out.println("out of memory");
       flag=1;

           System.out.println("clearing bitmap????????????");
           if (imgBitmap!=null) { 
           this.setBackgroundResource(0);
           this.clearAnimation();
       imgBitmap.recycle(); 
        imgBitmap = null;}




   }


        screenDensity = context.getResources().getDisplayMetrics().density;

        initPaints();
        gestureDetector = new GestureDetector(new MyGestureDetector());        
    }

    public ZoomableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
      System.out.println("zoomableimageview");
        screenDensity = context.getResources().getDisplayMetrics().density;      
        initPaints();
        gestureDetector = new GestureDetector(new MyGestureDetector());

        defaultScale = ZoomableImageView.DEFAULT_SCALE_FIT_INSIDE; 
    }

    private void initPaints() {
        background = new Paint();
        System.out.println("initPaints");
    }

    @Override
    protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
        super.onSizeChanged(width, height, oldWidth, oldHeight);
        System.out.println("onsizechanged");

        //Reset the width and height. Will draw bitmap and change
        containerWidth = width;
        containerHeight = height;

        if(imgBitmap != null) {
            int imgHeight = imgBitmap.getHeight();
            int imgWidth = imgBitmap.getWidth();

            float scale;
            int initX = 0;
            int initY = 0;          

            if(defaultScale == ZoomableImageView.DEFAULT_SCALE_FIT_INSIDE) {              
                if(imgWidth > containerWidth) {          
                    scale = (float)containerWidth / imgWidth;          
                    float newHeight = imgHeight * scale;          
                    initY = (containerHeight - (int)newHeight)/2;

                    matrix.setScale(scale, scale);
                    matrix.postTranslate(0, initY);
                    System.out.println("scale"+scale);
                }
                else {          
                    scale = (float)containerHeight / imgHeight; 
                    float newWidth = imgWidth * scale;
                    initX = (containerWidth - (int)newWidth)/2;

                    matrix.setScale(scale, scale);
                    matrix.postTranslate(initX, 0);
                }

                curX = initX;
                curY = initY;

                currentScale = scale;
                minScale = scale;
                System.out.println("scale"+scale);
            }
            else {
                if(imgWidth > containerWidth) {                                  
                    initY = (containerHeight - (int)imgHeight)/2;                  
                    matrix.postTranslate(0, initY);
                }
                else {                              
                    initX = (containerWidth - (int)imgWidth)/2;                  
                    matrix.postTranslate(initX, 0);
                }

                curX = initX;
                curY = initY;

                currentScale = 1.0f;
                minScale = 1.0f;  


            }
            System.out.println("currentscale"+currentScale);

            System.out.println("minscale"+minScale);


            invalidate();          
        }
    }

    @Override
    protected void onDraw(Canvas canvas) { 
        System.out.println("ondraw");

        if(imgBitmap != null && canvas != null)
        {                                          
            canvas.drawBitmap(imgBitmap, matrix, background);     
            System.out.println("image not null---------------------");

        }
        if(flag==1){
            System.out.println("image null*************************");
            Intent intent = new Intent(Intent.ACTION_MAIN);
           intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            con.startActivity(intent);
            System.exit(0);
        }

    }

    //Checks and sets the target image x and y co-ordinates if out of bounds
    private void checkImageConstraints() {
        System.out.println("checkimageconstans");
        if(imgBitmap == null) {
            return;
        }

        float[] mvals = new float[9];
        matrix.getValues(mvals);

        currentScale = mvals[0];

        if(currentScale < minScale) {                              
            float deltaScale = minScale / currentScale;                  
            float px = containerWidth/2;
            float py = containerHeight/2;          
            matrix.postScale(deltaScale, deltaScale, px, py);
            invalidate();
        }      

        matrix.getValues(mvals);
        currentScale = mvals[0];
        curX = mvals[2];
        curY = mvals[5];

        int rangeLimitX = containerWidth - (int)(imgBitmap.getWidth() * currentScale);
        int rangeLimitY = containerHeight - (int)(imgBitmap.getHeight() * currentScale);


        boolean toMoveX = false;
        boolean toMoveY = false;  

        if(rangeLimitX < 0) {
            if(curX > 0) {
                targetX = 0;
                toMoveX = true;
            }
            else if(curX < rangeLimitX) {
                targetX = rangeLimitX;
                toMoveX = true;
            }
        }
        else {
            targetX = rangeLimitX / 2;
            toMoveX = true;
        }

        if(rangeLimitY < 0) {
            if(curY > 0) {
                targetY = 0;
                toMoveY = true;
            }
            else if(curY < rangeLimitY) {
                targetY = rangeLimitY;
                toMoveY = true;
            }
        }
        else {
            targetY = rangeLimitY / 2;
            toMoveY = true;
        }

        if(toMoveX == true || toMoveY == true) {
            if(toMoveY == false) {
                targetY = curY;
            }
            if(toMoveX == false) {
                targetX = curX;
            }          

            //Disable touch event actions
            isAnimating = true;
            //Initialize timer          
            mHandler.removeCallbacks(mUpdateImagePositionTask);
            mHandler.postDelayed(mUpdateImagePositionTask, 100);
        }
    }      


    @Override
    public boolean onTouchEvent(MotionEvent event) {  
        System.out.println("onTouch");
        flag=0;
        if(gestureDetector.onTouchEvent(event)) {
            return true;
        }

        if(isAnimating == true) {
            return true;
        }

        //Handle touch events here      
        float[] mvals = new float[9];
        switch(event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            if(isAnimating == false) {
                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;

            matrix.getValues(mvals);
            curX = mvals[2];
            curY = mvals[5];
            currentScale = mvals[0];

            if(isAnimating == false) {                                      
                checkImageConstraints();
            }
        break;

        case MotionEvent.ACTION_MOVE:          
            if(mode == DRAG && isAnimating == false) {
                matrix.set(savedMatrix);
                float diffX = event.getX() - start.x;
                float diffY = event.getY() - start.y;

                matrix.postTranslate(diffX, diffY);

                matrix.getValues(mvals);
                curX = mvals[2];
                curY = mvals[5];
                currentScale = mvals[0];
            }
            else if(mode == ZOOM && isAnimating == false) {
                System.out.println("for zooming");
                zom=true;
                float newDist = spacing(event);              
                if(newDist > 10f) {
                    matrix.set(savedMatrix);
                    float scale = newDist / oldDist;                  
                    matrix.getValues(mvals);
                    currentScale = mvals[0];

                    if(currentScale * scale <= minScale) {
                        matrix.postScale(minScale/currentScale, minScale/currentScale, mid.x, mid.y);
                    }                  
                    else if(currentScale * scale >= maxScale) {
                        matrix.postScale(maxScale/currentScale, maxScale/currentScale, mid.x, mid.y);
                    }
                    else {
                        matrix.postScale(scale, scale, mid.x, mid.y);  
                    }


                    matrix.getValues(mvals);
                    curX = mvals[2];
                    curY = mvals[5];
                    currentScale = mvals[0];   
                   trans=scale;
                }
            }

        break;                              
        }

        //Calculate the transformations and then invalidate
        invalidate();

        return true;
    }

    private float spacing(MotionEvent event) {
        System.out.println("spacing");
        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) {
        System.out.println("midpoint");
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        point.set(x/2, y/2);
    }

    public void setImageBitmap(Bitmap b) {   
        System.out.println("setImageBitmap");
        if(b != null) {
            imgBitmap = b;              

            containerWidth = getWidth();
            containerHeight = getHeight();

            int imgHeight = imgBitmap.getHeight();
            int imgWidth = imgBitmap.getWidth();

            float scale;
            int initX = 0;
            int initY = 0;

            matrix.reset();

            if(defaultScale == ZoomableImageView.DEFAULT_SCALE_FIT_INSIDE) {              
                if(imgWidth > containerWidth) {          
                    scale = (float)containerWidth / imgWidth;          
                    float newHeight = imgHeight * scale;          
                    initY = (containerHeight - (int)newHeight)/2;

                    matrix.setScale(scale, scale);
                    matrix.postTranslate(0, initY);
                }
                else {          
                    scale = (float)containerHeight / imgHeight;
                    float newWidth = imgWidth * scale;
                    initX = (containerWidth - (int)newWidth)/2;

                    matrix.setScale(scale, scale);
                    matrix.postTranslate(initX, 0);
                }

                curX = initX;
                curY = initY;

                currentScale = scale;
                minScale = scale;
            }
            else {
                if(imgWidth > containerWidth) {
                    initX = 0;
                    if(imgHeight > containerHeight) {                      
                        initY = 0;
                    }
                    else {                      
                        initY = (containerHeight - (int)imgHeight)/2;
                    }

                    matrix.postTranslate(0, initY);
                }
                else {                              
                    initX = (containerWidth - (int)imgWidth)/2;
                    if(imgHeight > containerHeight) {
                        initY = 0;
                    }
                    else {
                        initY = (containerHeight - (int)imgHeight)/2;
                    }
                    matrix.postTranslate(initX, 0);
                }

                curX = initX;
                curY = initY;

                currentScale = .5f;
                minScale = .5f;              
            }

            invalidate();          
        }
        else {
            Log.d(TAG, "bitmap is null");
        }
    }

    public Bitmap getPhotoBitmap() { 

        System.out.println("getphotobitmap");
        return imgBitmap;
    }


    private Runnable mUpdateImagePositionTask = new Runnable() {
        public void run() { 
            //zoomout();
             if(zom==true && trans<1.0f)
             {PageCurlView.zoom=0;

               Intent i = new Intent();
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.setClass(getContext(), StandaloneExample.class);   
                getContext().startActivity(i);

                zoomout();

                if(imgBitmap!=null){
                imgBitmap.recycle();
                imgBitmap = null;}
//                  
                try {
                    this.finalize();
                } catch (Throwable e) {
                    // TODO Auto-generated catch block
                    System.out.println("finalizer problem");   
                    e.printStackTrace();
                }

//                  


             } 
            System.out.println("run");
            float[] mvals;

            if(Math.abs(targetX - curX) < 5 && Math.abs(targetY - curY) < 5) {
                isAnimating = false;
                mHandler.removeCallbacks(mUpdateImagePositionTask);

                mvals = new float[9];
                matrix.getValues(mvals);

                currentScale = mvals[0];
                curX = mvals[2];
                curY = mvals[5];

                //Set the image parameters and invalidate display
                float diffX = (targetX - curX);
                float diffY = (targetY - curY);

                matrix.postTranslate(diffX, diffY);
            }
            else {
                isAnimating = true;
                mvals = new float[9];
                matrix.getValues(mvals);

                currentScale = mvals[0];
                curX = mvals[2];
                curY = mvals[5];

                //Set the image parameters and invalidate display
                float diffX = (targetX - curX) * 0.3f;
                float diffY = (targetY - curY) * 0.3f;

                matrix.postTranslate(diffX, diffY);              
                mHandler.postDelayed(this, 25);              
            }

            invalidate();          
        }
    };

    private Runnable mUpdateImageScale = new Runnable() {
        public void run() {  
            System.out.println("run2");
            float transitionalRatio = targetScale / currentScale;          
            float dx;
            if(Math.abs(transitionalRatio - 1) > 0.05) {
                isAnimating = true;              
                if(targetScale > currentScale) {                                      
                    dx = transitionalRatio - 1;
                    scaleChange = 1 + dx * 0.2f;

                    currentScale *= scaleChange;

                    if(currentScale > targetScale) {
                        currentScale = currentScale / scaleChange;
                        scaleChange = 1;
                    }
                }
                else {                                  
                    dx = 1 - transitionalRatio;                  
                    scaleChange = 1 - dx * 0.5f;
                    currentScale *= scaleChange;

                    if(currentScale < targetScale) {
                        currentScale = currentScale / scaleChange;
                        scaleChange = 1;
                    }
                }


                if(scaleChange != 1) {
                    matrix.postScale(scaleChange, scaleChange, targetScaleX, targetScaleY);              
                    mHandler.postDelayed(mUpdateImageScale, 15);
                    invalidate();
                }
                else {
                    isAnimating = false;
                    scaleChange = 1;                  
                    matrix.postScale(targetScale/currentScale, targetScale/currentScale, targetScaleX, targetScaleY);
                    currentScale = targetScale;
                    mHandler.removeCallbacks(mUpdateImageScale);
                    invalidate();
                    checkImageConstraints();
                }              
            }
            else {
                isAnimating = false;
                scaleChange = 1;              
                matrix.postScale(targetScale/currentScale, targetScale/currentScale, targetScaleX, targetScaleY);
                currentScale = targetScale;
                mHandler.removeCallbacks(mUpdateImageScale);
                invalidate();
                checkImageConstraints();
            }                              
        }
    };

   /** Show an event in the LogCat view, for debugging */
   private void dumpEvent(MotionEvent event) {
       System.out.println("dumpevent");
      String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE", "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
      StringBuilder sb = new StringBuilder();
      int action = event.getAction();
      int actionCode = action & MotionEvent.ACTION_MASK;
      sb.append("event ACTION_").append(names[actionCode]);
      if (actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP) {
         sb.append("(pid ").append(action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
         sb.append(")");
      }
      sb.append("[");

      for (int i = 0; i < event.getPointerCount(); i++) {
         sb.append("#").append(i);
         sb.append("(pid ").append(event.getPointerId(i));
         sb.append(")=").append((int) event.getX(i));
         sb.append(",").append((int) event.getY(i));
         if (i + 1 < event.getPointerCount())
            sb.append(";");
      }
      sb.append("]");
   }

   class MyGestureDetector extends SimpleOnGestureListener {

        @Override
        public boolean onDoubleTap(MotionEvent event) {  
            System.out.println("simpleOnGestureListener");
            if(isAnimating == true) {
                return true;
            }

            scaleChange = 1;
            isAnimating = true;
            targetScaleX = event.getX();
            targetScaleY = event.getY();

            if(Math.abs(currentScale - maxScale) > 0.1) {          
                targetScale = maxScale;
            }
            else {
                targetScale = minScale;
            }
            targetRatio = targetScale / currentScale;
            mHandler.removeCallbacks(mUpdateImageScale);
            mHandler.post(mUpdateImageScale);          
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            System.out.println("onfling");
            return super.onFling(e1, e2, velocityX, velocityY);
        }

        @Override
        public boolean onDown(MotionEvent e) {
            System.out.println("ondown");
            return false;
        }



   }
   public Bitmap decodeFile(File f) {

        try {
            // decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            o.inDither=false;                     //Disable Dithering mode
            o.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
            o.inInputShareable=true; 
            o.inPreferredConfig = Bitmap.Config.ARGB_8888;
            //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
            o.inTempStorage=new byte[16*1024]; 

            BitmapFactory.decodeStream(new FileInputStream(f), null, o); 

            // Find the correct scale value. It should be the power of 2.
             int REQUIRED_SIZE = 300;

            int width_tmp = o.outWidth, height_tmp = o.outHeight; 

            if(REQUIRED_SIZE  > width_tmp)
            REQUIRED_SIZE  = width_tmp;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE
                        || height_tmp / 2 < REQUIRED_SIZE) 
                    break; 
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
                System.out.println(scale+"______________________________-");  
            }

            // decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();

            o2.inDither=false;
            o2.inScaled = false;

            o2.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
            o2.inInputShareable=true; 
            //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
            o2.inTempStorage=new byte[24*1024]; 
            o2.inSampleSize = 2; 
            o2.outWidth = width_tmp;
            o2.outHeight = height_tmp;
            o2.inPreferredConfig = Bitmap.Config.ARGB_8888;
            try {
                BitmapFactory.Options.class.getField("inNativeAlloc").setBoolean(o2,true);    

                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {  
                    e.printStackTrace();
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                }


            o2.inJustDecodeBounds = false;
            return BitmapFactory.decodeStream(new FileInputStream(f), null,  
                    o2);
        } 

        catch (FileNotFoundException e) {
            System.out.println("file not found");
        }
        return null;

    }
//   @Override
//protected void onDetachedFromWindow() {
//  // TODO Auto-generated method stub
//     System.out.println("ondetatched from wind");
//  super.onDetachedFromWindow();
//  if (imgBitmap != null) {
//  imgBitmap.recycle();
//  imgBitmap=null;}
//  
//}
   public void zoomout(){
       if(zom==true && trans<1.0f)
       {PageCurlView.zoom=0;

//         Intent i = new Intent();
//          i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//          i.setClass(getContext(), StandaloneExample.class);   
//          getContext().startActivity(i);


            if(imgBitmap!=null){
                this.setBackgroundResource(0);
                this.clearAnimation();
                this.setBackgroundResource(0);
                   this.clearAnimation();
            imgBitmap.recycle();
            imgBitmap = null;}
//          
            try {
                this.finalize();
            } catch (Throwable e) {
                // TODO Auto-generated catch block
                System.out.println("finalizer problem");   
                e.printStackTrace();
            }



       } 
   }

}

您可以将此类包含在您的包中,并使用

替换您的xml文件