首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android学习11-多媒体技术(二) Animation

2012-11-23 
Android学习11-----多媒体技术(2) AnimationNo.方法描述12345678package com.iflytek.demoimport android

Android学习11-----多媒体技术(2) Animation

No.

方法

描述

1

2

3

4

5

6

7

8

package com.iflytek.demo;import android.app.Activity;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.AnimationSet;import android.view.animation.RotateAnimation;import android.view.animation.ScaleAnimation;import android.view.animation.TranslateAnimation;import android.widget.ImageView;public class Animation01_Activity extends Activity {private ImageView image = null;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);this.image = (ImageView) super.findViewById(R.id.image);this.image.setOnClickListener(new OnClickListenerImpl());}private class OnClickListenerImpl implements OnClickListener {@Overridepublic void onClick(View v) {AnimationSet set = new AnimationSet(true);// -------------1、渐变效果------------------AlphaAnimation alpha = new AlphaAnimation(1, 0); // 由完全显示 --> 完全透明alpha.setDuration(3000); // 3秒完成动画// -------------2、缩放效果------------------ScaleAnimation scale = new ScaleAnimation(1, 0.0f, 1, 0.0f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);// xy轴从完全开始都不显示,xy轴以自身的0.5进行缩放scale.setDuration(3000); // 3秒完成动画// -------------3、平移效果------------------TranslateAnimation tran = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0.0f ,// X轴开始位置Animation.RELATIVE_TO_SELF,0.5f ,// X轴移动的结束位置Animation.RELATIVE_TO_SELF,0.0f ,// Y轴开始位置Animation.RELATIVE_TO_SELF,1.5f );// Y轴移动位置tran.setDuration(3000); // 3秒完成动画// -------------4、旋转效果------------------RotateAnimation rotate = new RotateAnimation(0,360 ,// 0~360度Animation.RELATIVE_TO_PARENT,0.5f ,// Y轴开始位置Animation.RELATIVE_TO_PARENT,0.0f );// Y轴移动位置rotate.setDuration(3000); // 3秒完成动画set.addAnimation(rotate); // 增加动画set.addAnimation(tran); // 增加动画set.addAnimation(scale); // 增加动画set.addAnimation(alpha); // 增加动画Animation01_Activity.this.image.startAnimation(set); // 启动动画}}}?

动画速率,Interpolator:每当实例化AnimationSet类对象的时候都会定义如下的一个构造方法AnimationSet set = new AnimationSet(true);//定义一个动画集

No.

子类

描述

1

2

3

4

5

package com.iflytek.demo;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.AccelerateInterpolator;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.ScaleAnimation;import android.view.animation.TranslateAnimation;import android.widget.ImageView;public class Animation02_Activity extends Activity {private ImageView image = null;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);this.image = (ImageView) super.findViewById(R.id.image);this.image.setOnClickListener(new OnClickListenerImpl());}private class OnClickListenerImpl implements OnClickListener {@Overridepublic void onClick(View v) {AnimationSet set = new AnimationSet(true);TranslateAnimation tran = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, // X轴开始位置Animation.RELATIVE_TO_SELF, 0.5f, // X轴移动的结束位置Animation.RELATIVE_TO_SELF, 0.0f, // Y轴开始位置Animation.RELATIVE_TO_SELF, 1.5f); // Y轴移动位置ScaleAnimation scale = new ScaleAnimation(1, 0.0f, 1, 0.0f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);scale.setRepeatCount(30);//重复set.addAnimation(tran); // 增加动画set.addAnimation(scale); // 增加动画set.setInterpolator(new AccelerateInterpolator()); // 加速度set.setDuration(6000); // 6秒完成动画Animation02_Activity.this.image.startAnimation(set); // 启动动画}}}?

动画结束时触发:public voidonAnimationEnd(Animation animation)

package com.iflytek.demo;import android.app.Activity;import android.os.Bundle;import android.view.ViewGroup;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;import android.view.animation.AnimationSet;import android.view.animation.TranslateAnimation;import android.widget.ImageView;public class Animation03_Activity extends Activity {private ImageView image = null;private ViewGroup group = null ; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.image = (ImageView) super.findViewById(R.id.image);this.group = (ViewGroup) super.findViewById(R.id.group);AnimationSet set = new AnimationSet(true);TranslateAnimation tran = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0.0f ,// X轴开始位置Animation.RELATIVE_TO_SELF,0.5f ,// X轴移动的结束位置Animation.RELATIVE_TO_SELF,0.0f ,// Y轴开始位置Animation.RELATIVE_TO_SELF,1.5f );// Y轴移动位置tran.setDuration(6000); // 6秒完成动画set.addAnimation(tran); // 增加动画set.setAnimationListener(new AnimationListenerImpl()) ;this.image.startAnimation(set); // 启动动画 } private class AnimationListenerImpl implements AnimationListener {@Overridepublic void onAnimationEnd(Animation animation) {Animation03_Activity.this.group.removeView(Animation03_Activity.this.image) ;}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic void onAnimationStart(Animation animation) {if(animation instanceof AnimationSet) {AnimationSet set = (AnimationSet) animation ;AlphaAnimation alpha = new AlphaAnimation(1, 0);alpha.setDuration(3000) ;set.addAnimation(alpha) ;}}}}?

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/group" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /></LinearLayout>?

No.

可配置的元素

描述

1

2

3

4

5

No.

可配置属性

描述

1

2

3

4

5

6

7

8

package com.iflytek.demo;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.ImageView;public class Animation04_Activity extends Activity {private ImageView image = null;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);this.image = (ImageView) super.findViewById(R.id.image);this.image.setOnClickListener(new OnClickListenerImpl());}private class OnClickListenerImpl implements OnClickListener {@Overridepublic void onClick(View v) {Animation anim = AnimationUtils.loadAnimation(Animation04_Activity.this, R.anim.all);Animation04_Activity.this.image.startAnimation(anim); // 启动动画}}}?

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_interpolator"android:shareInterpolator="true"><translateandroid:fromXDelta="0.0"android:toXDelta="50%"android:fromYDelta="0.0"android:toYDelta="150%"android:duration="3000" /><scaleandroid:fromXScale="1.0"android:toXScale="0.0"android:fromYScale="1.0"android:toYScale="0.0"android:pivotX="50%"android:pivotY="50%"android:startOffset="100"android:repeatCount="3"android:duration="3000" /></set>

?alpha.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><alphaandroid:fromAlpha="1.0"android:toAlpha="0.0"android:duration="3000" /></set>

?rotate.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><rotateandroid:fromDegrees="0.0"android:toDegrees="+360.0"android:pivotX="50%p"android:pivotY="0%p"android:duration="3000" /></set>

?scale.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><scaleandroid:fromXScale="1.0"android:toXScale="0.0"android:fromYScale="1.0"android:toYScale="0.0"android:pivotX="50%"android:pivotY="50%"android:startOffset="100"android:repeatCount="3"android:duration="3000" /></set>

?translate.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:fromXDelta="0.0"android:toXDelta="50%"android:fromYDelta="0.0"android:toYDelta="150%"android:duration="3000" /></set>
?

package com.iflytek.demo;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class Animation05_Activity extends Activity {private ImageView dingqiu = null;private Button start = null;private AnimationDrawable draw = null;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);this.dingqiu = (ImageView) super.findViewById(R.id.dingqiu);this.start = (Button) super.findViewById(R.id.start);this.start.setOnClickListener(new OnClickListenerImpl());}private class OnClickListenerImpl implements OnClickListener {@Overridepublic void onClick(View v) {Animation05_Activity.this.dingqiu.setBackgroundResource(R.anim.dingqiu);Animation05_Activity.this.draw = (AnimationDrawable) Animation05_Activity.this.dingqiu.getBackground();Animation05_Activity.this.draw.setOneShot(false);Animation05_Activity.this.draw.start();}}}?

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="true"><item android:drawable="@drawable/dingqiu0001" android:duration="200" /><item android:drawable="@drawable/dingqiu0002" android:duration="200" /><item android:drawable="@drawable/dingqiu0003" android:duration="200" /><item android:drawable="@drawable/dingqiu0004" android:duration="200" /><item android:drawable="@drawable/dingqiu0005" android:duration="200" /><item android:drawable="@drawable/dingqiu0006" android:duration="200" /><item android:drawable="@drawable/dingqiu0007" android:duration="200" /><item android:drawable="@drawable/dingqiu0008" android:duration="200" /><item android:drawable="@drawable/dingqiu0009" android:duration="200" /></animation-list>?

package com.iflytek.demo;import android.app.Activity;import android.os.Bundle;import android.widget.GridView;public class Animation06_Activity extends Activity {private GridView myGridView = null;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);this.myGridView = (GridView) super.findViewById(R.id.myGridView);this.myGridView.setAdapter(new ImageAdapter(this));}}?

package com.iflytek.demo;import java.lang.reflect.Field;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.view.ViewGroup.LayoutParams;import android.widget.BaseAdapter;import android.widget.GridView;import android.widget.ImageView;public class ImageAdapter extends BaseAdapter {private Context context = null;private List<Integer> picRes = new ArrayList<Integer>();public ImageAdapter(Context context) {this.context = context;this.initPic();}@Overridepublic int getCount() {return this.picRes.size();}@Overridepublic Object getItem(int position) {return this.picRes.get(position);}@Overridepublic long getItemId(int position) {return this.picRes.get(position).intValue();}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ImageView img = new ImageView(this.context);img.setBackgroundColor(0xFF000000);img.setImageResource(this.picRes.get(position));img.setScaleType(ImageView.ScaleType.CENTER);img.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));img.setPadding(3, 3, 3, 3);return img;}private void initPic() { // 利用反射加载所有的图片Field[] fields = R.drawable.class.getDeclaredFields();for (int x = 0; x < fields.length; x++) {if (fields[x].getName().startsWith("dingqiu00")) {try {this.picRes.add(fields[x].getInt(R.drawable.class));} catch (IllegalArgumentException e) {} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}?

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><alphaandroid:fromAlpha="1.0"android:toAlpha="0.0"android:duration="3000" /><scaleandroid:fromXScale="1.0"android:toXScale="0.0"android:fromYScale="1.0"android:toYScale="0.0"android:pivotX="50%"android:pivotY="50%"android:startOffset="100"android:repeatCount="3"android:duration="3000" /></set>?

<?xml version="1.0" encoding="utf-8"?><layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"android:delay="0.5"android:animationOrder="random"android:animation="@anim/anim_set"/>

?main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <GridView        android:id="@+id/myGridView"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layoutAnimation="@anim/layout_animation"        android:numColumns="3"        android:stretchMode="columnWidth" /></LinearLayout>

?

ListView使用动画,通过配置文件
Animation07_Activity.java

package com.iflytek.demo;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.widget.ListView;import android.widget.SimpleAdapter;public class Animation07_Activity extends Activity {private ListView myListView = null;private String idData[] = new String[] { "xdwang", "java", "Android", "IBM" };private String titleData[] = new String[] { "王旭东", "java语言", "Android语言","国外" };private SimpleAdapter simple = null;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);this.myListView = (ListView) super.findViewById(R.id.myListView);List<Map<String, Object>> all = new ArrayList<Map<String, Object>>();Map<String, Object> map = null;for (int x = 0; x < this.idData.length; x++) {map = new HashMap<String, Object>();map.put("id", this.idData[x]);map.put("title", this.titleData[x]);all.add(map);}this.simple = new SimpleAdapter(this, all, R.layout.info, new String[] {"id", "data" }, new int[] { R.id.id, R.id.title });this.myListView.setAdapter(this.simple);}}

?info.xml

<?xml version="1.0" encoding="utf-8"?><TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TableRow><TextViewandroid:id="@+id/id"android:textSize="16px"android:layout_height="wrap_content"android:layout_width="100px" /><TextViewandroid:id="@+id/title"android:textSize="16px"android:layout_height="wrap_content"android:layout_width="200px" /></TableRow></TableLayout>

?main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><ListViewandroid:id="@+id/myListView" android:layout_width="fill_parent"android:layout_height="wrap_content" android:layoutAnimation="@anim/layout_animation"/></LinearLayout>

?

ListView使用动画, 程序实现LayoutAnimationController
Animation08_Activity.java

package com.iflytek.demo;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.view.animation.LayoutAnimationController;import android.widget.ListView;import android.widget.SimpleAdapter;public class Animation08_Activity extends Activity {private ListView myListView = null;private String idData[] = new String[] { "xdwang", "java", "Android", "IBM" };private String titleData[] = new String[] { "王旭东", "java语言", "Android语言","国外" };private SimpleAdapter simple = null;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);this.myListView = (ListView) super.findViewById(R.id.myListView);List<Map<String, Object>> all = new ArrayList<Map<String, Object>>();Map<String, Object> map = null;for (int x = 0; x < this.idData.length; x++) {map = new HashMap<String, Object>();map.put("id", this.idData[x]);map.put("title", this.titleData[x]);all.add(map);}this.simple = new SimpleAdapter(this, all, R.layout.info, new String[] {"id", "data" }, new int[] { R.id.id, R.id.title });this.myListView.setAdapter(this.simple);Animation anim = AnimationUtils.loadAnimation(this, R.anim.anim_set);LayoutAnimationController control = new LayoutAnimationController(anim);control.setDelay(0.5f);control.setOrder(LayoutAnimationController.ORDER_RANDOM);this.myListView.setLayoutAnimation(control);}}

?

将上面的main.xml中的添加动画取消

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><ListViewandroid:id="@+id/myListView" android:layout_width="fill_parent"android:layout_height="wrap_content" /></LinearLayout>
?

?

?

?

?

?

?

热点排行