gpt4 book ai didi

android - 画一个以捏为中心的圆

转载 作者:行者123 更新时间:2023-11-30 03:21:29 25 4
gpt4 key购买 nike

我正在尝试制作一个自定义 View ,在该 View 的中心绘制一个圆圈(位置始终固定),我想使用捏合手势实现该圆圈的比例(我使用了 ScaleGestureDetector)。

我设法在自定义布局的中心绘制了圆圈,但我无法使用捏合手势正确实现缩放功能。

拜托,你能给我一个提示吗?

提前致谢!

Custom Image View

更新:这是我当前的代码。如果你测试它,你可以看到圆圈在捏合时移动,我希望圆圈始终位于 View 的中心:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

public class CoasterView extends View {
private static float MIN_ZOOM = 0;
private static float MAX_ZOOM = 5f;

private static float RADIO = 100f;


private float scaleFactor = 1.f;
private ScaleGestureDetector detector;


Paint paint;

public CoasterView(Context paramContext) {
super(paramContext);
detector = new ScaleGestureDetector(paramContext, new ScaleListener());

init();
}

public CoasterView(Context paramContext, AttributeSet paramAttributeSet) {
super(paramContext, paramAttributeSet);
detector = new ScaleGestureDetector(getContext(), new ScaleListener());

init();
}

public CoasterView(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
super(paramContext, paramAttributeSet, paramInt);
detector = new ScaleGestureDetector(getContext(), new ScaleListener());

init();
}

private void init() {
this.paint = new Paint();
this.paint.setColor(-16711936);
this.paint.setStrokeWidth(40.0F);
this.paint.setStyle(Paint.Style.FILL);
}

protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

canvas.save();
canvas.scale(scaleFactor, scaleFactor);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, RADIO, paint);
canvas.restore();

}




@Override
public boolean onTouchEvent(MotionEvent event) {
detector.onTouchEvent(event);

invalidate();
return true;

}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {

scaleFactor *= detector.getScaleFactor();
scaleFactor = Math.max(MIN_ZOOM, Math.min(scaleFactor, MAX_ZOOM));
invalidate();
return true;
}
}
}

最佳答案

您正在缩放整个 Canvas ,最好只将缩放应用于绘制圆的半径:

protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
//canvas.scale(scaleFactor, scaleFactor);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, RADIO * scaleFactor, paint);
canvas.restore();
}

如果你真的想缩放整个 Canvas (如果你要在 Canvas 上绘制其他东西,比如图像......),你需要重新居中圆圈:

protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.scale(scaleFactor, scaleFactor);
canvas.drawCircle(getWidth() / 2 / scaleFactor, getHeight() / 2 / scaleFactor, RADIO, paint);
canvas.restore();
}

关于android - 画一个以捏为中心的圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19062313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com