标签:安卓小说阅读器
package com.example.viewpagerok;
import java.io.File;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
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.ContextMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
@SuppressLint({ "InflateParams", "ShowToast", "ViewHolder" })
public class MainActivity extends Activity {
ArrayList<View> arrayList;
ArrayList<String>name;
//如果是公共变量,每次界面进入,gridview的setadapter就会执行一次,就会得到一个getdata,这个变量就会追加一个,所以不能是公共变量
ArrayList<String>progress=new ArrayList<String>();
ArrayList<String>pathList2;
ArrayList<Integer>aIntegers=new ArrayList<Integer>();
int[] bookimage={R.drawable.book1,R.drawable.book2,R.drawable.book4,R.drawable.book5,R.drawable.book6,R.drawable.book7,R.drawable.book8,R.drawable.book9,R.drawable.book10,R.drawable.book11,R.drawable.book12,R.drawable.book13};
int[] web={R.drawable.hjsm,R.drawable.zl,R.drawable.yanqing888,R.drawable.xs8,R.drawable.xiaoxiangshuyuan};
String[]book={"http://hjsm.tom.com","http://www.zhulang.com","http://www.yanqing888.net/","http://www.xs8.cn/","http://www.xxsy.net/"};
ViewPager viewPager;
TextView sdcard;
TextView sdcard1;
String sdpathString;
String sdpathString1;
int count = 0;
View view;
static String[] a = new String[] { "1", "2", "3", "4", "5" };
Button button;
SQLiteDatabase sqLiteDatabase;
Animation animation;
int itemposition;
ArrayList<ArrayList<String>> aList = new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<String>> pathr = new ArrayList<ArrayList<String>>();
ArrayList<String> filelist;
ArrayList<String> pathlist;
ListView listView;
ArrayAdapter<String> adapter;
LayoutInflater layoutInflater;
TextView textView;
TextView textView1;
String sql;
String tab;
TextView textViewmain;
TextView textViewmain1;
TextView textViewmain2;
GridView gridView;
View item;
View booknetview;
TextView textViewforpage3;
Drawable drawable;
String elments;
View view1;
LinearLayout linearLayout;
LinearLayout linearLayoutq;
LinearLayout linearLayoutw;
LinearLayout linearLayoute;
LinearLayout linearLayoutr;
c c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
setContentView(R.layout.activity_main);
if (!Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
Toast.makeText(MainActivity.this, "请确认内置SD卡",1).show();
finish();
}
sqLiteDatabase=new shujuku(getApplicationContext()).getWritableDatabase();
sql="create table if not exists bookv(id integer primary key autoincrement,name varchar(9),progress integer,path varchar(33))";
sqLiteDatabase.execSQL(sql);
// animation=AnimationUtils.loadAnimation(this, R.anim.halftouming);
linearLayout=(LinearLayout)findViewById(R.id.linearone);
linearLayoute=(LinearLayout)findViewById(R.id.lineartwo);
drawable=linearLayoute.getBackground();
// linearLayoutq=(LinearLayout)findViewById(R.id.linearthree);
// linearLayoutw=(LinearLayout)findViewById(R.id.linearfour);
linearLayoutr=(LinearLayout)findViewById(R.id.linearzim);
linearLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(0);
// linearLayout.startAnimation(animation);
// linearLayout.setAlpha(0.5f);
// Toast.makeText(MainActivity.this, "1", 1).show();
}
});
linearLayoute.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(1);
}
});
linearLayoutr.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(2);
}
});
/* linearLayoutq.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(2);
Toast.makeText(MainActivity.this, "3", 1).show();
}
});
*/
// 为什么在viewpager实例化之前可以调用呢?静态的?答案是否定的,即使是静态也不会因为是静态而第一个加载,正确的原因是,oncreate函数,第一步加载的层次是实例化层次,所有实例化都会在第一批加载
arrayList = new ArrayList<View>();
layoutInflater = LayoutInflater.from(MainActivity.this);
view = layoutInflater.inflate(R.layout.page1, null);
/*
* button=(Button)view.findViewById(R.id.button2);//一个xml布局会被解析成一个class,
* 所以有view.find`````` button.setOnClickListener(new OnClickListener() {
*
* @Override public void onClick(View v) { // TODO Auto-generated method
* stub count++; button.setText(a[count]); } });
*/
arrayList.add(view);
view1=layoutInflater.inflate(R.layout.page2, null);
arrayList.add(view1);
booknetview=layoutInflater.inflate(R.layout.page3, null);
arrayList.add(booknetview);
// View viewq=layoutInflater.inflate(R.layout.page3, null);
// arrayList.add(viewq);
// textViewforpage3=(TextView)viewq.findViewById(R.id.textView1);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(new dxc());
// TODO Auto-generated method stub
getfilelist(Environment.getExternalStorageDirectory().toString());
// ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
// MainActivity.this, android.R.layout.simple_list_item_1,
// filelist);
/* listView.setAdapter(new listviewadapter());
//1 Toast.makeText(MainActivity.this, Integer.toString(aList.size()), 1).show();
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
// filelist=new ArrayList<String>();
File file = new File(pathr.get(pathr.size()-1).get(position));
getfilelist(pathr.get(pathr.size()-1).get(position));
if (file.isDirectory()) {
// getfilelist(filelist.get(position));
// ArrayAdapter<String>adapter=new
// ArrayAdapter<String>(MainActivity.this,
// android.R.layout.simple_list_item_1, filelist);
// 这里的filelist取的是局部变量,就是新传值进去的filelist还在公共变量,这里取的是之前的值,所以,给函数add一个返回值。
// 这里返回的还是之前的值,所以错误,集合公共变量,getfilelist()得到的就是上次的集合,所以不会刷新,必须把集合设置为局部变量,在局部new才行
// 将集合在公共new,是为了递归,有一个特定的集合接受所有信息,而这里,则需要不同的集合,每一次传参数都得到新的实例,否则总会指向上一次
count++;
Toast.makeText(MainActivity.this, Integer.toString(aList.size()), 1).show();
listView.setAdapter(new listviewadapter());
}
else {
Toast.makeText(MainActivity.this, "请长按加入书架", 1).show();
}
}
});
*/
linearLayout.setBackgroundColor(Color.parseColor("#FFCC66"));
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
// viewPager.setCurrentItem(0);
switch (arg0) {
case 0:
// Drawable drawable=linearLayoute.getBackground();
linearLayoute.setBackgroundColor(Color.parseColor("#00000000"));
linearLayout.setBackgroundColor(Color.parseColor("#FFCC66"));
linearLayoutr.setBackgroundColor(Color.parseColor("#00000000"));
// linearLayout.setAlpha(0.3f);
// linearLayoute.setBackgroundColor(Color.parseColor("#CCFF66"));
// registerForContextMenu(listView);
getfilelist(Environment.getExternalStorageDirectory().toString());
// ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
// MainActivity.this, android.R.layout.simple_list_item_1,
// filelist);
listView = (ListView) view.findViewById(R.id.listView1);
registerForContextMenu(listView);
listView.setAdapter(new listviewadapter());
Toast.makeText(MainActivity.this,"alist"+ Integer.toString(aList.size()), 1).show();
Toast.makeText(MainActivity.this, "path"+Integer.toString(pathr.size()), 1).show();
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
// filelist=new ArrayList<String>();
File file = new File(pathr.get(pathr.size()-1).get(position));
if (file.isDirectory()) {
getfilelist(pathr.get(pathr.size()-1).get(position));
// getfilelist(filelist.get(position));
// ArrayAdapter<String>adapter=new
// ArrayAdapter<String>(MainActivity.this,
// android.R.layout.simple_list_item_1, filelist);
// 这里的filelist取的是局部变量,就是新传值进去的filelist还在公共变量,这里取的是之前的值,所以,给函数add一个返回值。
// 这里返回的还是之前的值,所以错误,集合公共变量,getfilelist()得到的就是上次的集合,所以不会刷新,必须把集合设置为局部变量,在局部new才行
// 将集合在公共new,是为了递归,有一个特定的集合接受所有信息,而这里,则需要不同的集合,每一次传参数都得到新的实例,否则总会指向上一次
count++;
// Toast.makeText(MainActivity.this, Integer.toString(aList.size()), 1).show();
listView.setAdapter(new listviewadapter());
}
else {
Toast.makeText(MainActivity.this, "请长按加入书架", 1).show();
}
}
});
break;
case 1:
// linearLayoute.setAlpha(0.5f);
linearLayoute.setBackgroundColor(Color.parseColor("#FFCC66"));
linearLayout.setBackgroundColor(Color.parseColor("#00000000"));
linearLayoutr.setBackgroundColor(Color.parseColor("#00000000"));
getdata();
if (name.size()==0) {
Toast.makeText(MainActivity.this, "您还没有加入图书", 1).show();
}
else {
c=new c();
gridView=(GridView)view1.findViewById(R.id.gridView1);
gridView.setAdapter(c);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,novel.class);
intent.putExtra("path", pathList2.get(position));
File file=new File(pathList2.get(position));
if (!file.exists()) {
Toast.makeText(MainActivity.this, "请确认该文件存在", 1).show();
}
else{
intent.putExtra("name", name.get(position));
// Bundle bundle=new Bundle();
// bundle.putInt("progress", aIntegers.get(position));
// intent.putExtra("progress", bundle);
startActivity(intent);
}
}
});
}
// Toast.makeText(MainActivity.this, "tab1", Toast.LENGTH_LONG).show();
break;
// case 2:
// textViewforpage3.setText("9");
// linearLayoutq.setAlpha(0.5f);
// linearLayoutq.setBackgroundColor(Color.parseColor("#FFFF33"));
// linearLayoute.setBackgroundColor(Color.parseColor("#CCFF66"));
// linearLayout.setBackgroundColor(Color.parseColor("#CCFF66"));
// textViewmain.setBackgroundColor(Color.parseColor("#CCFF66"));
/* if (linearLayoute.getAlpha()==0.5f) {
linearLayoute.setAlpha(1.0f);
}
if (linearLayout.getAlpha()==0.5f) {
linearLayout.setAlpha(1.0f);
}
if (linearLayoutw.getAlpha()==0.5) {
linearLayoutw.setAlpha(1.0f);
}
if (linearLayoutr.getAlpha()==0.5) {
linearLayoutr.setAlpha(1.0f);
}
*/
// break;
case 2:
linearLayoutr.setBackgroundColor(Color.parseColor("#FFCC66"));
linearLayout.setBackgroundColor(Color.parseColor("#00000000"));
linearLayoute.setBackgroundColor(Color.parseColor("#00000000"));
ListView listView=(ListView)booknetview.findViewById(R.id.listView1);
listView.setAdapter(new booknet());
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this, netbook.class);
intent.putExtra("web", book[position]);
startActivity(intent);
}
});
break;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
viewPager.setCurrentItem(1);
viewPager.setCurrentItem(0);
}
//`````````````````````````````````````````````````````
public ArrayList<String> getfilelist(String path) {
// TODO Auto-generated method stub
File file = new File(path);
File[] files = file.listFiles();
filelist = new ArrayList<String>();
pathlist = new ArrayList<String>();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()
|| files[i].getName().endsWith("txt")) {
filelist.add(files[i].getName());
pathlist.add(files[i].getAbsolutePath());
}
}
aList.add(filelist);
pathr.add(pathlist);
return filelist;
}
class dxc extends PagerAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return arrayList.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView(arrayList.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
((ViewPager) container).addView(arrayList.get(position), 0);
return arrayList.get(position);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
// TODO Auto-generated method stub
super.restoreState(state, loader);
}
@Override
public void notifyDataSetChanged() {
// TODO Auto-generated method stub
super.notifyDataSetChanged();
}
}
/*
* @Override public void onBackPressed() { // TODO Auto-generated method
* stub count--; if (count>=0&&viewPager.getCurrentItem()==0){
* button.setText(a[count]); } if (count<0) { finish(); }
* Toast.makeText(MainActivity.this,
* Integer.toString(viewPager.getCurrentItem()), 1).show(); }
*/
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
count--;
if (count >= 0 && viewPager.getCurrentItem() == 0) {
aList.remove(aList.size() - 1);
pathr.remove(pathr.size()-1);
//多线程访问一个变量导致错误,alist的值没有及时传递
// adapter = new ArrayAdapter<String>(MainActivity.this,
// android.R.layout.simple_list_item_1,
// aList.get(aList.size() - 1));
// Toast.makeText(MainActivity.this, Integer.toString(aList.size()), 1).show();
listView.setAdapter(new listviewadapter());
} else {
super.onBackPressed();
}
}
class listviewadapter extends BaseAdapter {
final int type = 0;
final int type1 = 1;
public listviewadapter() {
layoutInflater = LayoutInflater.from(MainActivity.this);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return aList.get(aList.size()-1).size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return aList.get(aList.size()-1).get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
// Toast.makeText(MainActivity.this, Integer.toString(aList.size()), 1).show();
if ((aList.get(aList.size()-1).get(position)).endsWith("txt")) {
return type;
} else {
return type1;
}
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
int key = getItemViewType(position);
if (convertView == null) {
switch (key) {
case type:
//final作用
convertView = layoutInflater.inflate(R.layout.viewtype1,
parent, false);
textView = (TextView) convertView
.findViewById(R.id.textView1);
convertView.setTag(1);
break;
case type1:
convertView = layoutInflater.inflate(R.layout.viewt2,
parent, false);
textView1 = (TextView) convertView
.findViewById(R.id.textView2);
convertView.setTag(2);
default:
break;
}
} else {
switch (key) {
case type:
convertView.getTag(1);
break;
case type1:
convertView.getTag(2);
break;
}
}
switch (key) {
case type:
textView.setText(aList.get(aList.size()-1).get(position));
break;
case type1:
textView1.setText(aList.get(aList.size()-1).get(position));
}
return convertView;
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.main, menu);
menu.setQwertyMode(true);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterContextMenuInfo info=(AdapterContextMenuInfo)item.getMenuInfo();
int index=info.position;
int id = item.getItemId();
if (aList.get(aList.size()-1).get(index).endsWith("txt")) {
if (id == R.id.add) {
tab=aList.get(aList.size()-1).get(index);
String[] tabsplit=tab.split("\\.");
sqLiteDatabase.execSQL("insert into bookv values(null,?,'0',?)",new String[]{tabsplit[0],pathr.get(pathr.size()-1).get(index)});
Toast.makeText(MainActivity.this,"已经加入书架", Toast.LENGTH_LONG).show();
}
}
return super.onContextItemSelected(item);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return super.onOptionsItemSelected(item);
}
class shujuku extends SQLiteOpenHelper{
public shujuku(Context context) {
super(context, "sql", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
class c extends BaseAdapter{
int count1=0;
public c(){
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return name.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return name.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
count1++;
count1=count1%bookimage.length;
convertView=(View)layoutInflater.inflate(R.layout.item, null);
ImageView imageView=(ImageView)convertView.findViewById(R.id.imageView1);
TextView textView=(TextView)convertView.findViewById(R.id.textView1);
imageView.setImageResource(bookimage[count1]);
textView.setText(name.get(position));
return convertView;
}
}
void getdata(){
name=new ArrayList<String>();
pathList2=new ArrayList<String>();
Cursor cursor=sqLiteDatabase.rawQuery("select * from bookv", null);
// if (cursor.getCount()==0) {
//每次调用adapter都会调用这个方法,都会运行name.add(a+"/n"+p);结果每次都会增加一个,所以才会追加绘制!每个图片都不一样,表示的确不是重复绘制,而是数据源变化
// Toast.makeText(MainActivity.this, "您还没有加入图书", 1).show();
// name.add(" ");
// }
// else {
while (cursor.moveToNext()) {
String a=cursor.getString(cursor.getColumnIndex("name"));
Integer b=cursor.getInt(cursor.getColumnIndex("progress"));
String path=cursor.getString(cursor.getColumnIndex("path"));
File file=new File(path);
Float f=(float)b*10240/file.length();
// String p=Float.toString(f*100)+"%";
name.add(a);
// progress.add(b.toString());
pathList2.add(path);
aIntegers.add(b);
}
}
//}
class booknet extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return book.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return book[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
convertView=layoutInflater.inflate(R.layout.booknet, null);
ImageView imageView=(ImageView)convertView.findViewById(R.id.imageView1);
imageView.setImageResource(web[position]);
return convertView;
}
}
}
<pre name="code" class="java">package com.example.viewpagerok;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
@SuppressLint("SetJavaScriptEnabled") public class netbook extends Activity{
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
setContentView(R.layout.web);
String web=getIntent().getStringExtra("web");
webView=(WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(web);
webView.setWebViewClient(new webclient());
}
class webclient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode==KeyEvent.KEYCODE_BACK&&webView.canGoBack()) {
webView.goBack();
return true;
}
else {
finish();
return false;
}
}
}
package com.example.viewpagerok;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import android.R.integer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
@SuppressLint({ "ClickableViewAccessibility", "ShowToast", "InflateParams" }) public class novel extends Activity implements OnSeekBarChangeListener{
String pathString;
ScrollView scrollView;
int count;
int count1;
LinearLayout layout;
Integer progress;
TextView textView;
FileInputStream fileInputStream;
String string;
byte[]b;
byte[]q;
Integer progress1;
pop p;
Cursor cursor;
FileInputStream fileInputStreamx;
SQLiteDatabase bsqLiteDatabase;
String z;
touch buttontouch;
LayoutInflater layoutInflater1;
DisplayMetrics displayMetrics;
textsize t;
TextView textView666;
Float aFloat;
Integer progressforpauseInteger;
TextView textView111;
String cv;
TextView textView999;
Button button3;
View view1;
Integer j;
View view;
Cursor cursoraCursor;
Integer orginary;
boolean flag;
String name;
Cursor newyork;
SQLiteDatabase sqLiteDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
setContentView(R.layout.novel);
count=0;
count1=0;
Intent intent=getIntent();
pathString=intent.getStringExtra("path");
name=intent.getStringExtra("name");
textView=(TextView)findViewById(R.id.textview);
bsqLiteDatabase=new set(getApplicationContext()).getWritableDatabase();
bsqLiteDatabase.execSQL("create table if not exists j(id integer primary key autoincrement,ci varchar(9))");
bsqLiteDatabase.execSQL("create table if not exists h(id integer primary key autoincrement,size float)");
// sqLiteDatabase.execSQL("create table if not exists proeqb(id integer primary key autoincrement,path varchar(33),jindu integer,touch integer)");
bsqLiteDatabase.execSQL("create table if not exists "+name+" (id integer primary key autoincrement,pro integer,touch integer)");
Cursor cursorq=bsqLiteDatabase.rawQuery("select * from j", null);
if (cursorq.getCount()!=0) {
cursorq.moveToLast();
z=cursorq.getString(cursorq.getColumnIndex("ci"));
textView.setBackgroundColor(Color.parseColor(z));
}
Cursor c=bsqLiteDatabase.rawQuery("select * from h", null);
if (c.getCount()!=0) {
c.moveToLast();
aFloat=c.getFloat(c.getColumnIndex("size"));
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, aFloat);
}
// new Handler().post(new Runnable() {
// @Override
// public void run() {
// TODO Auto-generated method stub
// cursoraCursor=sqLiteDatabase.rawQuery("select * from proeqb where path=?",new String[]{pathString});
SQLiteDatabase sqLiteDatabase=new MainActivity().new shujuku(getApplicationContext()).getWritableDatabase();
// sqLiteDatabase.execSQL("select * from bookv where path =?" ,new String[]{pathString});
cursor=sqLiteDatabase.rawQuery("select * from bookv where path = ?", new String[]{pathString});
// if (cursoraCursor.getCount()==0) {
// orginary=0;
// }
while (cursor.moveToNext()) {
progress1=cursor.getInt(cursor.getColumnIndex("progress"));
}
scrollView = (ScrollView) findViewById(R.id.sc);
layout=(LinearLayout)findViewById(R.id.lo);
newyork=bsqLiteDatabase.rawQuery("select * from "+name, null);
File file=new File(pathString);
if (file.length()==0) {
Toast.makeText(getApplicationContext(), "没有内容", 1).show();
finish();
}
try {
fileInputStream=new FileInputStream(pathString);
if (newyork.getCount()==0) {
fileInputStream.skip(progress1*10240);
flag=false;
Toast.makeText(novel.this, "0", 1).show();
// orginary=0;
}
else {
flag=true;
newyork.moveToFirst();
j=newyork.getInt(newyork.getColumnIndex("pro"));
orginary=newyork.getInt(newyork.getColumnIndex("touch"));
fileInputStream.skip(j+10240*orginary);
// Toast.makeText(novel.this, "count!=0", 1).show();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
b=new byte[10240];
try {
string = new String(b, 0, fileInputStream.read(b), "GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
textView.setText(string);
scrollView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction()==MotionEvent.ACTION_UP){
int y=scrollView.getScrollY();
if (y==layout.getHeight()-scrollView.getHeight()) {
// cursoraCursor=sqLiteDatabase.rawQuery("select * from proeqb where path=?",new String[]{pathString});
count++;
count1++;
try {
// fileInputStream.read(b);
// cursoraCursor=sqLiteDatabase.rawQuery("select * from proeqb where path=?",new String[]{pathString});
textView.append(new String(b, 0, fileInputStream.read(b), "GBK"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// cursoraCursor=sqLiteDatabase.rawQuery("select * from proeqb where path=?",new String[]{pathString});
}
}
return false;
}
});
// }
// });
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
//安卓多线程访问一个变量,规则上是允许的,只是不允许子线程直接更新UI,还有就是数据错误,比如一个线程更新集合,另一个线程没收到数据,结果index超出了集合的总数,程序出错,但是规则上是允许的,只要不要求太精确,多线程还是好的。
// SQLiteDatabase sqLiteDatabase=new MainActivity().sqLiteDatabase;
// cursoraCursor=sqLiteDatabase.rawQuery("select * from proeqb where path=?",new String[]{pathString});
/* if (cursoraCursor.getCount()==0) {
SQLiteDatabase sqLiteDatabase=new MainActivity().new shujuku(getApplicationContext()).getWritableDatabase();
String a="UPDATE bookv SET progress = ";
String b=Integer.toString(count+progress1);
String c=" WHERE path = ";
// String c=" WHERE id = 0";
String e="'";
String f="'";
String d=pathString;
sqLiteDatabase.execSQL(a+b+c+e+d+f);
}
*/
// SQLiteDatabase sqLiteDatabase=new MainActivity().new shujuku(getApplicationContext()).getWritableDatabase();
newyork=bsqLiteDatabase.rawQuery("select * from "+name, null);
if (newyork.getCount()==0) {
SQLiteDatabase sqLiteDatabase=new MainActivity().new shujuku(getApplicationContext()).getWritableDatabase();
String a="UPDATE bookv SET progress = ";
String b=Integer.toString(count+progress1);
String c=" WHERE path = ";
// String c=" WHERE id = 0";
String e="'";
String f="'";
String d=pathString;
sqLiteDatabase.execSQL(a+b+c+e+d+f);
}
else {
newyork.moveToFirst();
Integer touch=newyork.getInt(newyork.getColumnIndex("touch"));
String a="UPDATE "+name+ " SET touch = ";
String b=Integer.toString(count1+touch);
String c=" WHERE id = 1";
// String e="'";
// String f="'";
// String d=pathString;
bsqLiteDatabase.execSQL(a+b+c);
}
super.onBackPressed();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode==KeyEvent.KEYCODE_MENU) {
displayMetrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
layoutInflater1=LayoutInflater.from(novel.this);
view1=layoutInflater1.inflate(R.layout.moon, null);
buttontouch=new touch();
Button button=(Button)view1.findViewById(R.id.button1);
button.setOnTouchListener(buttontouch);
button.setBackgroundColor(Color.parseColor("#00000000"));
Button button1=(Button)view1.findViewById(R.id.button2);
button1.setBackgroundColor(Color.parseColor("#00000000"));
button1.setOnTouchListener(buttontouch);
Button button2=(Button)view1.findViewById(R.id.button3);
button2.setBackgroundColor(Color.parseColor("#00000000"));
button2.setOnTouchListener(buttontouch);
button3=(Button)view1.findViewById(R.id.button4);
button3.setBackgroundColor(Color.parseColor("#00000000"));
button3.setOnTouchListener(buttontouch);
// button3.setBackgroundColor(Color.parseColor("#00000000"));
// PopupWindow popupWindow1=new PopupWindow(view1, 100, 100);
PopupWindow popupWindow1=new PopupWindow(view1, displayMetrics.widthPixels, LayoutParams.WRAP_CONTENT);
popupWindow1.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#999966")));
popupWindow1.setFocusable(true);
popupWindow1.setTouchable(true);
popupWindow1.setOutsideTouchable(true);
popupWindow1.showAtLocation(textView, Gravity.BOTTOM, 0, 0);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
p=new pop();
View view=LayoutInflater.from(novel.this).inflate(R.layout.a, null);
TextView textView1=(TextView)view.findViewById(R.id.textView1);
TextView textView2=(TextView)view.findViewById(R.id.textView2);
TextView textView3=(TextView)view.findViewById(R.id.textView3);
TextView textView4=(TextView)view.findViewById(R.id.textView4);
TextView textView5=(TextView)view.findViewById(R.id.textView5);
// Button textView6=(Button)view.findViewById(R.id.textView6);
textView1.setTag(1);
textView2.setTag(2);
textView3.setTag(3);
textView4.setTag(4);
textView5.setTag(5);
// textView6.setTag(6);
textView1.setOnClickListener(p);
textView2.setOnClickListener(p);
textView3.setOnClickListener(p);
textView4.setOnClickListener(p);
textView5.setOnClickListener(p);
// textView6.setOnClickListener(p);
// textView6.setText("\n"+"还"+"\n"+"原");
DisplayMetrics displayMetrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
PopupWindow popupWindow=new PopupWindow(view,displayMetrics.widthPixels,LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new ColorDrawable(0));
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.showAtLocation(textView, Gravity.CENTER, 0, 0);
/* if (popupWindow.isShowing()) {
button3=(Button)view1.findViewById(R.id.button4);
button3.setBackgroundColor(Color.parseColor("#00000000"));
button3.setOnTouchListener(buttontouch);
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setBackgroundColor(Color.parseColor("#00000000"));
bsqLiteDatabase.execSQL("delete from j");
}
});
}*/
}
});
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sqLiteDatabase=new MainActivity().new shujuku(getApplicationContext()).getWritableDatabase();
Cursor cursor=sqLiteDatabase.rawQuery("select * from bookv where path = "+"'"+pathString+"'", null);
cursor.moveToFirst();
Integer pro=cursor.getColumnIndex("progress");
View view=layoutInflater1.inflate(R.layout.qwer, null);
PopupWindow popupWindow=new PopupWindow(view, displayMetrics.widthPixels, LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.showAtLocation(textView, Gravity.CENTER, 0, 0);
SeekBar seekBar=(SeekBar)view.findViewById(R.id.seekBar1);
textView111=(TextView)view.findViewById(R.id.textview);
textView666=(TextView)view.findViewById(R.id.textView1);
textView999=(TextView)view.findViewById(R.id.textview2);
if (flag==true) {
Float gFloat=(float)( j+10240*orginary)/new File(pathString).length()*100;
BigDecimal bigDecimal=new BigDecimal(gFloat);
Float f1=bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
textView111.setText("complete:"+ Float.toString(f1)+"%");
}
else {
Float float1=(float) ((pro+count)*10240/new File(pathString).length())*100;
BigDecimal bigDecimal=new BigDecimal(float1);
Float f1=bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
textView111.setText("complete:"+Float.toString(f1)+"%");
}
seekBar.setOnSeekBarChangeListener(novel.this);
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
t=new textsize();
view=layoutInflater1.inflate(R.layout.liuyifeiliuyanrushen, null);
PopupWindow popupWindow=new PopupWindow(view, displayMetrics.widthPixels, LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.showAtLocation(textView, Gravity.CENTER, 0, 0);
Button textView1=(Button)view.findViewById(R.id.textView1);
Button textView2=(Button)view.findViewById(R.id.textView2);
Button textView3=(Button)view.findViewById(R.id.textView3);
Button textView4=(Button)view.findViewById(R.id.textView4);
Button textView5=(Button)view.findViewById(R.id.textView5);
Button textView6=(Button)view.findViewById(R.id.textView6);
// Button button=(Button)view.findViewById(R.id.textView7);
textView1.setBackgroundColor(Color.parseColor("#00000000"));
textView2.setBackgroundColor(Color.parseColor("#00000000"));
textView3.setBackgroundColor(Color.parseColor("#00000000"));
textView4.setBackgroundColor(Color.parseColor("#00000000"));
textView5.setBackgroundColor(Color.parseColor("#00000000"));
textView6.setBackgroundColor(Color.parseColor("#00000000"));
// button.setBackgroundColor(Color.parseColor("#00000000"));
textView1.setOnTouchListener(buttontouch);
textView2.setOnTouchListener(buttontouch);
textView3.setOnTouchListener(buttontouch);
textView4.setOnTouchListener(buttontouch);
textView5.setOnTouchListener(buttontouch);
textView6.setOnTouchListener(buttontouch);
textView1.setTag(1);
textView2.setTag(2);
textView3.setTag(3);
textView4.setTag(4);
textView5.setTag(5);
textView6.setTag(6);
// button.setTag(7);
textView1.setOnClickListener(t);
textView2.setOnClickListener(t);
textView3.setOnClickListener(t);
textView4.setOnClickListener(t);
textView5.setOnClickListener(t);
textView6.setOnClickListener(t);
// button.setOnClickListener(t);
/* if (popupWindow.isShowing()) {
button3=(Button)view.findViewById(R.id.button4);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
bsqLiteDatabase.execSQL("delete from h");
}
*/
}
});
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
View view=layoutInflater1.inflate(R.layout.restore, null);
PopupWindow popupWindow=new PopupWindow(view, displayMetrics.widthPixels, LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.showAtLocation(textView, Gravity.CENTER, 0, 0);
TextView htextView=(TextView)view.findViewById(R.id.textView1);
TextView textView1=(TextView)view.findViewById(R.id.textView2);
htextView.setOnTouchListener(buttontouch);
textView1.setOnTouchListener(buttontouch);
htextView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setBackgroundColor(Color.parseColor("#00000000"));
bsqLiteDatabase.execSQL("delete from j");
}
});
textView1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
bsqLiteDatabase.execSQL("delete from h");
}
});
}
});
return false;
}
else {
return super.onKeyDown(keyCode, event);
}
}
class pop implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int key=(Integer)v.getTag();
switch (key) {
case 1:
textView.setBackgroundColor(Color.parseColor("#00CC99"));
bsqLiteDatabase.execSQL("insert into j values(null,'#00CC99')");
Toast.makeText(novel.this, "text1", 1).show();
break;
case 2:
textView.setBackgroundColor(Color.parseColor("#99FF99"));
bsqLiteDatabase.execSQL("insert into j values(null,'#99FF99')");
Toast.makeText(novel.this, "text2", 1).show();
break;
case 3:
textView.setBackgroundColor(Color.parseColor("#99CCCC"));
bsqLiteDatabase.execSQL("insert into j values(null,'#99CCCC')");
Toast.makeText(novel.this, "text3", 1).show();
break;
case 4:
textView.setBackgroundColor(Color.parseColor("#99FFCC"));
bsqLiteDatabase.execSQL("insert into j values(null,'#99FFCC')");
Toast.makeText(novel.this, "text4", 1).show();
break;
case 5:
textView.setBackgroundColor(Color.parseColor("#CCCCFF"));
bsqLiteDatabase.execSQL("insert into j values(null,'#CCCCFF')");
Toast.makeText(novel.this, "text5", 1).show();
break;
// case 6:
// textView.setBackgroundColor(Color.parseColor("#00000000"));
// sqLiteDatabase.execSQL("delete from j");
}
}
}
class set extends SQLiteOpenHelper{
public set(Context context) {
super(context, "beijing", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
class textsize implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int key=(Integer)v.getTag();
switch (key) {
case 1:
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 9);
bsqLiteDatabase.execSQL("insert into h values(null,9)");
break;
case 2:
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
bsqLiteDatabase.execSQL("insert into h values(null,12)");
break;
case 3:
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
bsqLiteDatabase.execSQL("insert into h values(null,15)");
break;
case 4:
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
bsqLiteDatabase.execSQL("insert into h values(null,18)");
break;
case 5:
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 21);
bsqLiteDatabase.execSQL("insert into h values(null,21)");
break;
case 6:
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
bsqLiteDatabase.execSQL("insert into h values(null,24)");
break;
// case 7:
//textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
// sqLiteDatabase.execSQL("delete from h");
// break;
}
}
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
progressforpauseInteger=progress;
float a=(float)progress/100;
textView999.setText(Float.toString(a)+"%");
// textView999.setText(Double.toString(progress/100)+"%");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
// cursoraCursor=sqLiteDatabase.rawQuery("select * from proeqb where path=?",new String[]{pathString});
count1=0;
count=0;
try {
newyork=bsqLiteDatabase.rawQuery("select * from "+name, null);
fileInputStreamx = new FileInputStream(pathString);
File file=new File(pathString);
Integer cvqm=Math.round(file.length()*progressforpauseInteger/10000);
fileInputStreamx.skip(cvqm-1);
q=new byte[10240];
cv=new String(q, 0, fileInputStreamx.read(q), "GBK");
textView.setText(cv);
if (newyork.getCount()==0) {
bsqLiteDatabase.execSQL("insert into "+name+" values(null,?,0)",new Object[]{cvqm-1});
}
else {
bsqLiteDatabase.execSQL("update "+name+" set pro=?,touch=0 where id=1",new Object[]{cvqm-1});
// Toast.makeText(novel.this, "update", Toast.LENGTH_LONG).show();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class touch implements OnTouchListener{
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction()==MotionEvent.ACTION_UP) {
v.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
if (event.getAction()==MotionEvent.ACTION_DOWN) {
v.setBackgroundColor(Color.parseColor("#0099FF"));
}
return false;
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:安卓小说阅读器
原文地址:http://blog.csdn.net/nimeghbia/article/details/47659615