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

📄 progress.java

📁 anyview手机小说阅览器的开源代码
💻 JAVA
字号:
package com.ismyway.anyview;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * <p>Title: AnyView</p>
 *
 * <p>Description: E680(I) Reader</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: www.ismyway.com</p>
 *
 * @author ZhangJian
 * @version 1.0
 */
public class Progress extends AbstractWindow {
    public final static int ANCHOR = Graphics.LEFT | Graphics.TOP;
    private int percent = 0; //保存的值
    protected byte[][] byteItems; //选项的字节数组形式
    protected Image windowImage; //保存当前图体的Image对象
    private String[] items;

    protected Image selector;
    protected int itemIndex = 0;
    protected int selectedIndex = -1;
    protected boolean visible = true; //当前是否可见

    final static int HorPad = 10;
    final static int VecPad = 25;
    final static int LinePad = 5;
    final static int ShowItems = 10;

    /*protected Selector(String title) {
        this.title = title;
         }*/

    public Progress(int percent) {
        this.percent = percent;
        items = new String[] {"← 10% →", "← 5% →", "← 1% →", " 确  定 ", " 取  消 "};
        this.byteItems = new byte[items.length][];

        selectedIndex = 2;
        itemIndex = 2;

        //确定当前窗口的宽
        int max = 0;
        for (int i = 0; i < byteItems.length; i++) {
            byteItems[i] = AnyView.cf.str2bytes(items[i]);
            int c = byteItems[i].length;
            if (c >= max) {
                max = c;
            }
        }
        this.width = max * AnyView.cf.AsciiWidth + (HorPad << 1);

        //确定当前窗口的高
        this.height = items.length * (AnyView.cf.FontHeight + LinePad) -
                      LinePad + VecPad + HorPad;

        if (AnyView.readType == 1) { //横屏
            this.LEFT = (AnyView.ScreenWidth - this.height) >> 1;
            this.TOP = (AnyView.ScreenHeight + this.width) >> 1;
        } else {
            this.LEFT = (AnyView.ScreenWidth - this.width) >> 1;
            this.TOP = (AnyView.ScreenHeight - this.height) >> 1;
        }
        this.LEFT = this.LEFT < 0 ? 0 : this.LEFT;
        this.TOP = this.TOP < 0 ? 0 : this.TOP;

        //准备显示图像
        prepare();
    }

    /**
     * 创建当前可显示窗口的缓冲图
     */
    protected void prepare() {
        Image img;
        selector = Image.createImage(width, height);
        Graphics g = selector.getGraphics();
        g.setColor(AnyView.background);
        g.fillRect(0, 0, width, height);
        g.setColor(AnyView.bordorcolor);
        g.drawRect(0, 0, width - 1, height - 1);
        g.drawLine(0, 20, width, 20);

        byte[] t = AnyView.cf.str2bytes("当前: " + percent + "%");
        int w = t.length * AnyView.cf.AsciiWidth;
        int h = AnyView.cf.FontHeight;
        int y = (20 - h) >> 1;
        int x = (width - w) >> 1;
        AnyView.cf.setColor(AnyView.titlecolor);
        img = AnyView.cf.buildImage(t);
        g.drawImage(img, x, y, ANCHOR);

        int top = VecPad;
        AnyView.cf.setColor(AnyView.fontcolor);

        int offset = 0;
        for (int i = 0; i < ShowItems && offset < byteItems.length; i++) {
            img = AnyView.cf.buildImage(byteItems[offset++]);
            g.drawImage(img, (width - img.getWidth()) >> 1, top, ANCHOR);
            top += AnyView.cf.FontHeight + LinePad;
        }
    }

    /**
     * 仅通过itemIndex、selectedIndex和itemPages来返回当前窗口
     * @return Image
     */
    public Image getWindow() {
        windowImage = null;
        windowImage = Image.createImage(width, height);
        Graphics g = windowImage.getGraphics();
        g.drawImage(selector, 0, 0, ANCHOR);

        if (itemIndex > -1) { //绘制选择时背景
            int off = itemIndex;
            int y = VecPad + off * (CustomFont.FontHeight + LinePad) - 1;
            g.setStrokeStyle(Graphics.DOTTED);
            g.setColor(0xffffff);
            g.drawRoundRect(2, y, width - 5, CustomFont.FontHeight + 2, 8, 8);
        }

        //重绘标题栏
        g.setColor(AnyView.background);
        g.fillRect(1, 1, width - 2, 18);

        byte[] t = AnyView.cf.str2bytes("当前: " + percent + "%");
        int w = t.length * AnyView.cf.AsciiWidth;
        int h = AnyView.cf.FontHeight;
        int y = (20 - h) >> 1;
        int x = (width - w) >> 1;
        AnyView.cf.setColor(AnyView.titlecolor);
        Image img = AnyView.cf.buildImage(t);
        g.drawImage(img, x, y, ANCHOR);
        img = null;

        return windowImage;
    }

    public void destroy() {
        byteItems = null;
        //itemImage = null;
        windowImage = null;
        selector = null;
        System.gc();
        //System.out.println("destroy() " + Runtime.getRuntime().freeMemory());
    }

    public void move(int step) {
        if (byteItems == null || byteItems.length < 1) {
            return;
        }

        itemIndex += step;
        itemIndex = itemIndex < 0 ? byteItems.length - 1 : itemIndex;
        itemIndex = itemIndex > byteItems.length - 1 ? 0 : itemIndex;
    }

    public int getSelectedIndex() {
        return selectedIndex;
    }


    public void keyUp() {
        move( -1);
    }

    public void keyDown() {
        move(1);
    }

    public void keyLeft() {
        switch (itemIndex) {
        case 0:
            percent -= 10;
            percent = percent < 0 ? 0 : percent;
            break;
        case 1:
            percent -= 5;
            percent = percent < 0 ? 0 : percent;
            break;
        case 2:
            percent--;
            percent = percent < 0 ? 0 : percent;
            break;
        }
    }

    public void keyRight() {
        switch (itemIndex) {
        case 0:
            percent += 10;
            percent = percent > 100 ? 100 : percent;
            break;
        case 1:
            percent += 5;
            percent = percent > 100 ? 100 : percent;
            break;
        case 2:
            percent++;
            percent = percent > 100 ? 100 : percent;
            break;
        }
    }

    public int getCurrentValue() {
        return percent;
    }

    public void keyFire() {
        selectedIndex = itemIndex;
        visible = false;
    }

    public boolean bof() {
        return false;
    }

    public boolean getVisible() {
        return visible;
    }

    protected void pointerPressed(int x, int y) {
        super.pointerPressed(x, y);

        if (!contains(PRESSED_X, PRESSED_Y)) {
            //WINDOW_STATE = CLOSED;
            return;
        }

        //判断用户当前的选择项
        int find = -1;
        for (int i = 0; i < items.length && find == -1; i++) {
            int top = VecPad + i * (CustomFont.FontHeight + LinePad) - 1;
            if (PRESSED_Y > top && PRESSED_Y < top + CustomFont.FontHeight) {
                find = i;
                itemIndex = i;
                //System.out.println("pressed: itemIndex = " + itemIndex);
            }
        }

    }

    protected void pointerDragged(int x, int y) {

    }

    protected void pointerReleased(int x, int y) {
        TOUCH_EVENT = false;
        super.pointerReleased(x, y);

        if (!contains(RELEASE_X, RELEASE_Y) && !contains(PRESSED_X, PRESSED_Y)) {
            WINDOW_STATE = CLOSED;
            return;
        }

        //判断用户当前的选择项
        int find = -1;
        for (int i = 0; i < items.length && find == -1; i++) {
            int top = VecPad + i * (CustomFont.FontHeight + LinePad) - 1;
            if (RELEASE_Y > top && RELEASE_Y < top + CustomFont.FontHeight) {
                find = i;
                //System.out.println("released: find = " + find);
            }
        }

        if (find == -1) {
            return;
        }

        if (find == itemIndex) { //用户确认选择功能
            TOUCH_EVENT = true;
            boolean left = RELEASE_X < width >> 1;
            if (find == 3 || find == 4) {
                keyFire();
            } else {
                if (left) {
                    keyLeft();
                } else {
                    keyRight();
                }
            }
        }

        else {
            itemIndex = find;
        }
    }
}

⌨️ 快捷键说明

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