Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change view's size from 4 side in Android?

Is it possible to resize by pulling the matrix on the 4 side of the view? I can resize from a single point to a ratio like this.

Resize View

The example above works as follows:

protected boolean onTouchDown(@NonNull MotionEvent event) {
  oldDistance = (float) Math.sqrt((midPoint.x-event.getX()) * (midPoint.x-event.getX()) + (midPoint.y-event.getY()) * (midPoint.y-event.getY()));
...
}

float newDistance = (float) Math.sqrt((midPoint.x-event.getX()) * (midPoint.x-event.getX()) + (midPoint.y-event.getY()) * (midPoint.y-event.getY()));
moveMatrix.set(downMatrix);
moveMatrix.postScale(newDistance / oldDistance, newDistance / oldDistance, midPoint.x,
        midPoint.y);
handlingSticker.setMatrix(moveMatrix);

But, for example, how can I make the process of expanding on the right side like below pictures with matrix?

first second

like image 802
Umut ADALI Avatar asked Dec 16 '18 20:12

Umut ADALI


People also ask

How to set layout for different screen size in Android?

The best way to create a responsive layout is to use ConstraintLayout as the base layout in your UI. ConstraintLayout enables you to specify the position and size of each view according to spatial relationships with other views in the layout. All the views can then move and resize together as the screen size changes.

What are two different ways to set the height and width of component in Android?

First get layout params using view. getLayoutParams(). Second then set the height and width value then last set the layout params of view. Save this answer.


1 Answers

You can try following. The idea is, if you have only horizontal or only vertical scale, then you must provide actual scale for desired axis and 1 for the other axis. Checking if the scale is horizontal or vertical is a bit tricky, you can check either:

1 The first touch was near the "vertical" border of your view. Than scroll is horizontal (x axis). Otherwise its vertical (y axis).

2 If you have buttons to drag from as in the screenshot, this makes things even easier: you just need to remember which buttons the scale was initiated with.

protected boolean onTouchDown(@NonNull MotionEvent event) {
  oldDistance = (float) Math.sqrt((midPoint.x-event.getX()) * (midPoint.x-event.getX()) + (midPoint.y-event.getY()) * (midPoint.y-event.getY()));
...
}

boolean isHorizonalScale = ... // Check if the drag has been started on the "vertical" side. If no, the scale is vertical.

moveMatrix.set(downMatrix);

if (isHorizonalScale) {
    float newDistance = (float) Math.abs(midPoint.x-event.getX());
    moveMatrix.postScale(newDistance / oldDistance, 1, midPoint.x, midPoint.y);
} else if (isHorizonalScale) {
    float newDistance = (float) Math.abs(midPoint.y-event.getY());
    moveMatrix.postScale(1, newDistance / oldDistance, midPoint.x, midPoint.y);
}
handlingSticker.setMatrix(moveMatrix);
like image 157
Anhayt Ananun Avatar answered Oct 15 '22 20:10

Anhayt Ananun