免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2458 | 回复: 1
打印 上一主题 下一主题

不用线程做Android软件欢迎界面,透明效果,完成后自动跳转 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-13 17:35 |只看该作者 |倒序浏览
不用线程做Android软件欢迎界面,透明效果,完成后自动跳转






最近在网上看到一些Android软件的欢迎界面做得都挺复杂的(个人觉得),因为一般都用到了线程,接着就想有没有简单一点的办法。然后就有了下文:



这个欢迎界面主要是借助Animation动画来实现的(效果如图),不需要用到线程。实现的方法很简单,为动画设置监听就可以了,在动画播放结束时结束欢迎界面并跳转到软件的主界面。



  



Java代码  
1./**  
2. * 欢迎界面  
3. * @author 小建枫叶  
4. *  
5. */  
6.public class WelcomeActivity extends Activity implements AnimationListener {   
7.    private ImageView  imageView = null;   
8.    private Animation alphaAnimation = null;   
9.      
10.    @Override  
11.    protected void onCreate(Bundle savedInstanceState) {   
12.           
13.        super.onCreate(savedInstanceState);   
14.        setContentView(R.layout.welcome);   
15.        imageView = (ImageView)findViewById(R.id.welcome_image_view);   
16.        alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.welcome_alpha);   
17.        alphaAnimation.setFillEnabled(true); //启动Fill保持   
18.        alphaAnimation.setFillAfter(true);  //设置动画的最后一帧是保持在View上面   
19.        imageView.setAnimation(alphaAnimation);   
20.        alphaAnimation.setAnimationListener(this);  //为动画设置监听   
21.    }   
22.      
23.    @Override  
24.    public void onAnimationStart(Animation animation) {   
25.           
26.    }   
27.      
28.    @Override  
29.    public void onAnimationEnd(Animation animation) {   
30.        //动画结束时结束欢迎界面并转到软件的主界面   
31.        Intent intent = new Intent(this, MainActivity.class);   
32.        startActivity(intent);   
33.        this.finish();   
34.    }   
35.      
36.    @Override  
37.    public void onAnimationRepeat(Animation animation) {   
38.           
39.    }   
40.      
41.    @Override  
42.    public boolean onKeyDown(int keyCode, KeyEvent event) {   
43.        //在欢迎界面屏蔽BACK键   
44.        if(keyCode==KeyEvent.KEYCODE_BACK) {   
45.            return false;   
46.        }   
47.        return false;   
48.    }   
49.      
50.}  
/**
* 欢迎界面
* @author 小建枫叶
*
*/
public class WelcomeActivity extends Activity implements AnimationListener {
        private ImageView  imageView = null;
        private Animation alphaAnimation = null;
       
        @Override
        protected void onCreate(Bundle savedInstanceState) {
               
                super.onCreate(savedInstanceState);
                setContentView(R.layout.welcome);
                imageView = (ImageView)findViewById(R.id.welcome_image_view);
                alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.welcome_alpha);
                alphaAnimation.setFillEnabled(true); //启动Fill保持
                alphaAnimation.setFillAfter(true);  //设置动画的最后一帧是保持在View上面
                imageView.setAnimation(alphaAnimation);
                alphaAnimation.setAnimationListener(this);  //为动画设置监听
        }
       
        @Override
        public void onAnimationStart(Animation animation) {
               
        }
       
        @Override
        public void onAnimationEnd(Animation animation) {
                //动画结束时结束欢迎界面并转到软件的主界面
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                this.finish();
        }
       
        @Override
        public void onAnimationRepeat(Animation animation) {
               
        }
       
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
                //在欢迎界面屏蔽BACK键
                if(keyCode==KeyEvent.KEYCODE_BACK) {
                        return false;
                }
                return false;
        }
       
}
动画welcome_alpha.xml



Xml代码  
1.<?xml version="1.0" encoding="utf-8"?>  
2.<set xmlns:android="http://schemas.android.com/apk/res/android"  
3.    android:interpolator="@android:anim/accelerate_interpolator">  
4.    <alpha   
5.        android:fromAlpha="0.0"  
6.        android:toAlpha="1.0"  
7.        android:duration="2000"   
8.        />  
9.    <alpha   
10.        android:fromAlpha="1.0"  
11.        android:toAlpha="0.0"  
12.        android:startOffset="3000" //延迟3秒再开始   
13.        android:duration="3000"   
14.        />  
15.</set>  
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/accelerate_interpolator">
        <alpha
                android:fromAlpha="0.0"
                android:toAlpha="1.0"
                android:duration="2000"
                />
        <alpha
                android:fromAlpha="1.0"
                android:toAlpha="0.0"
                android:startOffset="3000" //延迟3秒再开始
                android:duration="3000"
                />
</set>

布局welcome.xml



Xml代码  
1.<?xml version="1.0" encoding="utf-8"?>  
2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
3.    android:layout_width="fill_parent"  
4.    android:layout_height="fill_parent"  
5.    androidrientation="vertical"   
6.    android:gravity="center_vertical|center_horizontal">  
7.    <ImageView  
8.        android:id="@+id/welcome_image_view"   
9.        android:layout_width="wrap_content"  
10.        android:layout_height="wrap_content"  
11.        android:src="@drawable/welcome"  
12.        />  
13.</LinearLayout>

论坛徽章:
0
2 [报告]
发表于 2011-11-14 09:23 |只看该作者
学习了..希望与ll能多交流
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP