Back to project page pixel-art.
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.
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(); } }