Android Open Source - pixel-art Rect






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.tools;
/*w  w  w  .ja va2s .  c om*/
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

import com.jaween.pixelart.tools.attributes.RectToolAttributes;

/**
 * Created by ween on 10/19/14.
 */
public class Rect extends Tool {

    private static final int TOOL_ID = 2;
    public Rect(String name, Drawable icon) {
        super(name, icon, TOOL_ID);
        toolAttributes = new RectToolAttributes();
    }

    private PointF start = new PointF();
    private RectF rectBounds = new RectF();

    @Override
    protected void onStart(Bitmap bitmap, PointF event) {
        start.x = event.x;
        start.y = event.y;

        draw(canvas, bitmap, event);
    }

    @Override
    protected void onMove(Bitmap bitmap, PointF event) {
        // Locks the oval to a circle
        if (((RectToolAttributes) toolAttributes).isSquareLocked()) {
            lockSquare(event);
        }

        draw(canvas, bitmap, event);
    }


    @Override
    protected void onEnd(Bitmap bitmap, PointF event) {
        // Locks the oval to a circle
        if (((RectToolAttributes) toolAttributes).isSquareLocked()) {
            lockSquare(event);
        }

        draw(canvas, bitmap, event);
    }

    // TODO: Allow the coordinates of rect to go less than 0 in both x and y
    private void draw(Canvas canvas, Bitmap bitmap, PointF end) {
        rectBounds.set(start.x, start.y, end.x, end.y);

        // Flips the bounds of the rect if they are inverted
        roundRectEdgeWorkAround(rectBounds);

        float radius;
        if (((RectToolAttributes) toolAttributes).isRoundedRect()) {
            radius = ((RectToolAttributes) toolAttributes).getRoundnessLevel();
        } else {
            // Regular rectangles can't be drawn outside the top and left boundaries, but rounded
            // rectangles can. We can draw a regular rect by using a rounded rect with corners of radius 0
            radius = 0;
        }

        canvas.setBitmap(bitmap);
        canvas.drawRoundRect(rectBounds, radius, radius, toolAttributes.getPaint());
    }

    // Locks the rect to a square (modifies the input point)
    private void lockSquare(PointF end) {
        float dX = end.x - start.x;
        float dY = end.y - start.y;

        float rectWidth = Math.abs(dX);
        float rectHeight = Math.abs(dY);

        float diameter = Math.max(rectWidth, rectHeight);

        // The diameter of the square is the larger of the rect's width or height
        if (dX > 0 && dY > 0) {
            // Lower right quadrant
            end.y = start.y + diameter;
            end.x = start.x + diameter;
        } else if (dX > 0 && dY < 0) {
            // Upper right quadrant
            end.y = start.y - diameter;
            end.x = start.x + diameter;
        } else if (dX < 0 && dY < 0) {
            // Upper left quadrant
            end.y = start.y - diameter;
            end.x = start.x - diameter;
        } else if (dX < 0 && dY > 0) {
            // Lower left quadrant
            end.y = start.y + diameter;
            end.x = start.x - diameter;
        }
    }

    // Canvas can't draw rounded rectangles with edges inverted, flips the edges to make it work
    private void roundRectEdgeWorkAround(RectF bounds) {
        if (bounds.right < bounds.left) {
            bounds.set(bounds.right, bounds.top, bounds.left, bounds.bottom);
        }
        if (bounds.bottom < bounds.top) {
            bounds.set(bounds.left, bounds.bottom, bounds.right, bounds.top);
        }
    }
}




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