如何在android中旋转按钮视图

发布于 2024-12-12 02:03:17 字数 799 浏览 0 评论 0原文

我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

随遇而安 2024-12-19 02:03:18

如果您只想旋转按钮及其中的文本,请在按钮的 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

梦醒时光 2024-12-19 02:03:17

此链接可能有帮助

我相信您需要将 RotateAnimation 应用于视图,并将 fillAfter 设置为 true 以保持其角度。上面的示例适用于布局,但您可以将动画应用到视图(在您的例子中为按钮)。

This link may help.

I believe you need to apply RotateAnimation to the view, with fillAfter 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).

烈酒灼喉 2024-12-19 02:03:17

如果您想要一个稳定的旋转按钮,请使用以下扩展按钮。也许您需要 getWidth() / 2, getHeight() / 2

public class RotateButton extends Button{

    public RotateButton(Context context) {
        super(context);
    }

    public RotateButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();
        canvas.rotate(45, getWidth() / 2, getHeight() / 2);
        super.onDraw(canvas);
        canvas.restore();
    }

}

并在您的布局中:

<com.samples.myapp.ui.RotateButton
        android:layout_height="wrap_content" android:id="@+id/MyBtn"
        android:padding="5dip" android:textColor="@color/darkGreen"
        android:textSize="16dip" android:text="TextView"
        android:layout_width="wrap_content"></com.samples.myapp.ui.RotateButton>

if you want to have an stable rotated Button use the following extended Button. Maybe you need getWidth() / 2, getHeight() / 2:

public class RotateButton extends Button{

    public RotateButton(Context context) {
        super(context);
    }

    public RotateButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();
        canvas.rotate(45, getWidth() / 2, getHeight() / 2);
        super.onDraw(canvas);
        canvas.restore();
    }

}

and in your layout:

<com.samples.myapp.ui.RotateButton
        android:layout_height="wrap_content" android:id="@+id/MyBtn"
        android:padding="5dip" android:textColor="@color/darkGreen"
        android:textSize="16dip" android:text="TextView"
        android:layout_width="wrap_content"></com.samples.myapp.ui.RotateButton>
成熟的代价 2024-12-19 02:03:17

如果您乐意在 API >= 11 上使用动画,我会选择:

ObjectAnimator.ofFloat(buttonView, "rotation", 0, 45).start();

If you're happy to use an animation, on API >= 11, I would go for:

ObjectAnimator.ofFloat(buttonView, "rotation", 0, 45).start();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文