Android TransitionDrawable ImageView过度效果使用实例

转载于 http://www.2cto.com/kf/201412/363133.html

实现两张图片渐隐渐现的过渡效果Transition Drawable实现两张图片之间动态过度效果的方式。

运行如下所示:第一张为初始界面,第二张为过度中界面,第三张为过渡结束

\\\

新建一个TransitionDrawable的Android工程。
目录结构:

\

主界面activity_main.xml代码如下:
 

?
1
<relativelayout android:layout_height= "match_parent" android:layout_width= "match_parent" xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" ><button android:background= "@drawable/transition" android:layout_centerinparent= "true" android:layout_height= "wrap_content" android:layout_marginbottom= "20dp/" android:layout_width= "match_parent" android:onclick= "change" android:text= "@string/button_text" ></button></relativelayout>

主界面主要是一个应用了TransitionDrawable的按钮
transition是在drawalbe文件夹下,其代码如下所示:

 

 

?
1
2
3
4
5
<!--?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>

strings.xml代码如下:

 

 

?
1
2
3
4
<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代码如下:

 

 

?
1
2
3
4
5
<!--?xml version= 1.0 encoding=utf- 8 ?-->
<resources>
   <color name= "red" >#ff0000</color>
   <color name= "green" >#00ff00</color> 
</resources>

MainActivity.java代码如下

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 );
     }
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值