如何在 Android 中从 4 边更改视图的大小? [英] How to change view's size from 4 side in Android?

查看:21
本文介绍了如何在 Android 中从 4 边更改视图的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过拉动视图 4 侧的矩阵来调整大小?我可以将单个点的大小调整为这样的比例.

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.

上面的例子如下:

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?

推荐答案

您可以尝试以下.这个想法是,如果您只有水平或只有垂直比例,那么您必须为所需轴提供实际比例,为另一个轴提供 1.检查刻度是水平还是垂直有点棘手,您可以检查:

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 第一次触摸靠近视图的垂直"边界.比滚动是水平的(x 轴).否则为垂直(y 轴).

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

2 如果您有按钮可拖动,如屏幕截图所示,这会让事情变得更简单:您只需要记住缩放是用哪些按钮启动的.

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);

这篇关于如何在 Android 中从 4 边更改视图的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆