android仿快圖瀏覽,圖片縮放移動效果

代碼是拿別人的改的 ,原來的有些BUG ,項目加載的都是網絡圖片 還沒弄左右滑動效果

 

yi 工具類

[java] package com.lin.image; 
 
 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Matrix; 
import android.graphics.PointF; 
import android.graphics.Rect; 
import android.util.FloatMath; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.widget.ImageView; 
 
public class MyImageView extends ImageView { 
    private    Matrix mMatrix; 
    private float bWidth;// 圖片寬度  
    private float bHeight; 
     
    private int dWidth;//屏幕寬度  
    private int  dHeight;//屏幕高度  
    private float initScale; 
    private float mScale; 
    private float scale; 
    ImageState mapState = new ImageState(); 
    private float oldDist; 
    PointF mStart = new PointF(); 
    private Bitmap mBitmap; 
    float[] values = new float[9]; 
     
    Matrix initMatrix; 
    Matrix mSavedMatrix; 
     
    public MyImageView(Context context) { 
        super(context); 
         this.setScaleType(ScaleType.MATRIX); 
    } 
    public void init(MotionEvent event){ 
        mStart.set(event.getX(), event.getY()); 
        mSavedMatrix.set(mMatrix); 
         
    } 
    float rate=1.0f; 
     // 刷新界面  
    public void setView() { 
        //rate=rate/10*9;  
        // UserUtils.log(TAG, "set view", "set view");  
        //mMatrix.postScale(scale,scale,0,0);  
        this.setImageMatrix(mMatrix); 
         
        Rect rect = this.getDrawable().getBounds(); 
        this.getImageMatrix().getValues(values); 
        bWidth = rect.width() * values[0]; 
        bHeight = rect.height() * values[0]; 
 
        mapState.left = values[2]; 
        mapState.top = values[5]; 
        mapState.right = mapState.left + bWidth; 
        mapState.bottom = mapState.top + bHeight; 
    } 
    private float s=0.9f; 
    public void setScale(){ 
     
        float sX = dWidth / 2; 
        float sY = dHeight / 2; 
 
       mMatrix.postScale(s, s, sX, sY); 
       setView(); 
    } 
    public void setScreenSize(Context context, int width, int height,Bitmap bitmap) { 
         mBitmap =bitmap; 
        dWidth = width; 
        dHeight = height; 
        setImageBitmap(mBitmap); 
//        gd = new GestureDetector(context, new LearnGestureListener());  
 
        bWidth = mBitmap.getWidth(); 
        bHeight = mBitmap.getHeight(); 
        // mView = (ImageView) findViewById(R.id.imageView);  
        float xScale = (float) dWidth / bWidth; 
        float yScale = (float) dHeight / bHeight; 
        mScale = xScale <= yScale ? xScale : yScale; 
      scale = mScale < 1 ? mScale : 1; 
        initScale = scale; 
        mMatrix = new Matrix(); 
        mSavedMatrix = new Matrix(); 
        System.out.println("dwidth==="+dHeight+"  bHeight===="+bHeight); 
        // 平移  
        mMatrix.postTranslate((dWidth – bWidth) / 2, (dHeight – bHeight) / 2); 
 
        float sX = dWidth / 2; 
        float sY = dHeight / 2; 
         
        mSavedMatrix.set(mMatrix); 
       mMatrix.postScale(scale, scale, sX, sY); 
        setView(); 
    } 
     
    /** 計算移動距離 */ 
    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); 
    } 
    float backScale; 
    //縮放  
    public void zoom(MotionEvent event) { 
 
        float newDist = spacing(event); 
        if((mapState.right-mapState.left)>4*dWidth&&newDist>oldDist) 
            return; 
        Log.e("lin","oldDist="+oldDist+",newDist=="+newDist); 
        if (newDist > 10f&&Math.abs((newDist-oldDist))>10f) { 
            scale = newDist / oldDist; 
            if (scale < 1) { 
                mMatrix.postScale(scale, scale, dWidth / 2, dHeight / 2); 
            } else { 
                mMatrix.postScale(scale, scale, dWidth / 2, dHeight / 2); 
            } 
            oldDist=newDist; 
        } 
 
        setView(); 
    } 
     
    /**
     * @return the oldDist
     */ 
    public float getOldDist(MotionEvent event) { 
        this.oldDist = this.spacing(event); 
        if (oldDist > 10f) { 
                mSavedMatrix.set(mMatrix); 
        } 
        backScale=oldDist; 
        Log.e("lin","oldDist="+oldDist); 
        return oldDist; 
    } 
    public void backScale(){ 
         scale = backScale / oldDist; 
         if (scale < 1) { 
          //   mMatrix.postScale(scale, scale, dWidth / 2, dHeight / 2);  
         } else { 
             if(mapState.right-mapState.left<=dWidth){ 
                  scale= dWidth/(mapState.right-mapState.left); 
             
                 mMatrix.postScale(scale, scale, dWidth / 2, dHeight / 2); 
                    float h=(dHeight-(mapState.bottom-mapState.top))/2; 
                    float w=(dWidth-(mapState.right-mapState.left))/2; 
                     mMatrix.postTranslate(w-mapState.left,h-mapState.top); 
             } 
         } 
         setView(); 
    } 
    public void backDrag(){ 
        if (mapState.left >= 0 || mapState.right <= dWidth 
               ||mapState.top >= 0 || mapState.bottom <= dHeight) { 
            float h=(dHeight-(mapState.bottom-mapState.top))/2; 
            float w=(dWidth-(mapState.right-mapState.left))/2; 
             mMatrix.postTranslate(w-mapState.left,h-mapState.top); 
             setView(); 
        } 
    } 
    //拖動  
    public void drag(MotionEvent event){ 
        //Log.i  
        Log.e("lin","left="+mapState.left+",right="+mapState.right 
                +",top="+mapState.top+",bottom="+mapState.bottom); 
 
        mMatrix.set(mSavedMatrix); 
        if ((mapState.left <= 0 || mapState.right >= dWidth) 
                && (mapState.top <= 0 || mapState.bottom >= dHeight)) { 
            mMatrix.postTranslate(event.getX() – mStart.x, event.getY() 
                    – mStart.y); 
        } else if (mapState.top <= 0 || mapState.bottom >= dHeight) { 
            mMatrix.postTranslate(0, event.getY() – mStart.y); 
        } else if (mapState.left <= 0 || mapState.right >= dWidth) { 
            mMatrix.postTranslate(event.getX() – mStart.x, 0); 
        } 
        else{ 
              mMatrix.postTranslate(event.getX() – mStart.x, event.getY()-mStart.y); 
        } 
        //mStart.x=event.getX();  
       // mStart.y=event.getY();  
        setView(); 
         
    } 
    private class ImageState { 
        private float left; 
        private float top; 
        private float right; 
        private float bottom; 
    } 

package com.lin.image;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.Rect;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

public class MyImageView extends ImageView {
 private    Matrix mMatrix;
    private float bWidth;// 圖片寬度
    private float bHeight;
   
    private int dWidth;//屏幕寬度
    private int  dHeight;//屏幕高度
    private float initScale;
    private float mScale;
    private float scale;
    ImageState mapState = new ImageState();
    private float oldDist;
    PointF mStart = new PointF();
    private Bitmap mBitmap;
    float[] values = new float[9];
   
    Matrix initMatrix;
    Matrix mSavedMatrix;
   
 public MyImageView(Context context) {
  super(context);
      this.setScaleType(ScaleType.MATRIX);
 }
    public void init(MotionEvent event){
        mStart.set(event.getX(), event.getY());
        mSavedMatrix.set(mMatrix);
       
    }
 float rate=1.0f;
  // 刷新界面
    public void setView() {
     //rate=rate/10*9;
        // UserUtils.log(TAG, "set view", "set view");
     //mMatrix.postScale(scale,scale,0,0);
        this.setImageMatrix(mMatrix);
       
        Rect rect = this.getDrawable().getBounds();
        this.getImageMatrix().getValues(values);
        bWidth = rect.width() * values[0];
        bHeight = rect.height() * values[0];

        mapState.left = values[2];
        mapState.top = values[5];
        mapState.right = mapState.left + bWidth;
        mapState.bottom = mapState.top + bHeight;
    }
    private float s=0.9f;
    public void setScale(){
   
        float sX = dWidth / 2;
        float sY = dHeight / 2;

       mMatrix.postScale(s, s, sX, sY);
       setView();
    }
    public void setScreenSize(Context context, int width, int height,Bitmap bitmap) {
      mBitmap =bitmap;
     dWidth = width;
        dHeight = height;
        setImageBitmap(mBitmap);
//        gd = new GestureDetector(context, new LearnGestureListener());

        bWidth = mBitmap.getWidth();
        bHeight = mBitmap.getHeight();
        // mView = (ImageView) findViewById(R.id.imageView);
        float xScale = (float) dWidth / bWidth;
        float yScale = (float) dHeight / bHeight;
        mScale = xScale <= yScale ? xScale : yScale;
      scale = mScale < 1 ? mScale : 1;
        initScale = scale;
        mMatrix = new Matrix();
        mSavedMatrix = new Matrix();
        System.out.println("dwidth==="+dHeight+"  bHeight===="+bHeight);
        // 平移
        mMatrix.postTranslate((dWidth – bWidth) / 2, (dHeight – bHeight) / 2);

        float sX = dWidth / 2;
        float sY = dHeight / 2;
       
        mSavedMatrix.set(mMatrix);
       mMatrix.postScale(scale, scale, sX, sY);
        setView();
    }
   
    /** 計算移動距離 */
    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);
    }
    float backScale;
    //縮放
    public void zoom(MotionEvent event) {

        float newDist = spacing(event);
     if((mapState.right-mapState.left)>4*dWidth&&newDist>oldDist)
      return;
        Log.e("lin","oldDist="+oldDist+",newDist=="+newDist);
        if (newDist > 10f&&Math.abs((newDist-oldDist))>10f) {
            scale = newDist / oldDist;
            if (scale < 1) {
                mMatrix.postScale(scale, scale, dWidth / 2, dHeight / 2);
            } else {
                mMatrix.postScale(scale, scale, dWidth / 2, dHeight / 2);
            }
            oldDist=newDist;
        }

        setView();
    }
   
    /**
     * @return the oldDist
     */
    public float getOldDist(MotionEvent event) {
        this.oldDist = this.spacing(event);
        if (oldDist > 10f) {
                mSavedMatrix.set(mMatrix);
        }
        backScale=oldDist;
        Log.e("lin","oldDist="+oldDist);
        return oldDist;
    }
    public void backScale(){
      scale = backScale / oldDist;
         if (scale < 1) {
          //   mMatrix.postScale(scale, scale, dWidth / 2, dHeight / 2);
         } else {
          if(mapState.right-mapState.left<=dWidth){
            scale= dWidth/(mapState.right-mapState.left);
         
           mMatrix.postScale(scale, scale, dWidth / 2, dHeight / 2);
                float h=(dHeight-(mapState.bottom-mapState.top))/2;
                float w=(dWidth-(mapState.right-mapState.left))/2;
             mMatrix.postTranslate(w-mapState.left,h-mapState.top);
          }
         }
    setView();
    }
    public void backDrag(){
        if (mapState.left >= 0 || mapState.right <= dWidth
               ||mapState.top >= 0 || mapState.bottom <= dHeight) {
        float h=(dHeight-(mapState.bottom-mapState.top))/2;
        float w=(dWidth-(mapState.right-mapState.left))/2;
     mMatrix.postTranslate(w-mapState.left,h-mapState.top);
     setView();
        }
    }
    //拖動
    public void drag(MotionEvent event){
     //Log.i
      Log.e("lin","left="+mapState.left+",right="+mapState.right
       +",top="+mapState.top+",bottom="+mapState.bottom);

        mMatrix.set(mSavedMatrix);
        if ((mapState.left <= 0 || mapState.right >= dWidth)
                && (mapState.top <= 0 || mapState.bottom >= dHeight)) {
            mMatrix.postTranslate(event.getX() – mStart.x, event.getY()
                    – mStart.y);
        } else if (mapState.top <= 0 || mapState.bottom >= dHeight) {
            mMatrix.postTranslate(0, event.getY() – mStart.y);
        } else if (mapState.left <= 0 || mapState.right >= dWidth) {
            mMatrix.postTranslate(event.getX() – mStart.x, 0);
        }
        else{
           mMatrix.postTranslate(event.getX() – mStart.x, event.getY()-mStart.y);
        }
        //mStart.x=event.getX();
       // mStart.y=event.getY();
        setView();
       
    }
    private class ImageState {
        private float left;
        private float top;
        private float right;
        private float bottom;
    }
}

 

 

二, Activity[java] package com.lin.image; 
 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.drawable.BitmapDrawable; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.MotionEvent; 
 
public class ImageScaleActivity extends Activity { 
    /** Called when the activity is first created. */ 
      MyImageView image; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        image=new MyImageView(this); 
        Bitmap bmp=((BitmapDrawable)getResources().getDrawable(R.drawable.totallist)).getBitmap(); 
        image.setScreenSize(this,getWindowManager().getDefaultDisplay().getWidth(), 
            getWindowManager().getDefaultDisplay().getHeight(),bmp); 
        setContentView(image); 
    } 
    private static final int DRAG=10; 
    private static final int  NULL=0; 
    private static final int SCALE=11; 
    private int mode; 
    private float mStartX; 
    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
        // TODO Auto-generated method stub  
        switch (event.getAction()) { 
        case  MotionEvent.ACTION_DOWN: 
            Log.i("lin","down"); 
            mode=DRAG; 
 
             mStartX = event.getRawX(); 
            image.init(event); 
            break; 
        case MotionEvent.ACTION_MOVE: 
            if(mode==SCALE) 
                image.zoom(event); 
            if(mode==DRAG){ 
                    image.drag(event); 
                      return true; 
            } 
            break; 
        case MotionEvent.ACTION_UP: 
            Log.i("lin","up"); 
            if(mode==SCALE){ 
                image.backScale(); 
                mode=NULL; 
            } 
            else if(mode==DRAG){ 
                image.backDrag(); 
            } 
            break; 
        case MotionEvent.ACTION_POINTER_1_DOWN: 
            Log.i("lin","down1"); 
            break; 
        case MotionEvent.ACTION_POINTER_2_DOWN: 
            Log.i("lin","down2"); 
            image.getOldDist(event); 
            mode=SCALE; 
            break; 
        case MotionEvent.ACTION_POINTER_2_UP: 
            Log.i("lin","up2"); 
 
        default: 
            break; 
        } 
 
        return super.onTouchEvent(event); 
    } 
    private float calculate(float x1,  float x2) { 
 
        float pz = x1 – x2;// 計算兩點間的距離  
        return pz; 
    } 

package com.lin.image;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;

public class ImageScaleActivity extends Activity {
    /** Called when the activity is first created. */
   MyImageView image;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        image=new MyImageView(this);
        Bitmap bmp=((BitmapDrawable)getResources().getDrawable(R.drawable.totallist)).getBitmap();
        image.setScreenSize(this,getWindowManager().getDefaultDisplay().getWidth(),
         getWindowManager().getDefaultDisplay().getHeight(),bmp);
        setContentView(image);
    }
    private static final int DRAG=10;
    private static final int  NULL=0;
    private static final int SCALE=11;
    private int mode;
    private float mStartX;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
     // TODO Auto-generated method stub
     switch (event.getAction()) {
     case  MotionEvent.ACTION_DOWN:
      Log.i("lin","down");
      mode=DRAG;

          mStartX = event.getRawX();
            image.init(event);
      break;
     case MotionEvent.ACTION_MOVE:
      if(mode==SCALE)
       image.zoom(event);
      if(mode==DRAG){
                    image.drag(event);
                      return true;
      }
      break;
     case MotionEvent.ACTION_UP:
      Log.i("lin","up");
      if(mode==SCALE){
       image.backScale();
       mode=NULL;
      }
      else if(mode==DRAG){
       image.backDrag();
      }
      break;
     case MotionEvent.ACTION_POINTER_1_DOWN:
      Log.i("lin","down1");
      break;
     case MotionEvent.ACTION_POINTER_2_DOWN:
      Log.i("lin","down2");
      image.getOldDist(event);
      mode=SCALE;
      break;
        case MotionEvent.ACTION_POINTER_2_UP:
         Log.i("lin","up2");

  default:
   break;
  }

     return super.onTouchEvent(event);
    }
    private float calculate(float x1,  float x2) {

        float pz = x1 – x2;// 計算兩點間的距離
        return pz;
    }
}
截圖

 

 
 

摘自 dikeboy1234的專欄

發佈留言