android学习之TransitionDrawable …

Drawable的例子,体现出Drawable的强大功能。Android SDK中说明了Drawable主要的作用是:在XML中定义各种动画,然后把 XML当作Drawable资源来读取,通过Drawable显示动画。

下面举个使用TransitionDrawable 的例子,创建一个Android工程,然后再这个工程的基础上修改,修改过程如下:
1、去掉layout/main.xml中的TextView,增加ImagView,如下:
<ImageView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:tint=”#55ff0000″
android:src=”@drawable/my_image”/>

2、创建一个XML文件,命名为expand_collapse.xml,内容如下:
<?xml version=”1.0″ encoding=”UTF-8″?>
<transition xmlns:android=”http://schemas.android.com/apk/res/android”>
<item android:drawable=”@drawable/image_expand”/>
<item android:drawable=”@drawable/image_collapse”/>
</transition>
需要3张png图片,存放到res\drawable目录下,3张图片分别命名为:my_image.png、image_expand.png、image_collapse.png。

3、修改Activity中的代码,内容如下:
LinearLayout mLinearLayout;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLinearLayout = new LinearLayout(this);
ImageView i = new ImageView(this);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mLinearLayout.addView(i);
setContentView(mLinearLayout);
Resources res = getResources();
TransitionDrawable transition =
(TransitionDrawable) res.getDrawable(R.drawable.expand_collapse);
i.setImageDrawable(transition);
ansition.startTransition(10000);}

4、如果修改的没有错误,运行程序,结果显示如下:
初始图片
TransitionDrawable-begin
过渡中的图片
TransitionDrawable-mid
最后的图片
TransitionDrawable-end

屏幕上动画显示的是:从图片image_expand.png过渡到image_collapse.png,也就是我们在expand_collapse.xml中定义的一个transition动画。看完这个例子,你对Drawable的理解是否又深入些?这里提供这个程序的源代码,供大家下载,可以在这个例子的基础上去体会其他的Drawable,来加深对Drawable的理解。

总结说明

通过以上2个例子程序,相信对Drawable会有一定的认识了,在以后的篇幅中会介绍更多的例子,更加深入的学习和理解Drawable。具体还有哪些Drawable,大家到Android SDK去深入学习吧。

 

转贴至:http://www.moandroid.com/?p=784

实例:

android学习之TransitionDrawable <wbr>实现变化效果

android学习之TransitionDrawable <wbr>实现变化效果

 

实现上图效果:

xml部份文件:

 <SlidingDrawer  
     android:id="@+id/slidingdrawer" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:handle="@+id/handle" 
     android:content="@+id/content">  
     <ImageView  
              android:id="@+id/handle" 
              android:layout_width="56dip"
              android:layout_height="fill_parent"
              android:background="@drawable/handle"
                 
              android:focusable="true"
              android:clickable="true"
                
              android:scaleType="center"    
              android:src="@drawable/handle_icon"
         />  
     <LinearLayout  
         android:id="@+id/content" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:background="#778899">  
         <Button  
             android:id="@+id/button" 
             android:layout_width="wrap_content" 
             android:layout_height="wrap_content" 
             android:text="Button" 
         />  
         <EditText  
             android:id="@+id/editText" 
             android:layout_width="fill_parent" 
             android:layout_height="wrap_content" 
         />  
     </LinearLayout>  
 </SlidingDrawer>   

 

hand_icon.xml

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_tray_expand"  />
    <item android:drawable="@drawable/ic_tray_collapse"  />
</transition>

 

hand.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/tray_handle_normal" />
    <item android:state_pressed="true" android:drawable="@drawable/tray_handle_pressed" />
    <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/tray_handle_selected" />
    <item android:state_enabled="true" android:drawable="@drawable/tray_handle_normal" />
    <item android:state_focused="true" android:drawable="@drawable/tray_handle_selected" />
</selector>


代码部份:

  mDrawer = (SlidingDrawer) findViewById(R.id.slidingdrawer);
  final SlidingDrawer drawer = mDrawer;

  mLinearLayout = (LinearLayout) drawer.getContent();
  final LinearLayout grid = mLinearLayout;

 

  mImageView = (ImageView) drawer.findViewById(R.id.handle);
  mHandleIcon = (TransitionDrawable) mImageView.getDrawable();
  mHandleIcon.setCrossFadeEnabled(true);

 

  final DrawerManager drawerManager = new DrawerManager();
  drawer.setOnDrawerOpenListener(drawerManager);
  drawer.setOnDrawerCloseListener(drawerManager);
  drawer.setOnDrawerScrollListener(drawerManager);

 

private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener,
   SlidingDrawer.OnDrawerCloseListener,
   SlidingDrawer.OnDrawerScrollListener {
  private boolean mOpen;

  public void onDrawerOpened() {
   Log.v(tag, "onDrawerOpened");
   if (!mOpen) {
    mHandleIcon.reverseTransition(150);
    mOpen = true;
   }
  }

  private void offsetBoundsToDragLayer(Rect bounds, View view) {
   view.getDrawingRect(bounds);
   Log.v(tag, "offsetBoundsToDragLayer");
  }

  public void onDrawerClosed() {
   Log.v(tag, "onDrawerClosed");
   if (mOpen) {
    mHandleIcon.reverseTransition(150);
    mOpen = false;
   }

  }

  public void onScrollStarted() {
   if (PROFILE_DRAWER) {
    android.os.Debug.startMethodTracing("/sdcard/launcher-drawer");
   }
  }

  public void onScrollEnded() {
   if (PROFILE_DRAWER) {
    android.os.Debug.stopMethodTracing();
   }
  }
 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值