Android 四种动画效果的调用实现代码
程序员文章站
2023-12-13 14:32:04
(1) main.xml 代码如下:(声明四个按钮控件) xml代码: 复制代码 代码如下:
(1) main.xml 代码如下:(声明四个按钮控件)
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<absolutelayout
android:id="@+id/widget32"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<textview
android:id="@+id/widget29"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:layout_x="0px"
android:layout_y="0px"
>
</textview>
<button
android:id="@+id/button_alpha"
android:layout_width="150px"
android:layout_height="150px"
android:text="alpha动画"
android:textsize="50px"
android:layout_x="0px"
android:layout_y="30px">
</button>
<button
android:id="@+id/button_scale"
android:layout_width="150px"
android:layout_height="150px"
android:text="scale动画"
android:textsize="50px"
android:layout_x="0px"
android:layout_y="180px">
</button>
<button
android:layout_width="150px"
android:layout_height="150px"
android:text="translate动画"
android:layout_x="161px"
android:layout_y="30px"
android:textsize="30px"
android:id="@+id/button_translate">
</button>
<button
android:id="@+id/button_rotate"
android:layout_width="150px"
android:layout_height="150px"
android:text="rotate动画"
android:layout_y="180px"
android:layout_x="161px"
android:textsize="44px">
</button>
</absolutelayout>
java代码:
import android.app.activity;
import android.os.bundle;
import android.view.view;
import android.view.view.onclicklistener;
import android.view.animation.animation;
import android.view.animation.animationutils;
import android.widget.button;
public class myactionanimation extends activity implements onclicklistener {
/** called when the activity is first created. */
private button button_alpha;
private button button_scale;
private button button_translate;
private button button_rotate;
private animation myanimation_alpha;
private animation myanimation_scale;
private animation myanimation_translate;
private animation myanimation_rotate;
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
button_alpha = (button) findviewbyid(r.id.button_alpha);
button_alpha.setonclicklistener(this);
button_scale = (button) findviewbyid(r.id.button_scale);
button_scale.setonclicklistener(this);
button_translate = (button) findviewbyid(r.id.button_translate);
button_translate.setonclicklistener(this);
button_rotate = (button) findviewbyid(r.id.button_rotate);
button_rotate.setonclicklistener(this);
}
public void onclick(view button) {
// todo auto-generated method stub
switch (button.getid()) {
case r.id.button_alpha: {
myanimation_alpha = animationutils.loadanimation(this,r.layout.my_alpha_action);
button_alpha.startanimation(myanimation_alpha);
}
break;
case r.id.button_scale: {
myanimation_scale= animationutils.loadanimation(this,r.layout.my_scale_action);
button_scale.startanimation(myanimation_scale);
}
break;
case r.id.button_translate: {
myanimation_translate= animationutils.loadanimation(this,r.layout.my_translate_action);
button_translate.startanimation(myanimation_translate);
}
break;
case r.id.button_rotate: {
myanimation_rotate= animationutils.loadanimation(this,r.layout.my_rotate_action);
button_rotate.startanimation(myanimation_rotate);
}
break;
default:
break;
}
}
}
效果图:
xml代码:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<absolutelayout
android:id="@+id/widget32"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<textview
android:id="@+id/widget29"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:layout_x="0px"
android:layout_y="0px"
>
</textview>
<button
android:id="@+id/button_alpha"
android:layout_width="150px"
android:layout_height="150px"
android:text="alpha动画"
android:textsize="50px"
android:layout_x="0px"
android:layout_y="30px">
</button>
<button
android:id="@+id/button_scale"
android:layout_width="150px"
android:layout_height="150px"
android:text="scale动画"
android:textsize="50px"
android:layout_x="0px"
android:layout_y="180px">
</button>
<button
android:layout_width="150px"
android:layout_height="150px"
android:text="translate动画"
android:layout_x="161px"
android:layout_y="30px"
android:textsize="30px"
android:id="@+id/button_translate">
</button>
<button
android:id="@+id/button_rotate"
android:layout_width="150px"
android:layout_height="150px"
android:text="rotate动画"
android:layout_y="180px"
android:layout_x="161px"
android:textsize="44px">
</button>
</absolutelayout>
java代码:
复制代码 代码如下:
import android.app.activity;
import android.os.bundle;
import android.view.view;
import android.view.view.onclicklistener;
import android.view.animation.animation;
import android.view.animation.animationutils;
import android.widget.button;
public class myactionanimation extends activity implements onclicklistener {
/** called when the activity is first created. */
private button button_alpha;
private button button_scale;
private button button_translate;
private button button_rotate;
private animation myanimation_alpha;
private animation myanimation_scale;
private animation myanimation_translate;
private animation myanimation_rotate;
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
button_alpha = (button) findviewbyid(r.id.button_alpha);
button_alpha.setonclicklistener(this);
button_scale = (button) findviewbyid(r.id.button_scale);
button_scale.setonclicklistener(this);
button_translate = (button) findviewbyid(r.id.button_translate);
button_translate.setonclicklistener(this);
button_rotate = (button) findviewbyid(r.id.button_rotate);
button_rotate.setonclicklistener(this);
}
public void onclick(view button) {
// todo auto-generated method stub
switch (button.getid()) {
case r.id.button_alpha: {
myanimation_alpha = animationutils.loadanimation(this,r.layout.my_alpha_action);
button_alpha.startanimation(myanimation_alpha);
}
break;
case r.id.button_scale: {
myanimation_scale= animationutils.loadanimation(this,r.layout.my_scale_action);
button_scale.startanimation(myanimation_scale);
}
break;
case r.id.button_translate: {
myanimation_translate= animationutils.loadanimation(this,r.layout.my_translate_action);
button_translate.startanimation(myanimation_translate);
}
break;
case r.id.button_rotate: {
myanimation_rotate= animationutils.loadanimation(this,r.layout.my_rotate_action);
button_rotate.startanimation(myanimation_rotate);
}
break;
default:
break;
}
}
}
效果图: