ome*_*ega 23 animation android button shake
我正在制作一个Android应用程序,我有一个通向消息传递位置的按钮.在使用按钮的活动中,我检查是否有任何未读消息,如果是,我想对按钮做一些事情让用户知道有些东西未读.
我想让按钮sorta水平振动,每2或3秒3次震动.
我知道如何在后台运行一个每隔x毫秒做一次的线程.但我不知道该做什么是水平摇动3次.
有人能帮忙吗?
我正在考虑使用sin函数,对于动画,我可以使用sin函数的输出来获取上下的值,我可以设置按钮的水平位置......但这似乎太极端了,是还有更好的方法吗?
谢谢.
use*_*030 26
我不能对@ omega的评论发表评论,因为我的声誉不够,但问题的答案应该是这样的:
shake.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="100" <!-- how long the animation lasts -->
android:fromDegrees="-5" <!-- how far to swing left -->
android:pivotX="50%" <!-- pivot from horizontal center -->
android:pivotY="50%" <!-- pivot from vertical center -->
android:repeatCount="10" <!-- how many times to swing back and forth -->
android:repeatMode="reverse" <!-- to make the animation go the other way -->
android:toDegrees="5" /> <!-- how far to swing right -->
Run Code Online (Sandbox Code Playgroud)
Class.java
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
view.startAnimation(shake);
Run Code Online (Sandbox Code Playgroud)
这只是做你想做的事的一种方式,可能有更好的方法.
RVG*_*RVG 23
在anim文件夹中创建shake.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="10"
android:duration="1000"
android:interpolator="@anim/cycle" />
Run Code Online (Sandbox Code Playgroud)
和anim文件夹中的cycle.xml
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="4" />
Run Code Online (Sandbox Code Playgroud)
现在在代码上添加动画
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
anyview.startAnimation(shake);
Run Code Online (Sandbox Code Playgroud)
如果需要垂直动画,请将Xdelta和Xdelta值更改为fromYdelta和Ydelta值
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_with_the_button);
final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.milkshake);
Button myButton = (Button) findViewById(R.id.new_game_btn);
myButton.setAnimation(myAnim);
}
Run Code Online (Sandbox Code Playgroud)
对于按钮的onClick
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(myAnim);
}
});
Run Code Online (Sandbox Code Playgroud)
在res目录中创建anim文件夹
右键单击,res - >新建 - >目录
将新目录命名为anim
创建一个新的xml文件名,即奶昔
milkshake.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="100"
android:fromDegrees="-5"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="10"
android:repeatMode="reverse"
android:toDegrees="5" />
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
94014 次 |
最近记录: |