各种绚丽的动画效果,为你的App锦上添花吧
首先贴出Activity代码:
MainAcivity代码:
package com.example.thirdty_animation_android;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
private ImageView image;
private Button scale;
private Button rotate;
private Button translate;
private Button mix;
private Button alpha;
private Button continue_btn;
private Button continue_btn2;
private Button flash;
private Button move;
private Button change;
private Button layout;
private Button frame;
private AnimationDrawable animationDrawable1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.image);
scale = (Button) findViewById(R.id.scale);
rotate = (Button) findViewById(R.id.rotate);
translate = (Button) findViewById(R.id.translate);
alpha = (Button) findViewById(R.id.alpha);
continue_btn = (Button) findViewById(R.id.continue_btn);
continue_btn2 = (Button) findViewById(R.id.continue_btn2);
flash = (Button) findViewById(R.id.flash);
move = (Button) findViewById(R.id.move);
change=(Button) findViewById(R.id.change);
layout=(Button) findViewById(R.id.layout);
frame=(Button) findViewById(R.id.frame);
scale.setOnClickListener(this);
rotate.setOnClickListener(this);
translate.setOnClickListener(this);
alpha.setOnClickListener(this);
continue_btn.setOnClickListener(this);
continue_btn2.setOnClickListener(this);
flash.setOnClickListener(this);
move.setOnClickListener(this);
change.setOnClickListener(this);
layout.setOnClickListener(this);
frame.setOnClickListener(this);
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
Animation loadAnimation;
switch (view.getId()) {
case R.id.scale: {
loadAnimation = AnimationUtils.loadAnimation(this, R.anim.scale);
image.startAnimation(loadAnimation);
break;
}
case R.id.rotate: {
loadAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate);
image.startAnimation(loadAnimation);
break;
}
case R.id.translate: {
loadAnimation = AnimationUtils.loadAnimation(this, R.anim.translate);
image.startAnimation(loadAnimation);
break;
}
case R.id.continue_btn: {
loadAnimation = AnimationUtils.loadAnimation(this, R.anim.translate);
image.startAnimation(loadAnimation);
final Animation loadAnimation2 = AnimationUtils.loadAnimation(this,R.anim.rotate);
loadAnimation.setAnimationListener(new AnimationListener() {
//开始
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
//重复
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
//结束
@Override
public void onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub
image.startAnimation(loadAnimation2);
}
});
break;
}
case R.id.continue_btn2: {
loadAnimation = AnimationUtils.loadAnimation(this,R.anim.continue_anim);
image.startAnimation(loadAnimation);
break;
}
case R.id.alpha: {
loadAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);
image.startAnimation(loadAnimation);
break;
}
case R.id.move: {
TranslateAnimation translate = new TranslateAnimation(-50, 50,
0, 0);
translate.setDuration(1000);
translate.setRepeatCount(Animation.INFINITE);
translate.setRepeatMode(Animation.REVERSE);
image.startAnimation(translate);
break;
}
case R.id.flash: {
AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
alphaAnimation.setDuration(100);
alphaAnimation.setRepeatCount(10);
//倒序重复REVERSE 正序重复RESTART
alphaAnimation.setRepeatMode(Animation.REVERSE);
image.startAnimation(alphaAnimation);
break;
}
case R.id.change:
{
Intent intent=new Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
overridePendingTransition(R.anim.zoom_in,R.anim.zoom_out);
break;
}
case R.id.layout:
{
Intent intent=new Intent(MainActivity.this,ListActivity.class);
startActivity(intent);
break;
}
case R.id.frame:
{
image.setImageResource(R.drawable.anim_list);
animationDrawable1 = (AnimationDrawable) image.getDrawable();
animationDrawable1.start();
break;
}
}
}
}
跳转动画,Activity页面切换,第二个MainActivity代码:
package com.example.thirdty_animation_android;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity2 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
}
ListView动画演示,需要一个ListActivity布局,代码如下:
package com.example.thirdty_animation_android;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListActivity extends Activity{
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
listView=(ListView) findViewById(R.id.listView);
List<String>list=new ArrayList<String>();
for(int i=0;i<20;i++)
{
list.add("慕课网"+i);
}
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
LayoutAnimationController lac=new LayoutAnimationController(AnimationUtils.loadAnimation(this, R.anim.zoom_in));
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
listView.setLayoutAnimation(lac);
listView.startLayoutAnimation();
}
}
接下来是xml文件,不再以一一叙述,请自己理解:
main.xml对应MainActivity布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<Button
android:id="@+id/scale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:paddingBottom="5dp"
android:paddingTop="5sp"
android:text="ScaleAnimation(缩放动画)" />
<Button
android:id="@+id/alpha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:paddingBottom="5dp"
android:paddingTop="5sp"
android:text="AlphaAnimation(透明度动画)" />
<Button
android:id="@+id/rotate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:paddingBottom="5dp"
android:paddingTop="5sp"
android:text="RotateAnimation(旋转动画)" />
<Button
android:id="@+id/translate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:paddingBottom="5dp"
android:paddingTop="5sp"
android:text="TranslateAnimation(位移动画)" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/continue_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:paddingBottom="5dp"
android:paddingTop="5sp"
android:text="续播1" />
<Button
android:id="@+id/continue_btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:paddingBottom="5dp"
android:paddingTop="5sp"
android:text="续播2" />
<Button
android:id="@+id/flash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:paddingBottom="5dp"
android:paddingTop="5sp"
android:text="闪烁" />
<Button
android:id="@+id/move"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:paddingBottom="5dp"
android:paddingTop="5sp"
android:text="抖动" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:paddingBottom="5dp"
android:paddingTop="5sp"
android:text="切换动画" />
<Button
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:paddingBottom="5dp"
android:paddingTop="5sp"
android:text="布局动画" />
<Button
android:id="@+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:paddingBottom="5dp"
android:paddingTop="5sp"
android:text="逐帧动画" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:src="@drawable/anim_list" >
</ImageView>
</LinearLayout>
</LinearLayout>
main2.xml对应MainActivity布局,很简单,就添加了一张图片:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/back"
android:orientation="vertical" >
</LinearLayout>
最后一个是ListView的xml文件代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>