イメージビューをピンチしてズームするタスクがあります。イメージをスワイプするアプリを作成しましたが、正常に動作しています。今、私は同じクラスのイメージビューをピンチしてズームしたいのですが、これを行うことはできません.さまざまな例を見てきましたが、この場合は何も役に立たないようです.以下は私のコードです,または別の例を提供してくださいこの問題のために。
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
bitmap = BitmapFactory.decodeResource(context.getResources(), image_id[position]);
bmpWidth = bitmap.getWidth();
bmpHeight = bitmap.getHeight();
distCurrent = 1;
dist0 = 1;
drawMatrix();
float distx, disty;
switch(event.getAction() & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_DOWN:
//A pressed gesture has started, the motion contains the initial starting location.
touchState = TOUCH;
break;
case MotionEvent.ACTION_POINTER_UP:
//A non-primary pointer has gone up.
touchState = TOUCH;
break;
case MotionEvent.ACTION_POINTER_DOWN:
//A non-primary pointer has gone down.
touchState = PINCH;
//Get the distance when the second pointer touch
distx = event.getX(0) - event.getX(1);
disty = event.getY(0) - event.getY(1);
dist0 = FloatMath.sqrt(distx * distx + disty * disty);
break;
case MotionEvent.ACTION_MOVE:
//A change has happened during a press gesture (between ACTION_DOWN and ACTION_UP).
if(touchState == PINCH){
//Get the current distance
distx = event.getX(0) - event.getX(1);
disty = event.getY(0) - event.getY(1);
distCurrent = FloatMath.sqrt(distx * distx + disty * disty);
drawMatrix();
}
break;
case MotionEvent.ACTION_UP:
//A pressed gesture has finished.
touchState = IDLE;
break;
}
touchState = IDLE;
return true;
}
});
private void drawMatrix(){
float curScale = distCurrent/dist0;
if (curScale < 0.1){
curScale = 0.1f;
}
Bitmap resizedBitmap;
int newHeight = (int) (bmpHeight * curScale);
int newWidth = (int) (bmpWidth * curScale);
System.out.println("new width: "+newWidth+" new heigt: "+newHeight);
resizedBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false);
System.out.println("resized bitmap: "+resizedBitmap);
imageView.setImageBitmap(resizedBitmap);
}