android阅读器开发

android阅读器开发

package com.duduli.li;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class Ibook2Activity extends Activity {
    private ScrollView sv;
    private TextView tv;
    private RandomAccessFile raf;
    private int scrollViewY = 0;
    private StringBuilder sb;
    private long currentPoint;
    private long fileLeght;
    private boolean flag = true;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
//        sv = (ScrollView) this.findViewById(R.id.scrollView);
        tv = (TextView) this.findViewById(R.id.textView);
        sv = (ScrollView) this.findViewById(R.id.scrollView);
        
        
        
        File file = new File("/sdcard/test.txt");
        try {
            raf = new RandomAccessFile(file, "r");
            fileLeght = raf.length();
            System.out.println("文件总长度: "+fileLeght);
            sb = new StringBuilder();
            for(int i=0; i<=50;i++){
                if(raf.getFilePointer() != fileLeght){
//                    string += new String(raf.readLine().getBytes("8859_1"), "gb2312")+("\r\n");
                    sb.append(new String(raf.readLine().getBytes("8859_1"), "gb2312")+("\r\n"));
    //                break;
            }else{
                break;
            }
            }
            
//            System.out.println(sb);
            currentPoint = raf.getFilePointer();
            System.out.println(currentPoint);
            tv.setText(sb);
//            tv.setText(string);
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
        sv.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
//                string = "";
                if(event.getAction() == MotionEvent.ACTION_UP){
                    if (sv.getScrollY() == 0) {  
                        System.out.println("up");
                        
                    
                    } else if (sv.getScrollY() - scrollViewY < 2 && sv.getScrollY() >= scrollViewY) {  
                        try {
                            
                            if(flag){
                                System.out.println("begin");
                                raf.seek(currentPoint);
                                
                                sb.delete(0, sb.length());
                                for(int i=0; i<=50;i++){
                                    if(raf.getFilePointer() != fileLeght){
//                                        string += new String(raf.readLine().getBytes("8859_1"), "gb2312")+("\r\n");
                                        sb.append(new String(raf.readLine().getBytes("8859_1"), "gb2312")+("\r\n"));
//                                        break;
                                    }else{
                                        
//                                        
                                        flag = false;
                                        break;
                                    }
                            }
                            
                                
                                
//                            System.out.println(string);
                            
                            currentPoint = raf.getFilePointer();
                            tv.setText(sb);
//                            tv.setText(string);
                            sv.scrollTo(0, 0);
                            
                            }
                            
                            
                            
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    } else {  
                        scrollViewY = sv.getScrollY();
//                        System.out.println(sv.getScrollY());
                    }  
                    if(!flag && (tv.getHeight() - sv.getScrollY() <= 362)){
                            Toast.makeText(getApplicationContext(), "本文已结束",
                                     Toast.LENGTH_SHORT).show();
                    }
                    System.out.println(tv.getHeight());
                    System.out.println(sv.getScrollY());
                    
                }
                
                return false;
            }
            
        });
       }
    
    
    
}

 

posted @ 2015-05-28 16:00  harry_ma  阅读(1045)  评论(0编辑  收藏  举报
 aa
    部分内容来自网络,如有异议,请联系我.     


      成绩来自坚持, 灵感来自激情。

                                                               感谢父母和亲朋好友的爱与支持!