zoukankan      html  css  js  c++  java
  • Android TransitionDrawable ImageView过度效果使用实例


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

    执行例如以下所看到的:第一张为初始界面。第二张为过度中界面,第三张为过渡结束

           

    新建一个TransitionDrawable的Androidproject。


    文件夹结构:



    主界面activity_main.xml代码例如以下:

    <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>

    主界面主要是一个应用了TransitionDrawable的button
    transition是在drawalbe目录下,其代码例如以下所看到的:

    <?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代码例如以下:

     <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>


    MainActivity.java代码例如以下

    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);
    	}
    }
    

    源代码下载

    扩展阅读:

    http://www.tuicool.com/articles/uiaYBrU

    http://adk.tumblr.com/post/13069401302/outofmemoryerror-imageview-and-transitiondrawable

  • 相关阅读:
    luogu P1064|| 01背包||金明的预算
    NOIp蒟蒻的爆零记——HA-0132
    模板输入计划
    1112测试教你做人
    NOIP注意事项
    强连通分量的一二三 | | JZOJ【P1232】 | | 我也不知道我写的什么
    图的割点 | | jzoj【P1230】 | | gdoi | |备用交换机
    【游戏作品】Sunset Game 制作组出品游戏一览
    【说明】我们计划从博客园迁移到知乎啦
    【总结】操作系统的重点
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/6940682.html
Copyright © 2011-2022 走看看