Search in sources :

Example 16 with Orientation

use of com.codename1.charts.renderers.XYMultipleSeriesRenderer.Orientation in project CodenameOne by codenameone.

the class List method scrollRectToVisible.

/**
 * Makes sure the selected index is visible if it is not in the current view
 * rect the list will scroll so it fits within
 *
 * @param rect the rectangle area to scroll to
 */
public void scrollRectToVisible(Rectangle rect) {
    if (fixedSelection < FIXED_NONE_BOUNDRY) {
        // Dimension elemSize = getElementSize();
        Rectangle toScroll;
        if (orientation != HORIZONTAL) {
            toScroll = new Rectangle(getScrollX(), rect.getY(), rect.getSize().getWidth(), rect.getSize().getHeight() + itemGap);
        } else {
            toScroll = new Rectangle(rect.getX(), getScrollY(), rect.getSize().getWidth() + itemGap, rect.getSize().getHeight());
        }
        super.scrollRectToVisible(toScroll, this);
    }
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle)

Example 17 with Orientation

use of com.codename1.charts.renderers.XYMultipleSeriesRenderer.Orientation in project CodenameOne by codenameone.

the class List method paint.

/**
 * {@inheritDoc}
 */
public void paint(Graphics g) {
    getUIManager().getLookAndFeel().drawList(g, this);
    Style style = getStyle();
    int width = getWidth() - style.getHorizontalPadding() - getSideGap();
    if (isScrollableX()) {
        width = Math.max(width, getScrollDimension().getWidth() - style.getHorizontalPadding() - getSideGap());
    }
    int numOfcomponents = model.getSize();
    if (numOfcomponents == 0) {
        paintHint(g);
        return;
    }
    int xTranslate = getX();
    int yTranslate = getY();
    g.translate(xTranslate, yTranslate);
    Rectangle pos = new Rectangle();
    Dimension rendererSize = getElementSize(false, true);
    if (fixedSelection > FIXED_NONE_BOUNDRY) {
        if (animationPosition != 0 || isDragActivated()) {
            if (orientation != HORIZONTAL) {
                yTranslate += (animationPosition + fixedDraggedAnimationPosition);
                g.translate(0, animationPosition + fixedDraggedAnimationPosition);
            } else {
                xTranslate += (animationPosition + fixedDraggedAnimationPosition);
                g.translate(animationPosition + fixedDraggedAnimationPosition, 0);
            }
        }
    }
    int clipX = g.getClipX();
    int clipY = g.getClipY();
    int clipWidth = g.getClipWidth();
    int clipHeight = g.getClipHeight();
    // this flag is for preformance improvements
    // if we figured out that the list items are not visible anymore
    // we should break from the List loop
    boolean shouldBreak = false;
    // improve performance for browsing the end of a very large list
    int startingPoint = 0;
    if (fixedSelection < FIXED_NONE_BOUNDRY) {
        int startX = clipX + getAbsoluteX();
        if (isRTL()) {
            // In RTL the start of the list is not in the left side of the viewport, but rather the right side
            startX += getWidth();
        }
        startingPoint = Math.max(0, pointerSelect(startX, clipY + getAbsoluteY()) - 1);
    }
    int startOffset = 0;
    int endOffset = numOfcomponents;
    if (mutableRendererBackgrounds) {
        for (int i = startingPoint; i < numOfcomponents; i++) {
            // skip on the selected
            if (i == getCurrentSelected() && animationPosition == 0 && fixedDraggedAnimationPosition == 0) {
                if (!shouldBreak) {
                    startOffset = i;
                }
                endOffset = i;
                shouldBreak = true;
                continue;
            }
            calculateComponentPosition(i, width, pos, rendererSize, getElementSize(true, true), i <= getCurrentSelected());
            // if the renderer is in the clipping region
            if (pos.intersects(clipX, clipY, clipWidth, clipHeight)) {
                if (!shouldBreak) {
                    startOffset = i;
                }
                endOffset = i;
                Dimension size = pos.getSize();
                Component selectionCmp = renderer.getListCellRendererComponent(this, getModel().getItemAt(i), i, i == getCurrentSelected());
                renderComponentBackground(g, selectionCmp, pos.getX(), pos.getY(), size.getWidth(), size.getHeight());
                shouldBreak = true;
            } else {
                // this is relevant only if the List is not fixed.
                if (shouldBreak && (fixedSelection < FIXED_NONE_BOUNDRY)) {
                    break;
                }
            }
        }
    } else {
        T valueAt0 = getModel().getItemAt(0);
        Component selectionCmp;
        int selectedIndex = getSelectedIndex();
        if (selectedIndex > -1 && selectedIndex < numOfcomponents) {
            // this is essential otherwise we constantly ticker based on the value of the first entry
            selectionCmp = renderer.getListCellRendererComponent(this, getModel().getItemAt(selectedIndex), 0, true);
        } else {
            selectionCmp = renderer.getListCellRendererComponent(this, valueAt0, 0, true);
        }
        Component unselectedCmp = renderer.getListCellRendererComponent(this, valueAt0, 0, false);
        for (int i = startingPoint; i < numOfcomponents; i++) {
            // skip on the selected
            if (i == getCurrentSelected() && animationPosition == 0) {
                if (!shouldBreak) {
                    startOffset = i;
                }
                endOffset = i;
                shouldBreak = true;
                continue;
            }
            calculateComponentPosition(i, width, pos, rendererSize, getElementSize(true, true), i <= getCurrentSelected());
            // if the renderer is in the clipping region
            if (pos.intersects(clipX, clipY, clipWidth, clipHeight)) {
                if (!shouldBreak) {
                    startOffset = i;
                }
                endOffset = i;
                if (i == getCurrentSelected()) {
                    Dimension size = pos.getSize();
                    renderComponentBackground(g, selectionCmp, pos.getX(), pos.getY(), size.getWidth(), size.getHeight());
                } else {
                    Dimension size = pos.getSize();
                    renderComponentBackground(g, unselectedCmp, pos.getX(), pos.getY(), size.getWidth(), size.getHeight());
                }
                shouldBreak = true;
            } else {
                // this is relevant only if the List is not fixed.
                if (shouldBreak && (fixedSelection < FIXED_NONE_BOUNDRY)) {
                    break;
                }
            }
        }
    }
    boolean shouldRendererSelectedEntry = (renderer.getListFocusComponent(this) == null && (fixedSelection < FIXED_NONE_BOUNDRY)) || animationPosition == 0 && model.getSize() > 0;
    Rectangle selectedPos = new Rectangle();
    calculateComponentPosition(getCurrentSelected(), width, selectedPos, rendererSize, getElementSize(true, true), true);
    Dimension size = selectedPos.getSize();
    int curSel = getCurrentSelected();
    if (shouldRendererSelectedEntry && curSel > -1 && curSel < model.getSize()) {
        Component selected = renderer.getListCellRendererComponent(this, model.getItemAt(getCurrentSelected()), getCurrentSelected(), true);
        renderComponentBackground(g, selected, selectedPos.getX(), selectedPos.getY(), size.getWidth(), size.getHeight());
    }
    if (paintFocusBehindList) {
        paintFocus(g, width, pos, rendererSize);
    }
    for (int i = startOffset; i <= endOffset; i++) {
        // skip on the selected
        if (i == getCurrentSelected() && animationPosition == 0) {
            continue;
        }
        calculateComponentPosition(i, width, pos, rendererSize, getElementSize(true, true), i <= getCurrentSelected());
        if (pos.intersects(clipX, clipY, clipWidth, clipHeight)) {
            T value = model.getItemAt(i);
            Component cmp = renderer.getListCellRendererComponent(this, value, i, false);
            cmp.setCellRenderer(true);
            Dimension sizeC = pos.getSize();
            renderComponent(g, cmp, pos.getX(), pos.getY(), sizeC.getWidth(), sizeC.getHeight());
        }
    }
    // if the animation has finished draw the selected element
    if (shouldRendererSelectedEntry) {
        Component selected = renderer.getListCellRendererComponent(this, model.getItemAt(getCurrentSelected()), getCurrentSelected(), true);
        renderComponent(g, selected, selectedPos.getX(), selectedPos.getY(), size.getWidth(), size.getHeight());
    }
    if (!paintFocusBehindList) {
        paintFocus(g, width, pos, rendererSize);
    }
    g.translate(-xTranslate, -yTranslate);
    if (spinnerOverlay != null) {
        if (spinnerOverlay.getBorder() != null) {
            spinnerOverlay.getBorder().paintBorderBackground(g, this);
            spinnerOverlay.getBorder().paint(g, this);
        } else {
            spinnerOverlay.getBgPainter().paint(g, getBounds());
        }
    }
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle) Style(com.codename1.ui.plaf.Style) Dimension(com.codename1.ui.geom.Dimension)

Example 18 with Orientation

use of com.codename1.charts.renderers.XYMultipleSeriesRenderer.Orientation in project CodenameOne by codenameone.

the class AndroidImplementation method createImage.

@Override
public Object createImage(String path) throws IOException {
    int IMAGE_MAX_SIZE = getDisplayHeight();
    if (exists(path)) {
        Bitmap b = null;
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            o.inPreferredConfig = Bitmap.Config.ARGB_8888;
            InputStream fis = createFileInputStream(path);
            BitmapFactory.decodeStream(fis, null, o);
            fis.close();
            int scale = 1;
            if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
                scale = (int) Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
            }
            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inPreferredConfig = Bitmap.Config.ARGB_8888;
            if (sampleSizeOverride != -1) {
                o2.inSampleSize = sampleSizeOverride;
            } else {
                String sampleSize = Display.getInstance().getProperty("android.sampleSize", null);
                if (sampleSize != null) {
                    o2.inSampleSize = Integer.parseInt(sampleSize);
                } else {
                    o2.inSampleSize = scale;
                }
            }
            o2.inPurgeable = true;
            o2.inInputShareable = true;
            fis = createFileInputStream(path);
            b = BitmapFactory.decodeStream(fis, null, o2);
            fis.close();
            // fix rotation
            ExifInterface exif = new ExifInterface(path);
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            int angle = 0;
            switch(orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    angle = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    angle = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    angle = 270;
                    break;
            }
            if (sampleSizeOverride < 0 && angle != 0) {
                Matrix mat = new Matrix();
                mat.postRotate(angle);
                Bitmap correctBmp = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), mat, true);
                b.recycle();
                b = correctBmp;
            }
        } catch (IOException e) {
        }
        return b;
    } else {
        InputStream in = this.getResourceAsStream(getClass(), path);
        if (in == null) {
            throw new IOException("Resource not found. " + path);
        }
        try {
            return this.createImage(in);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception ignored) {
                    ;
                }
            }
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) BufferedInputStream(com.codename1.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ExifInterface(android.media.ExifInterface) BitmapFactory(android.graphics.BitmapFactory) IOException(java.io.IOException) Paint(android.graphics.Paint) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ParseException(java.text.ParseException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException)

Aggregations

Dimension (com.codename1.ui.geom.Dimension)8 Style (com.codename1.ui.plaf.Style)7 Rectangle (com.codename1.ui.geom.Rectangle)5 Paint (com.codename1.charts.compat.Paint)3 Point (com.codename1.charts.models.Point)3 Component (com.codename1.ui.Component)3 Form (com.codename1.ui.Form)3 Orientation (com.codename1.charts.renderers.XYMultipleSeriesRenderer.Orientation)2 Container (com.codename1.ui.Container)2 Border (com.codename1.ui.plaf.Border)2 UIManager (com.codename1.ui.plaf.UIManager)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)1 Bitmap (android.graphics.Bitmap)1 BitmapFactory (android.graphics.BitmapFactory)1 Matrix (android.graphics.Matrix)1 Paint (android.graphics.Paint)1 ExifInterface (android.media.ExifInterface)1 Orientation (com.codename1.charts.compat.GradientDrawable.Orientation)1