I'm trying to implement a handle to scale a view in android. Instead of using something like multitouch I want to be able to resize an image with just one finger.
Here is my activity code. I feel as though I am very close there are a five things that don't work properly.
- The scaling is off. It grows at a much faster rate than it should. Solved Thanks @Salauyou
- The view will only grow, and not shrink. Solved Thanks @Salauyou
- The handle view doesn't move with the image. Solved Thanks @Salauyou
- The scaling starts extremely small Solved Thanks @Salauyou
- The handle doesn't follow your finger exactly.
I am looking for any help that could implement such a feature. Whether it's a library or someone can help with my code that I already have. I have found a library that helps with multi touch scaling of images (https://github.com/brk3/android-multitouch-controller) but the only pointer I could pick up was how to go about implementing the increase in scale. And this has to be done through using two points, and finding the distance between them.
My java activity:
public class MainActivity extends Activity {
ImageView imageView;
ImageView dragHandle;
RelativeLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setBackgroundColor(Color.MAGENTA);
dragHandle = (ImageView) findViewById(R.id.imageView2);
dragHandle.setBackgroundColor(Color.CYAN);
layout = (RelativeLayout) findViewById(R.id.relativeLayout2);
layout.setBackgroundColor(Color.YELLOW);
setUpResize();
}
public void setUpResize() {
dragHandle.setOnTouchListener(new View.OnTouchListener() {
int[] touchPoint = new int[2];
int[] centerOfImage = new int[2];
double originalDistance = 0;
double modifiedDistance = 0;
float originalScale = 0;
float modifiedScale = 0;
public boolean onTouch(View v, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
centerOfImage[0] = (int) (imageView.getX() + imageView.getWidth() / 2);
centerOfImage[1] = (int) (imageView.getY() + imageView.getHeight() / 2);
touchPoint[0] = (int) motionEvent.getRawX();
touchPoint[1] = (int) motionEvent.getRawY();
int[] p = new int[2];
p[0] = touchPoint[0] - centerOfImage[0];
p[1] = touchPoint[1] - centerOfImage[1];
originalDistance = (float) Math.sqrt(p[0] * p[0] + p[1] * p[1]);
originalScale = imageView.getScaleX();
} else if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
touchPoint[0] = (int) motionEvent.getRawX();
touchPoint[1] = (int) motionEvent.getRawY();
int[] p = new int[2];
p[0] = (touchPoint[0] + p[0] - centerOfImage[0]);
p[1] = (touchPoint[1] + p[1] - centerOfImage[1]);
modifiedDistance = Math.hypot(touchPoint[0] - centerOfImage[0], touchPoint[1] - centerOfImage[1]);
Log.e("resize", "original " + imageView.getWidth() + " modified: " + imageView.getHeight());
modifiedScale = (float) (modifiedDistance / originalDistance * originalScale);
imageView.setScaleX(modifiedScale);
imageView.setScaleY(modifiedScale);
dragHandle.setX(centerOfImage[0] + imageView.getWidth()/2 * modifiedScale);
dragHandle.setY(centerOfImage[1] + imageView.getHeight()/2 * modifiedScale);
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
}
return true;
}
});
}
}
My xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@+id/imageView1"
android:layout_toRightOf="@+id/imageView1"
android:src="@drawable/dragArrow" />
</RelativeLayout>
</RelativeLayout>