上一篇博客传送门:Android常见问题总结(三)
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot = "false" > <item android:drawable="@drawable/fat_po_f01" android:duration="60" /> <item android:drawable="@drawable/fat_po_f02" android:duration="60" /> <item android:drawable="@drawable/fat_po_f03" android:duration="60" /> <item android:drawable="@drawable/fat_po_f04" android:duration="60" /> <item android:drawable="@drawable/fat_po_f05" android:duration="60" /> <item android:drawable="@drawable/fat_po_f06" android:duration="60" /> <item android:drawable="@drawable/fat_po_f07" android:duration="60" /> <item android:drawable="@drawable/fat_po_f08" android:duration="60" /> <item android:drawable="@drawable/fat_po_f09" android:duration="60" /> <item android:drawable="@drawable/fat_po_f10" android:duration="60" /> <item android:drawable="@drawable/fat_po_f11" android:duration="60" /> <item android:drawable="@drawable/fat_po_f12" android:duration="60" /> <item android:drawable="@drawable/fat_po_f13" android:duration="60" /> <item android:drawable="@drawable/fat_po_f14" android:duration="60" /> <item android:drawable="@drawable/fat_po_f15" android:duration="60" /> <item android:drawable="@drawable/fat_po_f16" android:duration="60" /> <item android:drawable="@drawable/fat_po_f17" android:duration="60" /> <item android:drawable="@drawable/fat_po_f18" android:duration="60" /> <item android:drawable="@drawable/fat_po_f19" android:duration="60" /> <item android:drawable="@drawable/fat_po_f20" android:duration="60" /> <item android:drawable="@drawable/fat_po_f21" android:duration="60" /> <item android:drawable="@drawable/fat_po_f22" android:duration="60" /> <item android:drawable="@drawable/fat_po_f23" android:duration="60" /> <item android:drawable="@drawable/fat_po_f24" android:duration="60" /> <item android:drawable="@drawable/fat_po_f25" android:duration="60" /> <item android:drawable="@drawable/fat_po_f26" android:duration="60" /> <item android:drawable="@drawable/fat_po_f27" android:duration="60" /> </animation-list>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <ImageView android:id="@+id/image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@animator/fat_po"/> </LinearLayout>
public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageView = (ImageView)findViewById(R.id.image_view); // 开始动画,默认为停止 ((AnimationDrawable)imageView.getBackground()).start(); } }
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator"> <scale android:fromXScale="1.0" android:toXScale="0.01" android:fromYScale="1.0" android:toYScale="0.01" android:pivotX="50%" android:pivotY="50%" android:fillAfter="true" android:duration="3000"/> <alpha android:fromAlpha="1" android:toAlpha="0.05" android:duration="3000"/> <rotate android:fromDegrees="0" android:toDegrees="1800" android:pivotX="50%" android:pivotY="50%" android:duration="3000"/> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator"> <scale android:fromXScale="0.01" android:toXScale="1.0" android:fromYScale="0.01" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:fillAfter="true" android:duration="3000"/> <alpha android:fromAlpha="0.05" android:toAlpha="1" android:duration="3000"/> <rotate android:fromDegrees="1800" android:toDegrees="0" android:pivotX="50%" android:pivotY="50%" android:duration="3000"/> </set>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <ImageView android:id="@+id/image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/flower"/> </LinearLayout>
public class MyActivity extends Activity { private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { if (flag) imageView.startAnimation(close); else imageView.startAnimation(open); flag = !flag; }; }; private Animation open, close; private ImageView imageView; boolean flag = true; // 花瓣状态,true开,false合 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView)findViewById(R.id.image_view); // 花瓣张开 open = AnimationUtils.loadAnimation(this, R.anim.open); // 保留动画变化后的状态 open.setFillAfter(true); // 花瓣关闭 close = AnimationUtils.loadAnimation(this, R.anim.close); // 保留动画变化后的状态 close.setFillAfter(true); // 设置重复任务 new Timer().schedule(new TimerTask() { @Override public void run() { // 通过handler发送消息刷新UI handler.sendEmptyMessage(0); } }, 0, 3500); } }
public class MyApplication extends Application { public void exit() { System.exit(0); } }
public class ActivityC extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); Button button = (Button)findViewById(R.id.btn); button.setText("Finish app"); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 退出App ((MyApplication)getApplication()).exit(); } }); } }
public class BaseActivity extends Activity { // 维护一个Activity软引用的列表 private static List<SoftReference<Activity>> list = new ArrayList<SoftReference<Activity>>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); list.add(new SoftReference<Activity>(this)); } @Override protected void onDestroy() { super.onDestroy(); list.remove(new SoftReference<Activity>(this)); } /** * 关闭所有的Activity */ public void finishAll() { for (SoftReference<Activity> sr : list) { if (sr.get() != null) { sr.get().finish(); } } } }
android.os.Process.killProcess(android.os.Process.myPid()):同上
ActivityManager am= (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
am.killBackgroundProcesses(this.getPackageName()):测试无效
自定义BaseActivity维护Activity列表:可以关闭依次启动的所用Activity,进而退出整个App
finishAffinity():可以关闭同一个任务栈中的所有Activity,Android自带方法,比较方便