首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用android:rotation xml旋转按钮

使用android:rotation xml旋转按钮
EN

Stack Overflow用户
提问于 2015-01-01 12:54:48
回答 3查看 10.4K关注 0票数 3

我想要一个按钮(作为一个图像),以顺时针旋转只是为了UI。我不想旋转按钮onClick。只需在xml文件中旋转就足够了。android:旋转对我不起作用。

我的代码是

<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:background="@drawable/button" android:rotation="-90" android:id="@+id/button" android:layout_gravity="right|left" android:layout_centerVertical="true" android:layout_centerHorizontal="true" />

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-01-01 13:06:02

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="600"
    android:repeatMode="restart"
    android:repeatCount="infinite"
    android:interpolator="@android:anim/cycle_interpolator"/>

</set>

时钟-使用正toDegrees值

反时钟智慧-使用负toDegrees值

票数 5
EN

Stack Overflow用户

发布于 2016-04-08 16:43:50

您可以使用按钮OnClick中的以下代码简单地旋转按钮:ObjectAnimator.ofFloat(v,"rotation",0,360).start();

这里,'v‘是按钮视图。下面是一个示例:

代码语言:javascript
复制
button.setOnClickListener(new View.OnClickListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                ObjectAnimator.ofFloat(v, "rotation", 0f, 360f).start();     
                return false;
            }
        });
票数 2
EN

Stack Overflow用户

发布于 2015-01-01 13:05:26

创建一个XML文件,假设是rotation.xml,并将其放在一个名为anim的文件夹中(在res中)

打开该文件并编写以下代码:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotY="50%"
    android:repeatCount="infinite" //you can change it according to the duration you want it ti rotate
    android:duration="1200" />

然后在您的代码中,在OnCreate中执行以下操作:

代码语言:javascript
复制
button.startAnimation( 
    AnimationUtils.loadAnimation(activity, R.anim.rotation) );

希望它能帮上忙!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27728363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档