package com.xiaoli.ui;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import java.util.ArrayList;
import com.hck.LoverReader.R;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
public class GuideActivity extends Activity {
private ViewPager viewPager;
/**装分页显示的view的数�??*/
private ArrayList pageViews;
private ImageView imageView;
/**将小圆点的图片用数组表示*/
private ImageView[] imageViews;
//包裹滑动图片的LinearLayout
private ViewGroup viewPics;
//包裹小圆点的LinearLayout
private ViewGroup viewPoints;
//第一次还有进入loading
private SharedPreferences sp;
private SharedPreferences.Editor editor;
boolean isFirstRun;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sp = this.getSharedPreferences("share", MODE_PRIVATE);
//将要分页显示的View装入数组
LayoutInflater inflater = getLayoutInflater();
pageViews = new ArrayList();
pageViews.add(inflater.inflate(R.layout.viewpager_page1, null));
pageViews.add(inflater.inflate(R.layout.viewpager_page2, null));
pageViews.add(inflater.inflate(R.layout.viewpager_page3, null));
pageViews.add(inflater.inflate(R.layout.viewpager_page4, null));
//创建imageviews数组,大小是要显示的图片的数
imageViews = new ImageView[pageViews.size()];
//从指定的XML文件加载视图
viewPics = (ViewGroup) inflater.inflate(R.layout.activity_guide, null);
//实例化小圆点的linearLayout和viewpager
viewPoints = (ViewGroup) viewPics.findViewById(R.id.viewGroup);
viewPager = (ViewPager) viewPics.findViewById(R.id.guidePages);
//添加小圆点的图片
for(int i=0;i
}
最后这段代码指定了intent要跳转的activity,也就是我这个app的书架界面了
//跳转
Intent intent = new Intent();
intent.setClass(GuideActivity.this, BookListActivity2.class);
GuideActivity.this.startActivity(intent);
GuideActivity.this.finish();
二、lodingActivity
这个界面主要是在WelcomeActivity后,播放一下渐变动画(文字、图片随机出现),这个应该有点多余,因为可以从WelcomeActivity直接跳转到BookListActivity2界面,
但是自己写都写了,也不想删了
LodingActivity.java
package com.xiaoli.ui;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.hck.LoverReader.R;
public class LodingActivity extends Activity {
private TextView textView;
private FrameLayout layoutLoding2;
private ImageView imageView;
private Animation animation;
//图片资源
private int[] mArray = new int[]{R.drawable.f_0, R.drawable.f_1, R.drawable.f_2, R.drawable.loding2, R.drawable.f_3, R.drawable.f_4, R.drawable.f_5,
R.drawable.f_6, R.drawable.f_7, R.drawable.f_8, R.drawable.f_9, R.drawable.f_10, R.drawable.f_11,};
private Random random;
//引导提示文字
String[] tips;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.loding);
//获取布局(设置动画)
layoutLoding2 =(FrameLayout)findViewById(R.id.layoutLoding2);
//随机数
random=new Random();
int mImgs=random.nextInt(mArray.length);
imageView = (ImageView) findViewById(R.id.loding_im);
imageView.setImageResource(mArray[mImgs]);
//文字
tips=getResources().getStringArray(R.array.loadingtips);
int rTips=random.nextInt(tips.length);
textView=(TextView)findViewById(R.id.lodingTips);
textView.setText(tips[rTips]);
animation = AnimationUtils.loadAnimation(this, R.anim.loding);
layoutLoding2.setAnimation(animation);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationEnd(Animation arg0) {
//休眠500毫秒
new Thread(){
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
startActivity(new Intent(LodingActivity.this,
BookListActivity2.class));
finish();
}
});
}
我们可以看到,里面定义了接个控件和一个Animation,主要就是随机产生图片和文字,播放一段动画,挺简单的。
三、WelComeActivity
这个Activity的作用比较多,主要有
1.加载一段动画
2.动画加载完后,判断是不是用户第一次进入这个界面,如是则跳转到GuideActivity,否则跳转到LodingActivity.这个过程用到了sharedPreferences
3.加载(初始化)书架界面的数据(书籍存在Raw文件下)。不是第一次才加载。
Welcome.java源代码
package com.xiaoli.ui;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.hck.LoverReader.R;
import com.hck.book.uti.JMPManager;
import com.xiaoli.date.FinalDate;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import static com.hck.LoverReader.R.id.imageView;
public class WelcomeActivity extends Activity {
private RelativeLayout layoutWelcome;
private SharedPreferences sp;
private SharedPreferences.Editor editor;
private Animation animation;
//动画是否加载完
private boolean isOK;
//是否加载(登录)过数据
private boolean isLogin;
private Boolean isFirstRun;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FinalDate.context=this;
sp = getSharedPreferences("book", Context.MODE_PRIVATE);
editor = sp.edit();
isFirstRun = sp.getBoolean("isFirstRun", true);
if (sp.getLong("time", 0) == 0) {
editor = sp.edit();
editor.putLong("time", System.currentTimeMillis());
editor.commit();
FinalDate.tme=System.currentTimeMillis();
}
else {
FinalDate.tme=sp.getLong("time", 0);
}
if (sp.getBoolean("isFirst", true)) {
FinalDate.isFirst=true;
editor = sp.edit();
editor.putBoolean("isFirst", false);
editor.commit();
}
else {
FinalDate.isFirst=false;
}
JMPManager manager = new JMPManager();
manager.startService(this,1);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_welcome);
layoutWelcome = (RelativeLayout) findViewById(R.id.RelativeLayout1);
animation = AnimationUtils.loadAnimation(this, R.anim.dialog_enter);
layoutWelcome.setAnimation(animation);
if (!isLogin()) {
new WelcomeActivity.AsyncSetApprove().execute();
isLogin = false;
} else {
isLogin = true;
}
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationEnd(Animation arg0) {
isOK = true;
//休眠1000毫秒
new Thread(){
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
//发送消息
handler.sendEmptyMessage(2);
}
});
}
// 判断是否是第一次启动程序
private boolean isFristRun() {
if (!isFirstRun) {
return false;
} else {
editor.putBoolean("isFirstRun", false);
editor.commit();
return true;
}
}
//以前的loding代码
private boolean isLogin() {
sp = getSharedPreferences("mark", MODE_PRIVATE);
isLogin = sp.getBoolean("isInit", false);
if (isLogin) {
return true;
}
return false;
}
Handler handler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
if (isLogin && isOK ) {
if (isFristRun()) {
// 如果是第一次启动程序则进入引导界面
Intent intent = new Intent(WelcomeActivity.this,
GuideActivity.class);
startActivity(intent);
} else {
// 如果不是第一次启动则进入主页
Intent intent = new Intent(WelcomeActivity.this,
LodingActivity.class);
startActivity(intent);
}
WelcomeActivity.this.finish();
}
};
};
class AsyncSetApprove extends AsyncTask {
@Override
protected String doInBackground(String... params) {
File path = getFilesDir();
String[] strings = getResources().getStringArray(R.array.bookid);// 获取assets目录下的文件列表
for (int i = 0; i < strings.length; i++) {
try {
FileOutputStream out = new FileOutputStream(path + "/"
+ strings[i]);
BufferedInputStream bufferedIn = new BufferedInputStream(
getResources().openRawResource(R.raw.book1 + i));
BufferedOutputStream bufferedOut = new BufferedOutputStream(
out);
byte[] data = new byte[2048];
int length = 0;
while ((length = bufferedIn.read(data)) != -1) {
bufferedOut.write(data, 0, length);
}
// 将缓冲区中的数据全部写出
bufferedOut.flush();
// 关闭流
bufferedIn.close();
bufferedOut.close();
sp.edit().putBoolean("isInit", true).commit();
} catch (Exception e) {
e.printStackTrace();
}
}
ArrayList> insertList = new ArrayList>();
File[] f1 = path.listFiles();
int len = f1.length;
for (int i = 0; i < len; i++) {
if (f1[i].isFile()) {
if (f1[i].toString().contains(".txt")) {
HashMap insertMap = new HashMap();
insertMap.put("parent", f1[i].getParent());
insertMap.put("path", f1[i].toString());
insertList.add(insertMap);
}
}
}
SQLiteDatabase db = MyApplication.bookDB.getWritableDatabase();
db.delete(FinalDate.DATABASE_TABKE, "type='" + 2 + "'", null);
for (int i = 0; i < insertList.size(); i++) {
try {
if (insertList.get(i) != null) {
String s = insertList.get(i).get("parent");
String s1 = insertList.get(i).get("path");
String sql1 = "insert into " + FinalDate.DATABASE_TABKE
+ " (parent,path" + ", type"
+ ",now,ready) values('" + s + "','" + s1
+ "',2,0,null" + ");";
db.execSQL(sql1);
}
} catch (SQLException e) {
Log.e("hck", "setApprove SQLException", e);
} catch (Exception e) {
Log.e("hck", "setApprove Exception", e);
}
}
db.close();
sp.edit().putBoolean("isInit", true).commit();
return null;
}
@Override
protected void onPostExecute(String result) {
isLogin = true;
handler.sendEmptyMessage(1);
super.onPostExecute(result);
}
}
}
基本实现思路是:
1.不是第一次Login就去加载Raw文件里面的书籍,如是则不用。相关代码
if (!isLogin()) {
new WelcomeActivity.AsyncSetApprove().execute();
isLogin = false;
} else {
isLogin = true;
}
2.动画加载完后,发送消息给handler处理。相关代码
@Override
public void onAnimationEnd(Animation arg0) {
isOK = true;
//休眠1000毫秒
new Thread(){
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
//发送消息
handler.sendEmptyMessage(2);
}
3.handler的handlerMessage中判断用户是不是第一次进入,并选择跳转界面
public void handleMessage(android.os.Message msg) {
if (isLogin && isOK ) {
if (isFristRun()) {
// 如果是第一次启动程序则进入引导界面
Intent intent = new Intent(WelcomeActivity.this,
GuideActivity.class);
startActivity(intent);
} else {
// 如果不是第一次启动则进入主页
Intent intent = new Intent(WelcomeActivity.this,
LodingActivity.class);
startActivity(intent);
}
WelcomeActivity.this.finish();
}
}
// 判断是否是第一次启动程序
private boolean isFristRun() {
if (!isFirstRun) {
return false;
} else {
editor.putBoolean("isFirstRun", false);
editor.commit();
return true;
}
}
四、效果图
用AndroidStudio录制的短视频很模糊,然后直接用工具录制了GIF动图
1.第一次启动(welcomeActivity没录制上)
2.第二次启动
****************************************************************************************本文完,很少用csdn写文章,写得格式都全部乱了,唉!
以后有时间一定多写写