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

教程链接:启动界面实现

 

布局 

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


 

修改成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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中创建启动界面有多种方法,以下是其中两种常用的方法: 1. 使用ImageView显示启动界面图片: 在res目录下创建文件夹drawable-hdpi,并将启动界面图片放入该文件夹中。然后在启动界面的布局文件中使用ImageView来显示该图片。例如,创建一个名为activity_splash.xml的布局文件,内容如下: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/splash_image" android:scaleType="centerCrop" /> </RelativeLayout> ``` 其中,@drawable/splash_image是启动界面图片的资源ID。 2. 使用Theme设置启动界面样式: 在res/values/styles.xml文件中定义一个启动界面的主题样式,例如: ```xml <style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowBackground">@drawable/splash_image</item> </style> ``` 其中,@drawable/splash_image是启动界面图片的资源ID。 然后,在AndroidManifest.xml文件中将启动界面的主题样式应用启动的Activity上,例如: ```xml <activity android:name=".SplashActivity" android:theme="@style/SplashTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> ``` 其中,SplashActivity是启动界面的Activity类名。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值