单选按钮拖放,Android

时间:2014-07-03 08:24:33

标签: android xml

我在android xml中有自定义单选按钮,现在它看起来像这样:

1 http://i57.tinypic.com/2dvpx0w.png

在xml中我有两个选择器(一个用于同意另一个不同意):

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/agreechecked" android:state_checked="true"/>
<item android:drawable="@drawable/agreechecked" android:state_focused="true"/>
<item android:drawable="@drawable/agreeunchecked" android:state_pressed="false"/>
<item android:drawable="@drawable/agreeunchecked" android:state_checked="false"/>

</selector>

和此:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/disagreechecked" android:state_checked="true"/>
<item android:drawable="@drawable/disagreechecked" android:state_focused="true"/>
<item android:drawable="@drawable/disagreeunchecked" android:state_pressed="false"/>
<item android:drawable="@drawable/disagreeunchecked" android:state_checked="false"/>

</selector>

我希望能够点击同意电台并拖动白色圆圈以表示不同意并使其不同意。它不必是任何特殊的动画我只需要使它成为可能。到目前为止它只是通过点击同意或不同意来工作。我需要拖放。感谢。

1 个答案:

答案 0 :(得分:2)

你可以使用AnimationDrawable。创建一个xml文件并将其放在res / drawable文件夹中。在此xml文件中写下以下代码。

<animation-list android:id="@+id/selected" android:oneshot="false">
    <item android:drawable="@drawable/radio0" android:duration="100" />
    <item android:drawable="@drawable/radio1" android:duration="100" />
    <item android:drawable="@drawable/radio2" android:duration="100" />
    <item android:drawable="@drawable/radio3" android:duration="100" />
    <item android:drawable="@drawable/radio4" android:duration="100" />
    <item android:drawable="@drawable/radio5" android:duration="100" />
</animation-list>

这里是radio0,radio1,很快就是radio0左边有白色圆圈的图像,在radio1中稍微右边,在radio2中稍微向右,在radio5中很快就在右边。

然后你就可以开始动画了。

ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
img.setBackgroundResource(R.drawable.spin_animation);

// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

// Start the animation (looped playback by default).
frameAnimation.start();

根据您在xml文件中设置的持续时间,您可以相应地启动计时器(此处为600)以停止动画。因此,当您到达最后一张图像时,动画会停止。你可以按相反的顺序做同样的事情。