Android-旋转按钮内的图像 [英] Android - Rotate image inside the button

查看:70
本文介绍了Android-旋转按钮内的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单击按钮时旋转按钮内的图像.我只是尝试使用ImageView,但它可以工作,但是出于可访问性目的,我需要使用Button而不是ImageView.

I want to rotate the image inside the button on click of the button. I tried with just ImageView and it works but for accessibility purpose I need to use Button instead of ImageView.

点击前:

此处的向下箭头是按钮背景.

Here the down arrow is button background.

点击后:

单击按钮会显示更多数据,并且旋转背景箭头.如何在点击时设置动画和旋转按钮背景?

Click of button displays extra data and background arrow is rotated. How can I animate and rotate button background on click?

供参考的布局代码:

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical" >

            <LinearLayout
                android:id="@+id/cases_actions_row"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/case_action_item_1"
                    style="@style/card_action_button"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content" />

                <Button
                    android:id="@+id/case_action_item_2"
                    style="@style/card_action_button"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content" />

            </LinearLayout>

            <Button
                    android:id="@+id/cases_expand_button"
                    style="@style/card_action_button"
                    android:layout_height="20dp"
                    android:layout_width="20dp"
                    android:layout_alignParentEnd="true"
                    android:layout_centerVertical="true"
                    android:background="@drawable/ic_arrow" />

        </RelativeLayout>

推荐答案

最简单的方法是旋转整个按钮(正如弗兰克·斯坦因(Frank N. Stein)在评论中建议的那样,尽管ImageButton没有什么可以阻止您使用其他小部件).

The simplest method would be to rotate the entire button (and as Frank N. Stein suggested in the comments, an ImageButton is probably best suited, although there's nothing to stop you from using a different widget).

有几种旋转按钮的方法,但是ViewPropertyAnimator仍然是最简单的方法:

There are several ways to rotate the button, but a ViewPropertyAnimator is again likely the most straightforward:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        float deg = button.getRotation() + 180F;
        button.animate().rotation(deg).setInterpolator(new AccelerateDecelerateInterpolator());
    }
});

顺便说一句,如果您希望箭头反转其原始动画,则可以尝试:

By the way, if you want the arrow to reverse its original animation, you could instead try:

float deg = (button.getRotation() == 180F) ? 0F : 180F;

而不是float deg = button.getRotation() + 180F;

这篇关于Android-旋转按钮内的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆