Andorid簡單應用理財工具-實現啟動介面
阿新 • • 發佈:2019-02-19
佈局
基本上是按照教程走的,但是按照教程上說的並不能真正的居中,有點錯位,按照自己理解修改了
修改成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