OnTouchListener を追加した ImageView があるので、2 本の指でピンチ ジェスチャを使用して後者をズームインおよびズームアウトできます。以下のコードを使用していますが、実際のビットマップのサイズ変更に問題があります。後者はぼやけ、同じサイズのままで、画像のサイズ変更されたバージョンでオーバーレイされます。これは、新しい画像を古い画像の上に置き換えるのではなく、単に重ねているかのようです。drawMatrix メソッドに問題があると思います...
int touchState;
final int IDLE = 0;
final int TOUCH = 1;
final int PINCH = 2;
float dist0, distCurrent;
public boolean onTouch(View view, MotionEvent event) {
boolean handledHere = false;
float distx, disty;
final int action = event.getAction();
switch(action & 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_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((ImageView) view);
}
break;
case MotionEvent.ACTION_UP:
//A pressed gesture has finished.
touchState = IDLE;
break;
case MotionEvent.ACTION_POINTER_UP:
//A non-primary pointer has gone up.
touchState = TOUCH;
break;
}
return handledHere;
}
private void drawMatrix(ImageView view){
float curScale = distCurrent/dist0;
if (curScale < 0.1){
curScale = 0.1f;
}
view.buildDrawingCache();
Bitmap originalBitmap = view.getDrawingCache();
Bitmap resizedBitmap;
int newHeight = (int) (view.getHeight() * curScale);
int newWidth = (int) (view.getWidth() * curScale);
resizedBitmap = Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, false);
view.setImageBitmap(resizedBitmap);
}
何度か拡大縮小するとこんな感じに・・・
私が得ることができる助けをありがとう。