我有 1 个疑问和 1 个问题。
我想在游戏中绘制一条扩展的弧线,到目前为止,我已经设法使其逐渐扩展,但在某个点之后,弧线停止扩展并消失,这是为什么?
这是我的弧线代码:
public void render(Canvas canvas) {
if(gameState == TESTMODE){
drawMainBg(canvas);
hModeRadius += 50*thread.getDelta();
Paint p = new Paint();
RectF rectF = new RectF(hModeY - hModeRadius, hModeX + hModeRadius, hModeX + hModeRadius, hModeY-hModeRadius);
p.setColor(Color.WHITE);
canvas.drawArc (rectF, 0, 360, true, p);
}
现在我的第二个也是主要问题是,有没有办法用位图填充这个弧线?我想使用此弧线来更改游戏中的背景,并且我希望背景以弧线形式展开,直到填满屏幕。
最佳答案
您可以使用BitmapShader
来实现此目的:
// Define a method to generate a bitmap shader from a drawable resource
private static Shader getDrawableShader(Context ctx, int resId) {
Bitmap bmp = BitmapFactory.decodeResource(ctx.getResources(), resId);
return new BitmapShader(bmp, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
}
// Outside of onDraw(), preferably on a background thread if possible
Shader shader = getDrawableShader(getContext(), R.drawable.my_image);
// In onDraw()
paint.setShader(shader);
canvas.drawArc(rectF, 0, 360, true, paint);
关于java - 在生长的弧内绘制位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21564295/