1 import android.content.Context; 2 import android.graphics.Canvas; 3 import android.graphics.Color; 4 import android.graphics.Paint; 5 import android.util.AttributeSet; 6 import android.view.View; 7 8 public class RotatingRect extends View { 9 11 private Paint p; // 画笔 12 private float degrees = 0; // 旋转角度 13 14 public RotatingRect(Context context, AttributeSet attrs, int defStyleAttr) { 15 super(context, attrs, defStyleAttr); 16 17 initProperties(); 18 } 19 20 public RotatingRect(Context context, AttributeSet attrs) { 21 super(context, attrs); 22 23 initProperties(); 24 } 25 26 public RotatingRect(Context context) { 27 super(context); 28 29 initProperties(); 30 } 31 33 private void initProperties(){ 34 p = new Paint(); 35 p.setColor(Color.RED); 36 } 37 39 @Override 40 public void draw(Canvas canvas) { 41 super.draw(canvas); 42 43 canvas.save(); 44 // canvas.rotate(degrees); 45 // 调整坐标 46 canvas.translate(200, 200); 47 // 设置,绕着中心点旋转 48 canvas.rotate(degrees, 50, 50); 49 canvas.drawRect(0, 0, 100, 100, p); 51 degrees ++; 52 canvas.restore(); 53 54 invalidate(); 55 } 57 }
时间: 2024-10-01 04:58:49