日期:2018.9.11
最近在做的這個項目裏使用了歡迎頁,只顯示一張圖片太過於單調,沒有服務器(編程小白,沒錢買服務器)也不能調用服務器的資源,於是想到是不是可以獲取其他網站的圖片,便想到Bing每日一圖的圖片能不能獲取到手機上作爲歡迎界面,網上查了一下,果然有很多教程,於是自己也做了一下,不廢話,直接上代碼。
第一部分:歡迎頁代碼
新建一個SplashActivity並自動生成一個activity_splash.xml佈局文件,佈局文件代碼如下:一個ImageView用於顯示背景圖片,注意這裏的scaleType屬性,設置爲centerCrop,之前在《每日一記—ImageView的scaleType屬性和獲取當前手機日期、時間的方法》已經介紹過,這個屬性值的意思是按比例擴大圖片的size居中顯示,使得圖片長 (寬)等於或大於View的長(寬)(圖片會發生正常的裁剪);一個TextView用於顯示軟件版本號
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_background1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#ff00aaff"/>
<TextView
android:id="@+id/tv_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
android:textSize="20sp"
android:text="V1.0"/>
</RelativeLayout>
SplashActivity.java中的代碼如下:代碼中AnalysisUtils.isFolderExists用於判斷文件夾是否存在,如果不存在就直接創建一個,AnalysisUtils.fileIsExists用於判斷文件是否存在,這裏如果不存在的話,跳轉到獲取being每日一圖的代碼,cutPictureUtils.decodeUriAsBitmap用於將圖片的URL轉換爲bitmap;接下來就是獲取程序包信息;最後就是運用Timer和TimerTask配合實現眼石3秒的作用,延時結束後直接跳轉到登錄界面,另外在跳轉時我加入了一個自動登錄判斷,將登錄狀態isLogin保存在了SharedPreferences中。
private TextView tv_version;
private ImageView iv_background;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//設置爲此界面爲豎屏(因爲是歡迎界面)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
init();
}
private void init(){
tv_version= findViewById(R.id.tv_version);
iv_background=findViewById(R.id.iv_background1);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
// 如果想加入時分秒,pattern設爲"yyyy年MM月dd日 HH:mm:ss"
Date date = new Date(System.currentTimeMillis());
String fileName = simpleDateFormat.format(date) + ".jpg";
Uri imgUri= Uri.parse("file:///sdcard/crazystudy/"+ fileName);
if(AnalysisUtils.isFolderExists("/sdcard/crazystudy/")){
if (AnalysisUtils.fileIsExists(imgUri.getPath())){
// 顯示出來
CutPictureUtils cutPictureUtils=new CutPictureUtils(SplashActivity.this,
"");
iv_background.setImageBitmap(cutPictureUtils.decodeUriAsBitmap(imgUri));
}else {
//獲取bing背景圖片
}
}
try{
//獲取程序包信息
PackageInfo info=getPackageManager().getPackageInfo(getPackageName(),0);
tv_version.setText("V"+info.versionName);
}catch (PackageManager.NameNotFoundException e){
tv_version.setText("V");
}
//讓此界面延遲3秒後再跳轉,timer中有一個線程,這個線程不斷執行task
Timer timer = new Timer();
//TimerTask類表示一個在指定時間內執行的task
TimerTask task = new TimerTask() {
@Override
public void run() {
//打開後判斷是否是登錄狀態,如果是,則自動登錄
SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
boolean isLogin=sp.getBoolean("isLogin",false);
Intent intent;
if (isLogin){
Intent data = new Intent();
data.putExtra("isLogin",true);
setResult(RESULT_OK,data);
intent=new Intent(SplashActivity.this,MainActivity.class);
}else {
intent=new Intent(SplashActivity.this,LoginActivity.class);
}
startActivity(intent);
SplashActivity.this.finish();
}
};
timer.schedule(task,3000); //設置這個task在延遲2秒之後自動執行
}
使用到的兩個Utils文件代碼如下:
AnalysisUtils.java
/**
* 判斷文件夾是否存在,不存在則創建這個文件夾
* @param strFolder
* @return
*/
public static boolean isFolderExists(String strFolder) {
File file = new File(strFolder);
if (!file.exists()) {
if (file.mkdir()) {
return true;
} else
return false;
}
return true;
}
/**
* 判斷文件是否存在(可以是文件夾或者是文件)
* @param strFile
* @return
*/
public static boolean fileIsExists(String strFile)
{
try
{
File f=new File(strFile);
if(!f.exists())
{
return false;
}
}
catch (Exception e)
{
return false;
}
return true;
}
cutPictureUtils.java
/**
* 通過URL獲得圖片
* @param uri
* @return 返回圖片
*/
public Bitmap decodeUriAsBitmap(Uri uri){
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(activity.getContentResolver().openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
return bitmap;
}