Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加启动页面 #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,32 @@

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
android:targetSdkVersion="19" />

<application
android:name=".ZhihuApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Daily.AppTheme.Light" >
<activity
android:name=".activity.MainActivity"

<activity android:name=".activity.SplashActivity"
android:alwaysRetainTaskState="true"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name" >
android:screenOrientation="portrait"
android:label="@string/app_name"
android:theme="@style/Theme.splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.MainActivity"
android:alwaysRetainTaskState="true"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name" >
</activity>
<activity
android:name=".activity.NewsDetailActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
Expand Down
5 changes: 5 additions & 0 deletions res/anim/alpha.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromAlpha="0.1"
android:toAlpha="1.0" />
Binary file added res/drawable-hdpi/default_splash.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xhdpi/default_splash.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xxhdpi/default_splash.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions res/layout/activity_splash.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

</LinearLayout>
3 changes: 3 additions & 0 deletions res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,7 @@
<item name="android:textColor">@color/ab_title_dark</item>
<item name="android:textSize">18sp</item>
</style>
<style name="Theme.splash" parent="@android:style/Theme.NoTitleBar.Fullscreen">
<item name="android:windowIsTranslucent">true</item>
</style>
</resources>
3 changes: 3 additions & 0 deletions src/com/cundong/izhihu/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public static final class Url {

// 获取过往新闻
public static final String URLDEFORE = "http://news.at.zhihu.com/api/3/news/before/";

//获取启动页面的图片
public static final String URL_LAUNCHER = "http://news-at.zhihu.com/api/3/start-image/";
}

public static final int NEWS_LIST = 1;
Expand Down
264 changes: 264 additions & 0 deletions src/com/cundong/izhihu/activity/SplashActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
package com.cundong.izhihu.activity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;

import com.cundong.izhihu.Constants.Url;
import com.cundong.izhihu.R;
import com.cundong.izhihu.http.HttpClientUtils;
import com.cundong.izhihu.task.MyAsyncTask;
import com.cundong.izhihu.util.SDCardUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

/**
* 启动页面
*
* @author gonglei
*
*/
public class SplashActivity extends Activity {

private static final String TAG = "SplashActivity";
private Context context;
private Handler mHandler = new Handler();
private String mImageUrl;
private View view;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

view = View.inflate(this, R.layout.activity_splash, null);
setContentView(view);

context = this;
mImageUrl = SDCardUtils.getExternalCacheDir(context) + File.separator
+ "launcher_image";
initData();

Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
view.startAnimation(animation);

animation.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
mHandler.postDelayed(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
goHome();
}
}, 500);
}
});
}

/**
* 获取图片更新View
*/
@SuppressLint("NewApi")
private void initData() {
Bitmap bitmap = getImageFromLocal(mImageUrl);
if (bitmap != null) {
Drawable drawable = new BitmapDrawable(bitmap);
view.setBackground(drawable);
Log.d(TAG, "local image");
} else {
view.setBackground(getResources().getDrawable(
R.drawable.default_splash));
Log.d(TAG, "default");
}
}

/**
* 获取手机分辨率拼接URL
*
* @return
*/
private String initUrl() {
String splashUrl = null;
int width = getResources().getDisplayMetrics().widthPixels;
if (width == 480) {
splashUrl = Url.URL_LAUNCHER + "480*728";
} else if (width == 720) {
splashUrl = Url.URL_LAUNCHER + "720*1184";
} else if (width == 1080) {
splashUrl = Url.URL_LAUNCHER + "1080*1776";
}
Log.d(TAG, "width = " + width + ", url = " + splashUrl);
return splashUrl;
}

/**
* 启动图片加载,跳转至主页
*/
private void goHome() {
new LoadImageTask().executeOnExecutor(MyAsyncTask.THREAD_POOL_EXECUTOR,
initUrl());
Intent intent = new Intent();
intent.setClass(this, MainActivity.class);
startActivity(intent);
finish();
}

/**
* 图片下载的Task
*
* @author gonglei
*
*/
private class LoadImageTask extends MyAsyncTask<String, String, String> {

@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String result = null;
if (params[0] != null) {
try {
result = HttpClientUtils.get(SplashActivity.this,
params[0], null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}

@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
Log.d(TAG, "result = " + result);
if (result == null) {
return;
}
JSONObject object;
String imageUrl = null;
try {
object = new JSONObject(result);
imageUrl = object.getString("img");
ImageLoader loader = ImageLoader.getInstance();
loader.loadImage(imageUrl, new SimpleImageLoadingListener() {

@Override
public void onLoadingComplete(String imageUri, View view,
Bitmap loadedImage) {
// TODO Auto-generated method stub
Log.d(TAG, "load complete");
saveImage(mImageUrl, loadedImage);
super.onLoadingComplete(imageUri, view, loadedImage);
}

@Override
public void onLoadingFailed(String imageUri, View view,
FailReason failReason) {
// TODO Auto-generated method stub
Log.d(TAG, "load failed");
super.onLoadingFailed(imageUri, view, failReason);
}

});
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onPostExecute(result);
}

}

/**
* 保存图片到本地
*
* @param imagePath
* @param bm
*/
private void saveImage(String imagePath, Bitmap bm) {

if (bm == null || imagePath == null || "".equals(imagePath)) {
return;
}

File f = new File(imagePath);
if (f.exists()) {
f.delete();
}
try {
File parentFile = f.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
f.createNewFile();
FileOutputStream fos;
fos = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (FileNotFoundException e) {
f.delete();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
f.delete();
}

}

/**
* 从本地获取图片
*
* @param imagePath
* @return
*/
private Bitmap getImageFromLocal(String imagePath) {
File file = new File(imagePath);
if (file.exists()) {
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
file.setLastModified(System.currentTimeMillis());
return bitmap;
}
return null;
}

}