⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 txtreadercanvas.java

📁 很好的基于java的手机文本阅读器anyview 2.0源码,
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        int start = ((TxtLine) mylines.elementAt(0)).offset;
        AnyView.cf.setColor(ColorSelector.colors[AnyView.frontColor]);

        int searchIndex = 0;
        int searchStart = ((TxtLine) mylines.elementAt(temp)).offset;
        if (needsearch) {
            for (searchIndex = 0; searchIndex < finds.size(); searchIndex++) {
                int pos = ((Integer) finds.elementAt(searchIndex)).intValue();
                if (pos >= searchStart) {
                    break;
                }
            }
        }

        //绘制进度指示器
        if (AnyView.guageheight > 0) {
            int y = viewHeight - AnyView.guageheight;
            int x = ((TxtLine) mylines.elementAt(currentLine)).offset *
                    viewWidth / txtLenght;
            g.setColor(0x0000ff);
            g.fillRect(0, y, x, AnyView.guageheight);
            g.setColor(0xffffff);
            g.fillRect(x, y, viewWidth - x, AnyView.guageheight);
        }

        while (temp < mylines.size() && line < lineOfPage) {
            TxtLine tl = (TxtLine) mylines.elementAt(temp);
            int len = 0;
            if (temp != mylines.size() - 1) {
                len = ((TxtLine) mylines.elementAt(temp + 1)).offset
                      - tl.offset;
            } else {
                len = start + bufferTxt.length - tl.offset;
            }
            byte[] b = new byte[len];
            System.arraycopy(bufferTxt, tl.offset - start, b, 0, len);
            Image img = AnyView.cf.buildImage(b);
            g.drawImage(img, 0, yOff, ANCHOR);
            if (needsearch) { //高亮显示搜索结果
                if (searchIndex < finds.size()) {
                    while (true && searchIndex < finds.size()) { //查找当前行所有的搜索结果
                        int pos = ((Integer) finds.elementAt(searchIndex)).
                                  intValue();
                        if (pos >= tl.offset && pos < tl.offset + tl.lenght) { //该行需要高亮显示
                            int xoff = (pos - tl.offset) *
                                       AnyView.cf.AsciiWidth;
                            g.drawImage(serachImage, xoff, yOff, ANCHOR);
                            searchIndex++;
                        } else {
                            break;
                        }
                    }
                }
                /*int nextLine = pos + searchStrLenght - tl.offset - tl.lenght;
                                 if (nextLine > 0) {
                    int xoff = (nextLine - searchStrLenght) *
                               AnyView.cf.AsciiWidth;
                 g.drawImage(serachImage, xoff, yOff + AnyView.cf.FontHeight +
                                AnyView.lineSpace, ANCHOR);
                                 }*/
            }

            yOff += (AnyView.cf.FontHeight + AnyView.lineSpace);
            line++;
            temp++;
        }

        //绘制搜索到的文字
        if (!needsearch || serachImage == null) {
            return;
        }
        //g.drawImage(serachImage, 0, 0, ANCHOR);
        //needsearch = false;
    }

    /**
     * 向前翻页
     * @param lines int 向前翻页的行数
     */
    void preparePreImage(int lines) {
        int oldCurrentOffset = 0;
        if (currentLine - lines < 0) { //当前缓冲不够显示
            currentLine = 0;
            if (bof) { //已到了文档的开始位置
                //什么也不做
            } else { //向前读缓冲
                oldCurrentOffset = currentOffset =
                        ((TxtLine) mylines.elementAt(currentLine)).
                        offset;
                eof = false;
                //currentOffset--; //移动到上一个结束处

                if (currentOffset - BufferLenght <= 0) {
                    bof = true;
                    currentOffset = 0;
                } else {
                    currentOffset -= BufferLenght;
                }
                fsa.locate(currentOffset);
                bufferbuffer = new byte[0];
                readNextBuffer();
                analysisBuffer();
                System.gc();
                for (currentLine = 0; currentLine < mylines.size();
                                   currentLine++) {
                    TxtLine tl = (TxtLine) mylines.elementAt(currentLine);
                    if (tl.offset >= oldCurrentOffset) {
                        break;
                    }
                }
                currentLine -= lines;
                currentLine = currentLine < 0 ? 0 : currentLine;
            }
        } else {
            currentLine -= lines;
        }
        currentOffset = ((TxtLine) mylines.elementAt(currentLine)).offset;

        //创建缓冲图像
        bufferImage = null;
        bufferImage = Image.createImage(viewWidth, viewHeight);
        Graphics g = bufferImage.getGraphics();
        g.setColor(ColorSelector.colors[AnyView.backColor]);
        g.fillRect(0, 0, viewWidth, viewHeight);

        if (background != null) {
            if (rotate) {
                g.drawRegion(background, 0, 0, background.getWidth(),
                             background.getHeight()
                             , Sprite.TRANS_ROT90, 0, 0,
                             ANCHOR);
            } else {
                g.drawImage(background, 0, 0, ANCHOR);
            }
        }

        int line = 0;
        int yOff = 0;
        int temp = currentLine;
        int start = ((TxtLine) mylines.elementAt(0)).offset;
        AnyView.cf.setColor(ColorSelector.colors[AnyView.frontColor]);

        int searchIndex = 0;
        int searchStart = ((TxtLine) mylines.elementAt(temp)).offset;
        if (needsearch) {
            for (searchIndex = 0; searchIndex < finds.size(); searchIndex++) {
                int pos = ((Integer) finds.elementAt(searchIndex)).intValue();
                if (pos >= searchStart) {
                    break;
                }
            }
        }

        //绘制进度指示器
        if (AnyView.guageheight > 0) {
            int y = viewHeight - AnyView.guageheight;
            int x = ((TxtLine) mylines.elementAt(currentLine)).offset *
                    viewWidth / txtLenght;
            g.setColor(0x0000ff);
            g.fillRect(0, y, x, AnyView.guageheight);
            g.setColor(0xffffff);
            g.fillRect(x, y, viewWidth - x, AnyView.guageheight);
        }

        while (temp < mylines.size() && line < lineOfPage) {
            TxtLine tl = (TxtLine) mylines.elementAt(temp);
            int len = 0;
            if (temp != mylines.size() - 1) {
                len = ((TxtLine) mylines.elementAt(temp + 1)).offset
                      - tl.offset;
            } else {
                len = start + bufferTxt.length - tl.offset;
            }
            byte[] b = new byte[len];
            System.arraycopy(bufferTxt, tl.offset - start, b, 0, len);
            Image img = AnyView.cf.buildImage(b);
            g.drawImage(img, 0, yOff, ANCHOR);
            if (needsearch) { //高亮显示搜索结果
                while (true && searchIndex < finds.size()) { //查找当前行所有的搜索结果
                    int pos = ((Integer) finds.elementAt(searchIndex)).
                              intValue();
                    if (pos >= tl.offset && pos < tl.offset + tl.lenght) { //该行需要高亮显示
                        int xoff = (pos - tl.offset) *
                                   AnyView.cf.AsciiWidth;
                        g.drawImage(serachImage, xoff, yOff, ANCHOR);
                        searchIndex++;
                    } else {
                        break;
                    }
                }
            }

            yOff += (AnyView.cf.FontHeight + AnyView.lineSpace);
            line++;
            temp++;
        }
    }

    protected void paint(Graphics g) {
        //g.setColor(ColorSelector.colors[AnyView.backColor]);
        //g.fillRect(0, 0, viewWidth, viewHeight);

        if (bufferImage != null) {
            if (rotate) {
                g.drawRegion(bufferImage, 0, 0, viewWidth, viewHeight,
                             Sprite.TRANS_ROT270, 0, 0, ANCHOR);
            } else {
                g.drawImage(bufferImage, 0, 0, ANCHOR);
            }
        }

        if (showMenu) {
            if (currentWindow != null) {
                if (AnyView.readType == 1) { //横屏
                    g.drawRegion(currentWindow.getWindow(), 0, 0,
                                 currentWindow.getWidth(),
                                 currentWindow.getHeight(),
                                 Sprite.TRANS_ROT270,
                                 currentWindow.getLeft(),
                                 currentWindow.getTop() -
                                 currentWindow.getWidth(),
                                 ANCHOR);

                } else {
                    g.drawImage(currentWindow.getWindow(),
                                currentWindow.getLeft(),
                                currentWindow.getTop(), ANCHOR);
                }
            } else {
                if (AnyView.readType == 1) { //横屏
                    g.drawRegion(menu.getWindow(), 0, 0, menu.getWidth(),
                                 menu.getHeight(), Sprite.TRANS_ROT270,
                                 menu.getLeft(),
                                 menu.getTop() - menu.getWidth(),
                                 ANCHOR);
                } else {
                    g.drawImage(menu.getWindow(), menu.getLeft(),
                                menu.getTop(),
                                ANCHOR);
                }
            }
        }

        if (message.getWindowState() == AbstractWindow.OPENING) {
            if (AnyView.readType == 1) { //横屏
                g.drawRegion(message.getWindow(), 0, 0, message.getWidth(),
                             message.getHeight(), Sprite.TRANS_ROT270,
                             message.getLeft(),
                             message.getTop() - message.getWidth(),
                             ANCHOR);
            } else {
                g.drawImage(message.getWindow(), message.getLeft(),
                            message.getTop(), ANCHOR);
            }
        }

        //AnyView.debug(g, AnyView.usedMemory(), 10, 10, ANCHOR);
    }

    /**
     * 根据旋转模式计算当前系统参数
     * @param rotate boolean
     */
    private void rotate(boolean rotate) {
        AnyView.readType = rotate ? 1 : 0;

        //计算当前的可视区域
        if (rotate) {
            viewWidth = srcH;
            viewHeight = srcW;
        } else {
            viewWidth = srcW;
            viewHeight = srcH;
        }

        if (mylines.size() > 0) {
            currentOffset = ((TxtLine) mylines.elementAt(currentLine)).
                            offset;
        }

        //计算当前一页可以显示多少行文字
        lineOfPage = viewHeight /
                     (AnyView.cf.FontHeight + AnyView.lineSpace);
    }

    public void keyPressed(int keyCode) {
        keyCode = mapKey(keyCode);

        if (message.getWindowState() == AbstractWindow.OPENING) {
            message.keyFire();
            repaint();
            return;
        }

        if (currentWindow != null && currentWindow instanceof Progress) {
            Progress progress = (Progress) currentWindow;
            if (keyCode == -3) { //Left
                progress.keyLeft();
            }
            if (keyCode == -4) { //Right
                progress.keyRight();
            }
            if (keyCode == -2) { //Down
                progress.keyDown();
            }
            if (keyCode == -1) { //UP
                progress.keyUp();
            }
            if (keyCode == -5) { //Fire
                progress.keyFire();
                System.gc();
                if (progress.getSelectedIndex() == 3) { //选择了确定
                    currentOffset = progress.getCurrentValue() * txtLenght /
                                    100 - 2048;
                    currentOffset = currentOffset < 0 ? 0 : currentOffset;
                    fsa.locate(currentOffset);
                    byte[] b = fsa.read(1);
                    while (currentOffset < txtLenght && b[0] != 10) {
                        b = fsa.read(1);
                        currentOffset++;
                    }
                    bufferbuffer = new byte[0]; //清缓冲的缓冲
                    readNextBuffer();
                    analysisBuffer();
                    //currentLine = 0;
                    prepareNextImage();
                    currentWindow = null;
                    menu = null;
                    showMenu = false;
                } else if (progress.getSelectedIndex() == 4) { //选择了取消
                    currentWindow = null;
                    menu = null;
                    showMenu = false;
                } else {
                    //什么也不做
                }
            }
            repaint();
            return;
        }

        //Left
        if (keyCode == -3) {
            if (showMenu) {
                if (currentWindow == null) { //处理菜单上的事件
                    //关闭菜单
                    showMenu = false;
                    menu = null;
                    System.gc();
                } else { //处理弹出窗口的事件
                    if (currentWindow.bof()) { //关闭弹出窗口
                        currentWindow.destroy();
                        currentWindow = null;
                    } else {
                        currentWindow.keyLeft();
                    }
                }
                repaint();
                return;
            } else {
                preparePreImage(lineOfPage);
                repaint();
                return;
            }
        }

        //Right
        if (keyCode == -4) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -