我正在尝试创建一个无尽的可缩放环,像这样 -
每当用户进行缩放时,就会在中心创建新的小圆圈,并且外圈会变大。我正在使用pinch zoom in relative layout创建以下效果,但无法正确实现。我为此创建了一个存储库,如果您能提供帮助,请告诉我 https://github.com/rohankandwal/zoomable-growing-circles
更新:-
更改了提到的 stackoverflow 答案上的 dispatchDraw
方法 -
protected void dispatchDraw(Canvas canvas) {
Paint myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
myPaint.setStyle(Paint.Style.STROKE);
int strokeWidth = 4; // or whatever
myPaint.setStrokeWidth(strokeWidth);
myPaint.setColor(0xffff0000); //color.RED
float radius = (float) (0.5 * (width + height) * 2.5);
for (int i = 1; i <= 51; i=i+10) {
canvas.drawCircle(canvas.getWidth() / 2, canvas.getWidth() / 2, (radius) + mScaleFactor + i,
myPaint);
}
canvas.save();
super.dispatchDraw(canvas);
canvas.restore();
}
此代码允许圆圈增长 -
没有捏缩放的原始图像
捏合缩放时的图像
如您所见,缩放正在工作,但不知道缩放到一定级别时如何创建新圆圈。
最佳答案
如果我正确理解你想要什么,你的代码就没有太多需要改变的
在OnPinchListener
中:
public boolean onScale(ScaleGestureDetector detector) {
// 1 : no zoom
final float zoomLevel = detector.getCurrentSpan() / startingSpan;
zoomableRadarLayout.scale(zoomLevel, startFocusX, startFocusY);
return true;
}
和在ZoomableRadarLayout
中:
// current pinch
private float mScaleFactor = 1;
// previous pinches
private float mAccumulatedScaleFactor = 1;
protected void dispatchDraw(Canvas canvas) {
...
float radius = (float) (0.5 * (width + height) * 2.5);
// space between circles
int step = 10;
// radius of the outer circle
int outerRadius = (int) (radius * mScaleFactor * mAccumulatedScaleFactor);
// radius of the inner circle
int innerRadius = 0;
for (int i = outerRadius; i >= innerRadius; i = i - step) {
canvas.drawCircle(canvas.getWidth() / 2, canvas.getWidth() / 2, i, myPaint);
}
...
}
public void restore() {
// add the ending pinch to the previous ones
mAccumulatedScaleFactor = mAccumulatedScaleFactor * mScaleFactor;
mScaleFactor = 1;
this.invalidate();
}
顺便说一句,如果可能的话,建议在绘图时避免创建诸如 Paint
之类的绘图对象。例如将创建放入某个 init 方法中。
关于Android 可缩放无尽环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51553592/