我有一个捏和缩放图像视图的任务,我已经制作了一个应用程序,我正在刷图像,它工作正常;现在我想在同一个类上捏合和缩放imageview;但是我无法做到这一点。我已经经历了各种各样的例子,但在这种情况下似乎没有任何帮助。下面是我的代码,或者提供给我另一个例子对于这个问题。
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);
}
答案 0 :(得分:1)
我必须将相同的功能集成到我的应用程序中。
但正如我们所知,在Android中无法实现可缩放的ImageView。
我使用了以下库: https://github.com/sephiroth74/ImageViewZoom
试试这个。可能会帮助你... :)