Android之旋转button视图

也许有时候我们的项目需要旋转按钮本身,而不是文本或按钮上的标签旋转。下面写一个小方法去实现旋转45°:

public class MyButton extends Button { 

public float degrees; 
public float sWidth; 
public float sHeight; 

public MyButton(Context context) { 
    super(context); 
    // TODO Auto-generated constructor stub 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    // TODO Auto-generated method stub 

    canvas.save(); 
    canvas.rotate(45.0f); 
    super.onDraw(canvas); 

    canvas.restore(); 
} 

@Override 
protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
    // TODO Auto-generated method stub 
    super.onSizeChanged(w, h, oldw, oldh); 
    sWidth=w; 
    sHeight=h; 
} 
}

相关推荐