I've a problem from last 2 days and unable to tackle it as I'm newbie. Actually I'm working on an Android App that needs pinch-zoom and 2-finger rotation on Android ImageView. I got the multiple tutorials and solutions that work fine for Pinch Zoom and but does not work for 2 finger rotation. I'm sharing the simplest tutorial that's easy to understand and I want to extend it for 2 finger rotation. Here is the code snippet:
public class MainActivity extends Activity {
private ImageView mImageView;
private Matrix mMatrix = new Matrix();
private float mScale = 1f;
private ScaleGestureDetector mScaleGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.imageView);
mScaleGestureDetector = new ScaleGestureDetector(this, new ScaleListener());
}
public boolean onTouchEvent(MotionEvent ev) {
mScaleGestureDetector.onTouchEvent(ev);
return true;
}
private class ScaleListener extends ScaleGestureDetector.
SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScale *= detector.getScaleFactor();
mScale = Math.max(0.1f, Math.min(mScale, 5.0f));
mMatrix.setScale(mScale, mScale);
mImageView.setImageMatrix(mMatrix);
return true;
}
}
}
Also I want to use them for GPUImage, I mean despite of Android ImageView I want to use GPUImage. How to transform the GPUImage to ImageView? This is the 2nd thing. First I want to implement the 2 finger rotation (or MultiTouch in some sense). Thanks
Tap anywhere on the screen, except the keyboard or navigation bar. Drag 2 fingers to move around the screen. Pinch with 2 fingers to adjust zoom. To stop magnification, use your magnification shortcut again.
Pinch-to-zoom on all devices may use algorithms, but only to scale the image — it doesn't change the content itself. This was an attempt to prevent the jury from getting a clearer view of the action, not a genuine challenge to the integrity of the video.
Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling.
Here is the solution that worked good for me. https://stackoverflow.com/a/18276033 Only one line I should add here and that should be
imageView.setRotation(imageView.getRotation() + (-angle));
in OnRotation(RotationGestureDetector rotationDetector)
method inside the activity to set the new rotation value to the ImageViewThis is for basic help. Remaining of the implementation is just fine
This is the library I created, which creates a imageview class can drag, rotate and zoom
Requirement: (Add on build.gradle)
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add Dependency
dependencies {
implementation 'com.github.lau1944:Zoom-Drag-Rotate-ImageView:1.0.0'
}
first , add image on xml:
<com.easystudio.rotateimageview.RotateZoomImageView
android:id="@+id/rotate"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/money"/>
or add programmatically:
RotateZoomImageView iv;
RelativeLayout playground = findViewById(R.id.playground);
iv = new RotateZoomImageView(getApplicationContext());
iv.setImageDrawable(getDrawable(R.drawable.money));
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(250, 250);
lp.addRule(RelativeLayout.BELOW);
iv.setLayoutParams(lp);
playground.addView(iv);
Step 2 : set up ontouchmethod
iv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return iv.onTouch(v,event);
}
});
And that is it .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With