如何在画布中旋转线条

时间:2015-10-26 12:38:18

标签: java android canvas android-canvas

目前我正在尝试轮播我在line中绘制的canvas。我设法做到了,但是当我将它们旋转360°时,前一个位置的线不会消失,所以最后我不会只获得1行,而是整个圆圈。

这是我的代码:

此代码段是在启动应用

时启动的
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;

imageView1 = (ImageView) findViewById(R.id.imageView1);
btn = (Button) findViewById(R.id.button1);

Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
bmp = Bitmap.createBitmap(width, height, conf); // this creates a MUTABLE bitmap
canvas = new Canvas(bmp);
p = new Paint();
p.setColor(Color.RED);
imageView1.setImageBitmap(bmp);
p.setStrokeWidth(5);
canvas.drawLine(0, height/2, width, height/2, p);

这件作品 - 每秒都会更新。

canvas.rotate(refY, width/2, height/2);
canvas.drawLine(0, height/2, width, height/2, p);
btn.setText(Integer.toString(refY))

1 个答案:

答案 0 :(得分:4)

相应地保存并恢复画布:

canvas.save();
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); //clear everything drawn to the bitmap
canvas.rotate(refY, width/2, height/2);
canvas.drawLine(0, height/2, width, height/2, p);
canvas.restore();
btn.setText(Integer.toString(refY))