如何在android中旋转按钮视图
我想将 Button 视图旋转 45 度。为此,我编写了如下所示的代码。现在,按钮上的文本或标签正在旋转,而不是旋转按钮本身。但我希望 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;
}
}
I want to rotate a Button view by 45 degrees. For that I have written the code which is shown below. Now rather than rotating the Button itself, the text or label on the Button is getting rotated. But I want the Button to get rotated by 45 degrees. How can I accomplish this?
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您只想旋转按钮及其中的文本,请在按钮的 xml 文件中使用 android:rotation="您想要旋转的角度"
If you simply want to rotate the button and the text within it use android:rotation="angle by which you want to rotate" within the xml file for the button
此链接可能有帮助。
我相信您需要将
RotateAnimation
应用于视图,并将fillAfter
设置为 true 以保持其角度。上面的示例适用于布局,但您可以将动画应用到视图(在您的例子中为按钮)。This link may help.
I believe you need to apply
RotateAnimation
to the view, withfillAfter
set to true to keep it's angle. The above example works on the layout, but you could apply the animation to a view (in your case, the button).如果您想要一个稳定的旋转按钮,请使用以下扩展按钮。也许您需要
getWidth() / 2, getHeight() / 2
:并在您的布局中:
if you want to have an stable rotated Button use the following extended Button. Maybe you need
getWidth() / 2, getHeight() / 2
:and in your layout:
如果您乐意在 API >= 11 上使用动画,我会选择:
If you're happy to use an animation, on API >= 11, I would go for: