Android Open Source - pixel-art Thumbnail






From Project

Back to project page pixel-art.

License

The source code is released under:

Apache License

If you think the Android project pixel-art listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.jaween.pixelart.ui;
/*  w ww  . j a v a2 s . c o  m*/
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

public class Thumbnail {

    // Thumbnail View
    private boolean enabled = true;

    // Thumbnail Metrics
    private RectF thumbnailRectF = new RectF();

    // View window
    private boolean viewWindowEnabled = true;
    private RectF viewWindowRect = new RectF();

    // Checkerboard
    private TransparencyCheckerboard transparencyCheckerboard;

    // Paints
    private Paint viewWindowPaint;

    // Colour preferences
    private static final int VIEW_WINDOW_COLOUR = Color.LTGRAY;
    private static final int VIEW_WINDOW_OPACITY = 170;
    
    public Thumbnail(Context context, RectF thumbnailRectF) {
        transparencyCheckerboard = new TransparencyCheckerboard(context);
        initialisePaints();
        this.thumbnailRectF = thumbnailRectF;
    }

    private void initialisePaints() {
        // Thumbnail window
        viewWindowPaint = new Paint();
        viewWindowPaint.setStyle(Paint.Style.STROKE);
        viewWindowPaint.setColor(VIEW_WINDOW_COLOUR);
        viewWindowPaint.setAlpha(VIEW_WINDOW_OPACITY);
        viewWindowPaint.setStyle(Paint.Style.FILL);
        viewWindowPaint.setAntiAlias(false);
    }

    /**
     * Draws thumbnail given a canvas to draw on to, the unscaled bitmap and the unscaled viewport
     * @param canvas The canvas to draw into
     * @param bitmap The bitmap to be drawn as a thumbnail
     * @param viewport The viewing area of the bitmap
     * @param shadowPaint The paint to draw the shadow around the thumbnail
     */
    public void draw(Canvas canvas, Bitmap bitmap, RectF viewport, Paint shadowPaint) {
        // Border shadow
        canvas.drawRect(thumbnailRectF, shadowPaint);

        // Transparency checkerboard
        transparencyCheckerboard.draw(canvas, thumbnailRectF);

        // Thumbnail
        canvas.drawBitmap(bitmap, null, thumbnailRectF, null);

        // View Window (portion of the image being viewed)
        if (viewWindowEnabled) {
            calculateViewWindow(viewport, bitmap.getWidth(), bitmap.getHeight());
            drawViewWindow(canvas);
        }
    }

    // Uses four semi-transparent rectangles around the view window to indicate viewing region,
    // visually similar to letterboxing and pillarboxing
    private void drawViewWindow(Canvas canvas) {
        // Top semi-transparent overlay
        canvas.drawRect(
                thumbnailRectF.left,
                thumbnailRectF.top,
                thumbnailRectF.right,
                viewWindowRect.top,
                viewWindowPaint);

        // Bottom semi-transparent overlay
        canvas.drawRect(
                thumbnailRectF.left,
                viewWindowRect.bottom,
                thumbnailRectF.right,
                thumbnailRectF.bottom,
                viewWindowPaint);

        // Left semi-transparent overlay
        canvas.drawRect(
                thumbnailRectF.left,
                viewWindowRect.top,
                viewWindowRect.left,
                viewWindowRect.bottom,
                viewWindowPaint);

        // Right semi-transparent overlay
        canvas.drawRect(
                viewWindowRect.right,
                viewWindowRect.top,
                thumbnailRectF.right,
                viewWindowRect.bottom,
                viewWindowPaint);
    }

    // Determines the edges of the scaled viewport, the viewWindowRect
    private void calculateViewWindow(RectF viewport, int bitmapWidth, int bitmapHeight) {
        // Left
        if (viewport.left < 0) {
            viewWindowRect.left = thumbnailRectF.left;
        } else if (viewport.left > bitmapWidth) {
            viewWindowRect.left = thumbnailRectF.right;
        } else {
            // In
            viewWindowRect.left = thumbnailRectF.left + (viewport.left / bitmapWidth)
                    * thumbnailRectF.width();
        }

        // Top
        if (viewport.top < 0) {
            viewWindowRect.top = thumbnailRectF.top;
        } else if (viewport.top > bitmapHeight) {
            viewWindowRect.top = thumbnailRectF.bottom;
        } else {
            viewWindowRect.top = thumbnailRectF.top + (viewport.top / bitmapHeight)
                    * thumbnailRectF.height();
        }

        // Right
        if (viewport.right > bitmapWidth) {
            viewWindowRect.right = thumbnailRectF.right;
        } else if (viewport.right < 0) {
            viewWindowRect.right = thumbnailRectF.left;
        } else {
            viewWindowRect.right = thumbnailRectF.left + (viewport.right / bitmapWidth)
                    * thumbnailRectF.width();
        }

        // Bottom
        if (viewport.bottom > bitmapHeight) {
            viewWindowRect.bottom = thumbnailRectF.bottom;
        } else if (viewport.bottom < 0) {
            viewWindowRect.bottom = thumbnailRectF.top;
        } else {
            viewWindowRect.bottom = thumbnailRectF.top + (viewport.bottom / bitmapHeight)
                    * thumbnailRectF.height();
        }
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setViewWindowEnable(boolean enabled) {
        viewWindowEnabled = enabled;
    }

    public boolean isViewWindowEnabled() {
        return viewWindowEnabled;
    }
}




Java Source Code List

com.jaween.pixelart.ContainerActivity.java
com.jaween.pixelart.ContainerFragment.java
com.jaween.pixelart.PanelManagerFragment.java
com.jaween.pixelart.io.AnimationFile.java
com.jaween.pixelart.io.FileAdapter.java
com.jaween.pixelart.io.ImportExport.java
com.jaween.pixelart.io.LoadFileDialog.java
com.jaween.pixelart.tools.Command.java
com.jaween.pixelart.tools.Dropper.java
com.jaween.pixelart.tools.Eraser.java
com.jaween.pixelart.tools.FloodFill.java
com.jaween.pixelart.tools.FreeSelect.java
com.jaween.pixelart.tools.MagicWand.java
com.jaween.pixelart.tools.Oval.java
com.jaween.pixelart.tools.Pen.java
com.jaween.pixelart.tools.RectSelect.java
com.jaween.pixelart.tools.Rect.java
com.jaween.pixelart.tools.Selection.java
com.jaween.pixelart.tools.ToolReport.java
com.jaween.pixelart.tools.Tool.java
com.jaween.pixelart.tools.attributes.EraserToolAttributes.java
com.jaween.pixelart.tools.attributes.MagicWandToolAttributes.java
com.jaween.pixelart.tools.attributes.OvalToolAttributes.java
com.jaween.pixelart.tools.attributes.PenToolAttributes.java
com.jaween.pixelart.tools.attributes.RectToolAttributes.java
com.jaween.pixelart.tools.attributes.ToolAttributes.java
com.jaween.pixelart.tools.options.EraserOptionsView.java
com.jaween.pixelart.tools.options.MagicWandOptionsView.java
com.jaween.pixelart.tools.options.OvalOptionsView.java
com.jaween.pixelart.tools.options.PenOptionsView.java
com.jaween.pixelart.tools.options.RectOptionsView.java
com.jaween.pixelart.tools.options.ToolOptionsView.java
com.jaween.pixelart.ui.ColourButton.java
com.jaween.pixelart.ui.ColourSelector.java
com.jaween.pixelart.ui.DrawingFragment.java
com.jaween.pixelart.ui.DrawingSurface.java
com.jaween.pixelart.ui.PaletteFragment.java
com.jaween.pixelart.ui.PanelFragment.java
com.jaween.pixelart.ui.PixelGrid.java
com.jaween.pixelart.ui.Thumbnail.java
com.jaween.pixelart.ui.ToolButton.java
com.jaween.pixelart.ui.ToolboxFragment.java
com.jaween.pixelart.ui.TransparencyCheckerboard.java
com.jaween.pixelart.ui.animation.AnimationFragment.java
com.jaween.pixelart.ui.animation.FrameAdapter.java
com.jaween.pixelart.ui.animation.Frame.java
com.jaween.pixelart.ui.colourpicker.ColourPickerDialog.java
com.jaween.pixelart.ui.colourpicker.ColourPickerFragment.java
com.jaween.pixelart.ui.colourpicker.ColourPickerView.java
com.jaween.pixelart.ui.layer.LayerAdapter.java
com.jaween.pixelart.ui.layer.LayerFragment.java
com.jaween.pixelart.ui.layer.Layer.java
com.jaween.pixelart.ui.undo.DrawOpManager.java
com.jaween.pixelart.ui.undo.DrawOpUndoData.java
com.jaween.pixelart.ui.undo.FrameUndoData.java
com.jaween.pixelart.ui.undo.LayerUndoData.java
com.jaween.pixelart.ui.undo.UndoItem.java
com.jaween.pixelart.ui.undo.UndoManager.java
com.jaween.pixelart.util.AbsVerticalSeekBar.java
com.jaween.pixelart.util.AnimatedGifEncoder.java
com.jaween.pixelart.util.AutoSaver.java
com.jaween.pixelart.util.BitmapEncoder.java
com.jaween.pixelart.util.Color.java
com.jaween.pixelart.util.ConfigChangeFragment.java
com.jaween.pixelart.util.Debug.java
com.jaween.pixelart.util.MarchingAnts.java
com.jaween.pixelart.util.PreferenceManager.java
com.jaween.pixelart.util.ScaleListener.java
com.jaween.pixelart.util.SlideAnimator.java
com.jaween.pixelart.util.SlidingLinearLayout.java
com.jaween.pixelart.util.VerticalProgressBar.java
com.tokaracamara.android.verticalslidevar.VerticalSeekBar.java