實現兩張圖片漸隱漸現的過渡效果Transition Drawable實現兩張圖片之間動態過分效果的方式。html
執行例如如下所看到的:第一張爲初始界面。第二張爲過分中界面,第三張爲過渡結束java
新建一個TransitionDrawable的Androidproject。android
文件夾結構:app
主界面activity_main.xml代碼例如如下:
ide
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/button_text" android:layout_centerInParent="true" android:onClick="change" android:background="@drawable/transition" android:layout_marginBottom="20dp"/> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <transition xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/red"></item> <item android:drawable="@color/green"></item> </transition> post
<string name="app_name">TransitionDrawable</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="button_text">過渡效果</string>colors.xml代碼例如如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="red">#ff0000</color> <color name="green">#00ff00</color> </resources> ui
package com.shen.transitiondrawable; import android.app.Activity; import android.graphics.drawable.TransitionDrawable; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public void change(View v) { TransitionDrawable drawable = (TransitionDrawable)((Button)v).getBackground(); drawable.startTransition(2000); } }
擴展閱讀:this
http://www.tuicool.com/articles/uiaYBrU
.net
http://adk.tumblr.com/post/13069401302/outofmemoryerror-imageview-and-transitiondrawable
code