0

我得到了搜索栏,所以可以滑动。当我滑动时,我会得到当前值。值在 10-35 之间。当我得到值时,我需要应用标签字段的字体大小并立即更改字体大小。我调用了invalidate()after 获取值,但它不会改变字体大小。

public class Custom_FontField extends Field {
private final static int STATE_NORMAL = 0;
private final static int STATE_FOCUSED = 1;
private final static int STATE_PRESSED = 2;

private final static int NUM_STATES = 3;

private Bitmap[] _thumb = new Bitmap[NUM_STATES];
private Bitmap[] _progress = new Bitmap[NUM_STATES];
private Bitmap[] _base = new Bitmap[NUM_STATES];
private Bitmap[] _progressTile = new Bitmap[NUM_STATES];
private Bitmap[] _baseTile = new Bitmap[NUM_STATES];

private int _thumbWidth;
private int _thumbHeight;
private int _progressHeight;
private int _baseWidth;
private int _baseHeight;

private int _leftCapWidth;
private int _rightCapWidth;

private boolean _focused;
private boolean _pressed;

private int _numValues;
private int _currentValue;

private int _preferredHeight;

private int _baseY;
private int _progressY;
private int _thumbY;
private int _trackWidth;

private int _rop;

public Custom_FontField(Bitmap normalThumb, Bitmap normalProgress,
        Bitmap normalBase, Bitmap focusedThumb, Bitmap focusedProgress,
        Bitmap focusedBase, int numValues, int initialValue,
        int leftCapWidth, int rightCapWidth) {
    this(normalThumb, normalProgress, normalBase, focusedThumb,
            focusedProgress, focusedBase, null, null, null, numValues,
            initialValue, leftCapWidth, rightCapWidth, 0);
}

public Custom_FontField(Bitmap normalThumb, Bitmap normalProgress,
        Bitmap normalBase, Bitmap focusedThumb, Bitmap focusedProgress,
        Bitmap focusedBase, int numValues, int initialValue,
        int leftCapWidth, int rightCapWidth, long style) {
    this(normalThumb, normalProgress, normalBase, focusedThumb,
            focusedProgress, focusedBase, null, null, null, numValues,
            initialValue, leftCapWidth, rightCapWidth, style);
}

public Custom_FontField(Bitmap normalThumb, Bitmap normalProgress,
        Bitmap normalBase, Bitmap focusedThumb, Bitmap focusedProgress,
        Bitmap focusedBase, Bitmap pressedThumb, Bitmap pressedProgress,
        Bitmap pressedBase, int numValues, int initialValue,
        int leftCapWidth, int rightCapWidth) {
    this(normalThumb, normalProgress, normalBase, focusedThumb,
            focusedProgress, focusedBase, pressedThumb, pressedProgress,
            pressedBase, numValues, initialValue, leftCapWidth,
            rightCapWidth, 0);
}

public Custom_FontField(Bitmap normalThumb, Bitmap normalProgress,
        Bitmap normalBase, Bitmap focusedThumb, Bitmap focusedProgress,
        Bitmap focusedBase, Bitmap pressedThumb, Bitmap pressedProgress,
        Bitmap pressedBase, int numValues, int initialValue,
        int leftCapWidth, int rightCapWidth, long style) {
    super(style);

    if (numValues < 2 || initialValue >= numValues) {
        throw new IllegalArgumentException("invalid value parameters");
    }
    if (normalThumb == null || normalProgress == null || normalBase == null
            || focusedThumb == null || focusedProgress == null
            || focusedBase == null) {
        throw new IllegalArgumentException(
                "thumb, normal, focused  are required");
    }

    _thumbWidth = normalThumb.getWidth();
    _thumbHeight = normalThumb.getHeight();
    _progressHeight = normalProgress.getHeight();
    _baseWidth = normalBase.getWidth();
    _baseHeight = normalBase.getHeight();

    if (focusedThumb.getWidth() != _thumbWidth
            || focusedThumb.getHeight() != _thumbHeight
            || focusedProgress.getHeight() != _progressHeight
            || focusedBase.getHeight() != _baseHeight) {
        throw new IllegalArgumentException(
                "all base bitmaps and all progress bitmaps must be the same height");
    }
    if (pressedThumb != null && pressedProgress != null
            && pressedBase != null) {
        if (pressedThumb.getWidth() != _thumbWidth
                || pressedThumb.getHeight() != _thumbHeight
                || pressedProgress.getHeight() != _progressHeight
                || pressedBase.getHeight() != _baseHeight) {
            throw new IllegalArgumentException(
                    "all base bitmaps and all progress bitmaps must be the same height");
        }
    }

    _leftCapWidth = leftCapWidth;
    _rightCapWidth = rightCapWidth;

    _rop = Graphics.ROP_SRC_COPY;

    initBitmaps(normalThumb, normalProgress, normalBase, STATE_NORMAL);
    initBitmaps(focusedThumb, focusedProgress, focusedBase, STATE_FOCUSED);

    if (pressedThumb != null && pressedProgress != null
            && pressedBase != null) {
        initBitmaps(pressedThumb, pressedProgress, pressedBase,
                STATE_PRESSED);
    } else {
        _thumb[STATE_PRESSED] = _thumb[STATE_FOCUSED];
        _progress[STATE_PRESSED] = _progress[STATE_FOCUSED];
        _base[STATE_PRESSED] = _base[STATE_FOCUSED];
        _progressTile[STATE_PRESSED] = _progressTile[STATE_FOCUSED];
        _baseTile[STATE_PRESSED] = _baseTile[STATE_FOCUSED];
    }

    _preferredHeight = Math.max(_thumbHeight,
            Math.max(_progressHeight, _baseHeight));

    _numValues = numValues;
    setValue(initialValue);
}

public void initBitmaps(Bitmap thumb, Bitmap progress, Bitmap base,
        int state) {
    if (progress.getWidth() <= _leftCapWidth
            || base.getWidth() <= _rightCapWidth) {
        throw new IllegalArgumentException();
    }

    if (thumb.hasAlpha() || progress.hasAlpha() || base.hasAlpha()) {
        _rop = Graphics.ROP_SRC_ALPHA;
    }

    _thumb[state] = thumb;
    _progress[state] = progress;
    _base[state] = base;

    int[] argbCopyBuffer;

    int progressTileWidth = progress.getWidth() - _leftCapWidth;
    int progressTileHeight = progress.getHeight();

    Bitmap progressTile = new Bitmap(progressTileWidth, progressTileHeight);

    argbCopyBuffer = new int[progressTileWidth * progressTileHeight];
    progress.getARGB(argbCopyBuffer, 0, progressTileWidth, _leftCapWidth,
            0, progressTileWidth, progressTileHeight);
    progressTile.setARGB(argbCopyBuffer, 0, progressTileWidth, 0, 0,
            progressTileWidth, progressTileHeight);

    int baseTileWidth = base.getWidth() - _rightCapWidth;
    int baseTileHeight = base.getHeight();

    Bitmap baseTile = new Bitmap(baseTileWidth, baseTileHeight);

    argbCopyBuffer = new int[baseTileWidth * baseTileHeight];
    base.getARGB(argbCopyBuffer, 0, baseTileWidth, 0, 0, baseTileWidth,
            baseTileHeight);
    baseTile.setARGB(argbCopyBuffer, 0, baseTileWidth, 0, 0, baseTileWidth,
            baseTileHeight);

    _progressTile[state] = progressTile;
    _baseTile[state] = baseTile;
}

public void setValue(int newValue) {
    if (newValue < 0 || newValue >= _numValues) {
        throw new IllegalArgumentException();
    }
    _currentValue = newValue;
    fieldChangeNotify(FieldChangeListener.PROGRAMMATIC);
    invalidate();
}

public int getValue() {
    return _currentValue;
}

public int getNumValues() {
    return _numValues;
}

public int getPreferredWidth() {
    return Integer.MAX_VALUE;
}

public int getPreferredHeight() {
    return _preferredHeight;
}

protected void layout(int width, int height) {
    width = Math.min(width, getPreferredWidth());
    height = Math.min(height, getPreferredHeight());

    _progressY = (height - _progressHeight) / 2;
    _baseY = (height - _baseHeight) / 2;
    _thumbY = (height - _thumbHeight) / 2;

    _trackWidth = width - _leftCapWidth - _rightCapWidth;

    setExtent(width, height);
}

public void paint(Graphics g) {
    int contentWidth = getContentWidth();

    int thumbX = _leftCapWidth + (_trackWidth - _thumbWidth)
            * _currentValue / (_numValues - 1);
    int transitionX = thumbX + _thumbWidth / 2;

    int currentState = _pressed ? STATE_PRESSED : (_focused ? STATE_FOCUSED
            : STATE_NORMAL);

    Bitmap thumb = _thumb[currentState];
    Bitmap progress = _progress[currentState];
    Bitmap base = _base[currentState];
    Bitmap progressTile = _progressTile[currentState];
    Bitmap baseTile = _baseTile[currentState];

    g.drawBitmap(0, _progressY, _leftCapWidth, _progressHeight, progress,
            0, 0);
    g.tileRop(_rop, _leftCapWidth, _progressY, transitionX - _leftCapWidth,
            _progressHeight, progressTile, 0, 0);

    g.drawBitmap(contentWidth - _rightCapWidth, _baseY, _rightCapWidth,
            _baseHeight, base, _baseWidth - _rightCapWidth, 0);
    g.tileRop(_rop, transitionX, _baseY, contentWidth - transitionX
            - _rightCapWidth, _baseHeight, baseTile, 0, 0);

    g.drawBitmap(thumbX, _thumbY, _thumbWidth, _thumbHeight, thumb, 0, 0);
}

protected void drawFocus(Graphics g, boolean on) {

}

protected void onFocus(int direction) {
    _focused = true;
    invalidate();
    super.onFocus(direction);
}

protected void onUnfocus() {
    _focused = false;
    invalidate();
    super.onUnfocus();
}

protected boolean touchEvent(TouchEvent message) {
    int event = message.getEvent();
    switch (event) {

    case TouchEvent.CLICK:
    case TouchEvent.DOWN:
        if (touchEventOutOfBounds(message)) {
            return false;
        }

    case TouchEvent.MOVE:
        _pressed = true;
        setValueByTouchPosition(message.getX(1));
        fieldChangeNotify(0);
        return true;

    case TouchEvent.UNCLICK:
    case TouchEvent.UP:
        _pressed = false;
        invalidate();
        return true;

    default:
        return false;
    }
}

private boolean touchEventOutOfBounds(TouchEvent message) {
    int x = message.getX(1);
    int y = message.getY(1);
    return (x < 0 || y < 0 || x > getWidth() || y > getHeight());
}

private void setValueByTouchPosition(int x) {
    _currentValue = MathUtilities.clamp(0, (x - _leftCapWidth) * _numValues
            / _trackWidth, _numValues - 1);
    invalidate();
}

protected boolean navigationMovement(int dx, int dy, int status, int time) {
    if (_pressed) {
        if (dx > 0 || dy > 0) {
            incrementValue();
        } else {
            decrementValue();
        }
        fieldChangeNotify(0);
        return true;
    }
    return super.navigationMovement(dx, dy, status, time);
}

private void incrementValue() {
    if (_currentValue + 1 < _numValues) {
        _currentValue++;
        invalidate();
    }
}

private void decrementValue() {
    if (_currentValue > 0) {
        _currentValue--;
        invalidate();
    }
}

protected boolean invokeAction(int action) {
    if (action == ACTION_INVOKE) {
        togglePressed();
        return true;
    }
    return false;
}

protected boolean keyChar(char key, int status, int time) {
    if (key == Characters.SPACE || key == Characters.ENTER) {
        togglePressed();
        return true;
    }
    return false;
}

protected boolean trackwheelClick(int status, int time) {
    togglePressed();
    return true;
}

private void togglePressed() {
    _pressed = !_pressed;
    invalidate();
}

}

public class Main_NewsDetail extends MainScreen {
private Custom_FontField slider;
private boolean isSliderVisible = false;
private int dynamicfont = 10;

public Main_NewsDetail() {
    super(USE_ALL_WIDTH);
    add(new Custom_TopField(this, 2, 0));
    add(new Custom_NewsDetailBottom());
    add(new Custom_HeaderField(""));
    add(new Custom_NewsDetailField(  <-- the font size set inside this field
            "aaaaaaaaaaaaaa",
            "bbb",
            "ccc",
            "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd",
            "eeeeeeeeee", "fffff"));
    slider = new Custom_FontField(
            Bitmap.getBitmapResource("slider_thumb_normal.png"),
            Bitmap.getBitmapResource("slider_progress_normal.png"),
            Bitmap.getBitmapResource("slider_base_normal.png"),
            Bitmap.getBitmapResource("slider_thumb_focused.png"),
            Bitmap.getBitmapResource("slider_progress_focused.png"),
            Bitmap.getBitmapResource("slider_base_focused.png"),
            Bitmap.getBitmapResource("slider_thumb_pressed.png"),
            Bitmap.getBitmapResource("slider_progress_pressed.png"),
            Bitmap.getBitmapResource("slider_base_pressed.png"), 35, 10, 5,
            5, FOCUSABLE);
    dynamicfont = slider.getValue(); <-- get the value
    invalidate(); <-- refresh it but did not set the fontsize instantly.
}

public class Custom_NewsDetailField extends Manager {
    ...
    ...
    contentlabel = new Custom_LabelField(content,
                LabelField.USE_ALL_WIDTH | DrawStyle.LEFT | Field.FOCUSABLE);
        contentlabel.setFont(Font.getDefault().derive(Font.BOLD,
                dynamicfont));
        contentlabel.setFontColor(Color.BLACK);
        add(contentlabel);
}

我可以控制invalidate()的可见字段slider出现在显示中,但不能显示contentlabel.

4

1 回答 1

0
protected boolean navigationMovement(int dx, int dy, int status,
            int time) {
        if (_pressed) {
            if (dx > 0 || dy > 0) {
                incrementValue();
            } else {
                decrementValue();
            }
            fieldChangeNotify(0);
            contentlabel.setFont(Font.getDefault().derive(Font.BOLD,
                    getValue()));
            return true;
        }
        return super.navigationMovement(dx, dy, status, time);
    }

这会做。

于 2012-07-11T05:18:18.563 回答