2016-06-21 12 views
0

私はズームインとズームアウトのためにカスタムリストビューを使います。
これは、カスタムリストビューです:android custom listview to zoom

public class CustomListView1 extends ListView { 

private static final int INVALID_POINTER_ID = -1; 
private int mActivePointerId = INVALID_POINTER_ID; 
private ScaleGestureDetector mScaleDetector; 

private float mScaleFactor = 1.f; 
private float maxWidth = 0.0f; 
private float maxHeight = 0.0f; 
private float mLastTouchX; 
private float mLastTouchY; 
private float mPosX; 
private float mPosY; 
private float width; 
private float height; 

public CustomListView1(Context context) { 
    super(context); 
    mScaleDetector = new ScaleGestureDetector(getContext(), 
      new ScaleListener()); 
} 

public CustomListView1(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    mScaleDetector = new ScaleGestureDetector(getContext(), 
      new ScaleListener()); 
} 

public CustomListView1(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    mScaleDetector = new ScaleGestureDetector(getContext(), 
      new ScaleListener()); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    width = MeasureSpec.getSize(widthMeasureSpec); 
    height = MeasureSpec.getSize(heightMeasureSpec); 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 

@Override 
public boolean onTouchEvent(@NonNull MotionEvent ev) { 
    super.onTouchEvent(ev); 
    final int action = ev.getAction(); 
    mScaleDetector.onTouchEvent(ev); 
    switch (action & MotionEvent.ACTION_MASK) { 
     case MotionEvent.ACTION_DOWN: { 
      final float x = ev.getX(); 
      final float y = ev.getY(); 

      mLastTouchX = x; 
      mLastTouchY = y; 

      mActivePointerId = ev.getPointerId(0); 
      break; 
     } 

     case MotionEvent.ACTION_MOVE: { 
      final int pointerIndex = ev.findPointerIndex(mActivePointerId); 
      final float x = ev.getX(pointerIndex); 
      final float y = ev.getY(pointerIndex); 
      final float dx = x - mLastTouchX; 
      final float dy = y - mLastTouchY; 

      mPosX += dx; 
      mPosY += dy; 

      if (mPosX > 0.0f) 
       mPosX = 0.0f; 
      else if (mPosX < maxWidth) 
       mPosX = maxWidth; 

      if (mPosY > 0.0f) 
       mPosY = 0.0f; 
      else if (mPosY < maxHeight) 
       mPosY = maxHeight; 

      mLastTouchX = x; 
      mLastTouchY = y; 

      invalidate(); 
      break; 
     } 

     case MotionEvent.ACTION_UP: { 
      mActivePointerId = INVALID_POINTER_ID; 
      break; 
     } 

     case MotionEvent.ACTION_CANCEL: { 
      mActivePointerId = INVALID_POINTER_ID; 
      break; 
     } 

     case MotionEvent.ACTION_POINTER_UP: { 
      final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; 
      final int pointerId = ev.getPointerId(pointerIndex); 
      if (pointerId == mActivePointerId) { 
       final int newPointerIndex = pointerIndex == 0 ? 1 : 0; 
       mLastTouchX = ev.getX(newPointerIndex); 
       mLastTouchY = ev.getY(newPointerIndex); 
       mActivePointerId = ev.getPointerId(newPointerIndex); 
      } 
      break; 
     } 
    } 

    return true; 
} 

@Override 
public void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    canvas.save(Canvas.MATRIX_SAVE_FLAG); 
    canvas.translate(mPosX, mPosY); 
    canvas.scale(mScaleFactor, mScaleFactor); 
    canvas.restore(); 
} 

@Override 
protected void dispatchDraw(@NonNull Canvas canvas) { 
    canvas.save(Canvas.MATRIX_SAVE_FLAG); 
    if (mScaleFactor == 1.0f) { 
     mPosX = 0.0f; 
     mPosY = 0.0f; 
    } 
    canvas.translate(mPosX, mPosY); 
    canvas.scale(mScaleFactor, mScaleFactor); 
    super.dispatchDraw(canvas); 
    canvas.restore(); 
    invalidate(); 
} 

private class ScaleListener extends 
     ScaleGestureDetector.SimpleOnScaleGestureListener { 
    @Override 
    public boolean onScale(ScaleGestureDetector detector) { 
     mScaleFactor *= detector.getScaleFactor(); 
     mScaleFactor = Math.max(1.0f, Math.min(mScaleFactor, 4.0f)); 
     maxWidth = width - (width * mScaleFactor); 
     maxHeight = height - (height * mScaleFactor); 
     invalidate(); 
     return true; 
    } 
} 

}

このリストビューは、ズームイン、ズームアウトすることができます。
このリストビューは、左上隅をズームするだけです。
ピンチの中心をどのようにズームして固定するのですか?

+1

:ちょうどこのコードの下部にScaleListenerクラスを置き換えますか? –

+0

リストビューには、URLからの画像ロードのリストが含まれています。 – abcd1234

+1

特定の画像を拡大したい場合は、listviewのonItemClickListener()を設定することができます。そして、選択した画像を差分でズームすることができます。アクティビティまたはポップアップで表示されます。リストビューをズームするのは少し複雑で、UI/UXの設計標準に従って信頼性がありません。 –

答えて

0

私は、他のZoomLayoutのものからコードを取り出し、それをコードに適用しました。あなたがリストビューをズームしたいのはなぜ

private class ScaleListener extends 
      ScaleGestureDetector.SimpleOnScaleGestureListener { 

     @Override 
     public boolean onScale(ScaleGestureDetector detector) { 
      float prevScaleFactor = mScaleFactor; 
      mScaleFactor *= detector.getScaleFactor(); 
      mScaleFactor = Math.max(1.0f, Math.min(mScaleFactor, 4.0f)); 
      maxWidth = width - (width * mScaleFactor); 
      maxHeight = height - (height * mScaleFactor); 

      float adjustedScaleFactor = mScaleFactor/prevScaleFactor; 
      float focusX = detector.getFocusX(); 
      float focusY = detector.getFocusY(); 
      mPosX += (mPosX - focusX) * (adjustedScaleFactor - 1); 
      mPosY += (mPosY - focusY) * (adjustedScaleFactor - 1); 

      invalidate(); 
      return true; 
     } 
    } 
+0

ありがとうございました! – abcd1234