Andorid简单应用理财工具-实现启动界面

来源:互联网 发布:老司机网站 知乎 编辑:程序博客网 时间:2024/05/14 07:00

教程链接:启动界面实现

 

布局 

基本上是按照教程走的,但是按照教程上说的并不能真正的居中,有点错位,按照自己理解修改了 


 

修改成RelativeLayout属性为android:gravity="center_vertical",每个View单独添加android:layout_centerHorizontal="true",效果如下:


 

布局源码如下:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/main_layout"    android:layout_width="match_parent"    android:layout_height="match_parent"     android:gravity="center_vertical" >        <ImageView android:id="@+id/main_logo"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/logo"        android:contentDescription="@string/main_logo_desc"        android:layout_centerHorizontal="true" />        <TextView android:id="@+id/main_welcome"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/main_logo"        android:text="@string/main_welcome"        android:layout_centerHorizontal="true" /></RelativeLayout>



主程序

 

直接在ImageView元素上设置alpha的方式已经不建议使用,改为直接资源修改

mainLogoDrawable = getResources().getDrawable(R.drawable.logo);mainLogoDrawable.setAlpha(logoAlpha);

 

handler建议使用静态类,具体可以看看网上的解决方案


主程序源码

package com.example.diligentpiggy;import java.lang.ref.WeakReference;import android.app.Activity;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.widget.ImageView;import android.widget.TextView;public class MainActivity extends Activity {    private ImageView mainLogo;    private TextView mainWelcome;        private Drawable mainLogoDrawable;        private int logoAlpha = 255;    private static final int UPDATE_ALPHA = -1;    private static final int START_ACTIVITY = -2;        private static final int THREAD_INIT = 0;    private static final int THREAD_RUN = 1;    private static final int THREAD_FINISH = 2;    private int threadState = THREAD_INIT;        private threadHandler handler;        @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                mainLogo = (ImageView)findViewById(R.id.main_logo);        mainWelcome = (TextView)findViewById(R.id.main_welcome);                Log.v("DiligentPiggy Main", "DiligentPiggy start ...");                        // 直接用mainLogo.setAlpha()的方式已经不建议使用        mainLogoDrawable = getResources().getDrawable(R.drawable.logo);                mainLogoDrawable.setAlpha(logoAlpha);                // 初始化句柄        handler = new threadHandler(this);                // 开启新线程        new Thread(new Runnable() {            @Override            public void run() {                initApp(); //初始化程序                                // 如果不是结束状态一直执行                while(threadState < THREAD_FINISH) {                    try {                        if(threadState == THREAD_INIT) {                            Thread.sleep(2000);                            threadState = THREAD_RUN;                        } else {                            Thread.sleep(50);                        }                        updateApp();                    } catch(InterruptedException e) {                        e.printStackTrace();                    }                }            }                    }).start();            }        public void updateLogoAlpha() {        mainLogoDrawable.setAlpha(logoAlpha);        mainLogo.invalidate();            }        public void startNextActivity() {        Log.v("DiligentPiggy Main", "DiligentPiggy start next activity");           mainWelcome.setText("DiligentPiggy start next activity");    }        static class threadHandler extends Handler {        private WeakReference<MainActivity> mainReference;                public threadHandler(MainActivity activity) {            mainReference = new WeakReference<MainActivity>(activity);        }                @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            MainActivity main = mainReference.get();            // 更新alpha            switch(msg.what) {                case MainActivity.UPDATE_ALPHA:                    main.updateLogoAlpha();                    break;                case MainActivity.START_ACTIVITY:                    main.startNextActivity();                    break;            }                           }    }        public void updateApp() {        logoAlpha -= 5;                if(logoAlpha <= 0) {            threadState = THREAD_FINISH;            handler.sendEmptyMessage(START_ACTIVITY);        } else {            handler.sendEmptyMessage(UPDATE_ALPHA);        }            }        public void initApp() {        Log.v("DiligentPiggy Main", "DiligentPiggy init ...");    }}

所有源码可以到github上下载,地址:https://github.com/lazyboywu/diligentpiggy/tree/course_1