gpt4 book ai didi

android - 在 Canvas Android 中随机移动网格路径上的球

转载 作者:行者123 更新时间:2023-12-04 15:34:36 25 4
gpt4 key购买 nike

要求是让球在Canvas中生成的Grid路径上一直运动。我在 Canvas 中生成了一个网格,但无法理解如何随机移动球意味着路径上的起点显示不同。我正在分享我所做的。我还在屏幕上绘制了球,但不知道如何将球随机准确地放在网格线上并开始移动它

public class PixelGridView extends View {

//number of row and column
int horizontalGridCount = 11;

private Drawable horiz;
private Drawable vert;
private final float width;
long mInterpolateTime;
PointF mImageSource = new PointF();


public PixelGridView(@NonNull Context context) {
this(context, null);
}

public PixelGridView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
horiz = new ColorDrawable(Color.WHITE);
horiz.setAlpha(160);
vert = new ColorDrawable(Color.WHITE);
vert.setAlpha(160);
width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, .9f, context.getResources().getDisplayMetrics());
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
horiz.setBounds(left, 0, right, (int) width);
vert.setBounds(0, top, (int) width, bottom);
}


private float getLinePosition(int lineNumber) {
int lineCount = horizontalGridCount;
return (1f / (lineCount + 1)) * (lineNumber + 1f);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//drawTask.start();
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
canvas.drawCircle(120, 110, 10, paint);

int count = horizontalGridCount;

for (int n = 0; n < count; n++) {
float pos = getLinePosition(n);
// Draw horizontal line
canvas.translate(0, pos * getHeight());
Log.e("Position1", "" + pos * getHeight());
horiz.draw(canvas);
canvas.translate(0, -pos * getHeight());
// Draw vertical line
canvas.translate(pos * getHeight(), 0);
Log.e("Position2", "" + pos * getHeight());
vert.draw(canvas);
canvas.translate(-pos * getHeight(), 0);
}
}
}[![Canvas Image][1]][1]

//MainActivity
public class PathAnimationActivity extends AppCompatActivity {

LinearLayout rlLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_path);
rlLayout=findViewById(R.id.rlLayout);

PixelGridView pixelGrid = new PixelGridView(this);
rlLayout.addView(pixelGrid);

}

}

enter image description here

最佳答案

首先我注意到你没有使用 invalidate ();最后,因为这对于动画 Canvas (重绘帧)至关重要,所以请包括在内。

可能有几种方法可以实现你想要的以下是我的想法这个 Canvas 需要分成多个 x , y 分割平面如下,并将它们保存在可以随机化的点数组中,并将这些点赋予球移动,

第一步,获取 Canvas 大小第 2 步,划分在 x 和 y 坐标中,具体取决于每个设备的大小,因此您需要通过屏幕大小来控制该因素第三步,将坐标保存在矩阵或数组中第 4 步,根据这些数组值设置球的位置(您可以根据坐标划分中 x 和 y 的最大值和最小值随机定义随机限制。

例如,函数 move 将采用 ball 对象,x, y 是位置 move (ball, x, y);并且您可以根据坐标划分示例的最大和最小限制随机化 x 和 y,例如总 y 行和总 x 行值

为了了解如何在 Canvas 上移动球,您可以在此处查看此代码:https://github.com/pintspin/ball_animation

enter image description here

关于android - 在 Canvas Android 中随机移动网格路径上的球,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60163854/

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