Android 为什么仍然无法显示重置图像

Android 为什么仍然无法显示重置图像,android,image,pinchzoom,Android,Image,Pinchzoom,我正在开发一个壁纸应用程序。当我缩放一幅图像并单击“下一步”按钮查看下一幅图像时,下一幅图像将以上一幅图像剩余的缩放级别显示 你可以看到下面的图片和我的代码 这是正常状态下的第一张图像。 当我缩放图像时 当我单击“下一步”按钮更改图像时,图像被放大 图像应该是这样的,不需要放大 这里是xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://sch

我正在开发一个壁纸应用程序。当我缩放一幅图像并单击“下一步”按钮查看下一幅图像时,下一幅图像将以上一幅图像剩余的缩放级别显示

你可以看到下面的图片和我的代码

这是正常状态下的第一张图像。 当我缩放图像时

当我单击“下一步”按钮更改图像时,图像被放大

图像应该是这样的,不需要放大

这里是xml

 <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent" 
       android:gravity="fill"
       android:orientation="vertical"
       android:weightSum="100" > 
 <com.example.imagezoom.TouchImageView 
        android:id="@+id/idImageViewPic"
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:layout_weight="100" 
        android:adjustViewBounds="true" 
        android:background="#66FFFFFF"
        android:maxHeight="91dip"
        android:maxWidth="47dip"
        android:src="@drawable/a1" />
  <LinearLayout
        android:id="@+id/linearLayout1" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" > 
  <Button 
        android:id="@+id/bprev" 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Back" > 
       </Button> 
  <Button
        android:id="@+id/bnext"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Next" >
       </Button>
    </LinearLayout>
      </LinearLayout> 
触摸图像视图

  public class TouchImageView extends ImageView {
   Matrix matrix;
    // We can be in one of three states 
   static int NONE=0; 
   static int DRAG=1;
   static int ZOOM=2;
   int mode=NONE;
    // Remember some things for zooming
   PointF last = new PointF (); 
   PointF last = new PointF ();
   float minScale = 1f; 
   float maxScale = 2f; 
   float[] m;
   int viewWidth, viewHeight; 
   static 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 );
  }
  public void resetView () { 
  // we can be in one of three states 
  NONE = 0; 
  DRAG = 1; 
  ZOOM = 2;
  mode = NONE; 
   last = new PointF ( ); 
   start= new PointF ( );
   minScale = 1f;
   maxScale = 2f;
   CLICK =3;
   saveScale = 1f;
   matrix = new Matrix ();
   m = new float [ 9 ]; 
   setImageMatrix ( matrix ); 
   setScaleType ( ScaleType.MATRIX ); 
  } 
   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;
     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 ); 
   } 
 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 ( ); 
 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 getFixTrans (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 );
    // Rescaled 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 ) bmHeight / ( 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 ); 
   origWidth = viewWidth - 2 * redundantXSpace; 
   origHeight = viewHeight -2 * redundantYSpace; 
   setImageMatrix ( matrix );
  }
   fixTrans (); 
  } 
 }  
公共类TouchImageView扩展了ImageView{
矩阵;
//我们可能处于三种状态之一
静态int NONE=0;
静态阻力系数=1;
静态整数缩放=2;
int模式=无;
//记住一些关于缩放的事情
PointF last=新的PointF();
PointF last=新的PointF();
浮动最小刻度=1f;
浮点最大刻度=2f;
浮动[]m;
int viewWidth,viewHeight;
静态int CLICK=3;
浮动存储比例=1f;
受保护的浮子起始宽度、起始高度;
int oldMeasuredWidth,oldMeasuredHeight;
scalegestruedetector mScaleDetector;
语境;
公共TouchImageView(上下文){
超级(上下文);
共享构造(上下文);
} 
公共TouchImageView(上下文、属性集属性){
超级(上下文,attrs);
共享构造(上下文);
}
public void resetView(){
//我们可能处于三种状态之一
无=0;
阻力=1;
缩放=2;
模式=无;
last=newpointf();
开始=新的点f();
最小刻度=1f;
最大刻度=2f;
点击=3;
saveScale=1f;
矩阵=新矩阵();
m=新浮点数[9];
setImageMatrix(矩阵);
setScaleType(ScaleType.MATRIX);
} 
私有void sharedConstructing(上下文){
Super.setClickable(true);
this.context=上下文;
mscaledtector=newscalegesturedtector(上下文,newscalelistener());
矩阵=新矩阵();
m=新浮点数[9];
setImageMatrix(矩阵);
setScaleType(ScaleType.MATRIX);
setOnTouchListener(新的OnTouchListener(){
@凌驾
公共布尔OnTouch(视图v,运动事件){
mScaleDetector.onTouchEvent(事件);
PointF curr=new PointF(event.getX(),event.getY());
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
最后一组(当前);
start.set(最后一个);
模式=拖动;
打破
case MotionEvent.ACTION\u移动:
如果(模式==拖动){
浮动deltaX=当前x-最后x;
浮动三角洲=当前y-最后y;
float fixtranx=getfixtragtrans(deltaX、viewWidth、origWidth*saveScale);
float FixTransY=GetFixtragtrans(三角洲、视图高度、origHeight*保存比例);
matrix.postTranslate(Fixtranx,Fixtrany);
fixTrans();
最后一组(当前x、当前y);
} 
打破
case MotionEvent.ACTION\u UP:
模式=无;
intxdiff=(int)Math.abs(curr.x-start.x);
int yDiff=(int)Math.abs(curr.y-start.y);
if(xDiff最大比例){
saveScale=maxScale;
mScaleFactor=最大刻度/原始刻度;
}如果(saveScale如果(contentSize您没有在
按钮ChangeImageListener
中调用
重置视图()

基本上,您需要在
next或prev
onClick()中调用
resetView()

编辑: 1.您可能必须..在您的
main活动中
,您有
hImageViewPic=(ImageView)findViewById(R.id.idImageViewPic);
这应该是
hImageViewPic=(TouchImageView)findViewById(R.id.idImageViewPic);

  • (强制性) 在
    TouchView.java
    中,在
    iButtonChangeMageListener
    gButtonChangeImageListener
    中,必须添加行
    hImageViewPic.resetView()

  • Thanx bro.你能告诉我如何调用resetview吗?我是android新手,对此不太了解。先生,当我添加hImageViewPic.resetview()。然后resetview显示错误。plz post error log=堆栈跟踪..并且你是否将
    imageview
    键入为
    TouchImageView
    ?Aftab bro.Thanx很多。你已经解决了我的问题。
      public class TouchImageView extends ImageView {
       Matrix matrix;
        // We can be in one of three states 
       static int NONE=0; 
       static int DRAG=1;
       static int ZOOM=2;
       int mode=NONE;
        // Remember some things for zooming
       PointF last = new PointF (); 
       PointF last = new PointF ();
       float minScale = 1f; 
       float maxScale = 2f; 
       float[] m;
       int viewWidth, viewHeight; 
       static 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 );
      }
      public void resetView () { 
      // we can be in one of three states 
      NONE = 0; 
      DRAG = 1; 
      ZOOM = 2;
      mode = NONE; 
       last = new PointF ( ); 
       start= new PointF ( );
       minScale = 1f;
       maxScale = 2f;
       CLICK =3;
       saveScale = 1f;
       matrix = new Matrix ();
       m = new float [ 9 ]; 
       setImageMatrix ( matrix ); 
       setScaleType ( ScaleType.MATRIX ); 
      } 
       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;
         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 ); 
       } 
     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 ( ); 
     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 getFixTrans (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 );
        // Rescaled 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 ) bmHeight / ( 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 ); 
       origWidth = viewWidth - 2 * redundantXSpace; 
       origHeight = viewHeight -2 * redundantYSpace; 
       setImageMatrix ( matrix );
      }
       fixTrans (); 
      } 
     }