Android Open Source - pixel-art Drawing Fragment






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;
//from   ww  w.j  a  v a 2  s.co m
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.jaween.pixelart.ContainerActivity;
import com.jaween.pixelart.ContainerFragment;
import com.jaween.pixelart.R;
import com.jaween.pixelart.tools.Tool;
import com.jaween.pixelart.ui.animation.Frame;
import com.jaween.pixelart.ui.undo.UndoManager;

import java.util.LinkedList;

public class DrawingFragment extends Fragment implements
        DrawingSurface.OnClearPanelsListener,
        DrawingSurface.OnSelectRegionListener,
        ActionMode.Callback {

    // Main SurfaceView on which to draw
    private DrawingSurface surface;

    // Callbacks
    private OnClearPanelsListener onClearPanelsListener = null;
    private ActionMode.Callback actionModeCallback = this;

    // Regular UI state
    private ActionMode actionMode = null;
    private Tool selectedTool;

    // Viewport save-state
    private static final String KEY_CENTER_X = "key_center_x";
    private static final String KEY_CENTER_Y = "key_center_y";
    private static final String KEY_SCALE = "key_scale";

    // UI save-state
    private static final String KEY_GRID = "key_grid";

    public DrawingFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        surface = new DrawingSurface(getActivity(), selectedTool);
        surface.setOnClearPanelsListener(this);
        surface.setOnSelectRegionListener(this);

        // Retrieves the undo manager for undoing and redoing drawing commands
        UndoManager undoManager = ((ContainerFragment) getParentFragment()).getUndoManager();
        surface.setUndoManager(undoManager);

        onRestoreInstanceState(savedInstanceState);

        return surface;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // If state is saved immediately after start up, the surface may not have been created yet
        if (surface.isSurfaceCreated()) {
            // Saves viewport
            RectF viewport = surface.getViewport();
            outState.putFloat(KEY_CENTER_X, viewport.left + viewport.width() / 2);
            outState.putFloat(KEY_CENTER_Y, viewport.top + viewport.height() / 2);
            outState.putFloat(KEY_SCALE, surface.getScale());

            // Saves the state of the grid
            outState.putBoolean(KEY_GRID, surface.isGridEnabled());
        }
    }

    public void onRestoreInstanceState(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            surface.setConfigurationChanged(true);

            // Restores viewport
            float DEFAULT_CENTER_X = 0;
            float DEFAULT_CENTER_Y = 0;
            float centerX = savedInstanceState.getFloat(KEY_CENTER_X, DEFAULT_CENTER_X);
            float centerY = savedInstanceState.getFloat(KEY_CENTER_Y, DEFAULT_CENTER_Y);
            float scale = savedInstanceState.getFloat(KEY_SCALE, DrawingSurface.DEFAULT_SCALE);
            surface.restoreViewport(centerX, centerY, scale);

            // Restores the grid
            boolean DEFAULT_GRID = false;
            boolean gridEnabled = savedInstanceState.getBoolean(KEY_GRID, DEFAULT_GRID);
            surface.setGridEnabled(gridEnabled);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        surface.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        surface.onPause();
    }

    public void setTool(Tool tool) {
        if (selectedTool != tool) {
            selectedTool = tool;
            if (surface != null) {
                surface.setTool(tool);
            }

            if (actionMode != null) {
                actionMode.finish();
            }
        }
    }

    public void setDimensions(int layerWidth, int layerHeight) {
        surface.setDimensions(layerWidth, layerHeight);
    }

    public void setFrames(LinkedList<Frame> frames) {
        surface.setFrames(frames);
    }

    public void setCurrentFrameIndex(int index) {
        surface.setCurrentFrameIndex(index);
    }

    public void setCurrentLayerIndex(int index) {
        surface.setCurrentLayerIndex(index);
    }

    public void setUndoManager(UndoManager undoManager) {
        surface.setUndoManager(undoManager);
    }

    public void undo(Object undoData) {
        surface.undo(undoData);
    }

    public void redo(Object redoData) {
        surface.redo(redoData);
    }

    public void invalidate() {
        surface.invalidate();
    }

    public boolean isGridEnabled() {
        return surface.isGridEnabled();
    }

    public void setGridEnabled(boolean enabled) {
        surface.setGridEnabled(enabled);
    }

    public void setOnClearPanelsListener(OnClearPanelsListener onClearPanelsListener) {
        this.onClearPanelsListener = onClearPanelsListener;
    }

    @Override
    public void onClearPanels() {
        if (onClearPanelsListener != null) {
            onClearPanelsListener.onClearPanels();
        }
    }

    @Override
    public void onSelectRegion(Path selectedRegion) {
        // Starts the Contextual Action Bar if there isn't one already there (i.e. actionMode == null)
        if (actionMode == null) {
            ((ContainerActivity) getActivity()).getToolbar().startActionMode(actionModeCallback);
        }
    }

    // Passed along to the DrawingSurface
    public void setOnDropColourListener(DrawingSurface.OnDropColourListener onDropColourListener) {
        surface.setOnDropColourListener(onDropColourListener);
    }

    @Override
    public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
        MenuInflater inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.selection_menu, menu);
        this.actionMode = actionMode;
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            /*case R.id.action_rotate:
                surface.rotateSelection();
                break;*/
            case R.id.action_select_all:
                surface.selectAll();
                break;
            /*case R.id.action_cut:
                surface.clearSelection();
                Toast.makeText(getActivity().getApplicationContext(), "TODO: Layer support", Toast.LENGTH_SHORT).show();
                surface.dismissSelection();
                actionMode.finish();
                break;*/
            /*case R.id.action_copy:
                surface.copySelection();
                break;*/
            case R.id.action_delete:
                surface.dismissSelection();
                actionMode.finish();
                break;
            default:
                return false;
        }
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode actionMode) {
        // Dismisses any selected regions
        surface.commitSelection();

        this.actionMode = null;
    }

    public interface OnClearPanelsListener {
        public boolean onClearPanels();
    }
}




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