Android Open Source - pixel-art Layer Adapter






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.layer;
//  ww  w .  j  av a2s .  c  om
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jaween.pixelart.R;

import java.util.LinkedList;

/**
 * Created by ween on 11/27/14.
 */
public class LayerAdapter extends BaseAdapter implements
        View.OnClickListener {

    private LinkedList<Layer> data;
    private LayoutInflater inflater;

    // Layer item drawables
    private Drawable layerVisible;
    private Drawable layerInvisible;
    private Drawable layerUnlocked;
    private Drawable layerLocked;

    // Transparency checkerboard
    private BitmapDrawable checkerboardTile;

    // Layer deletion callback
    private LayerListItemListener layerListItemListener = null;

    // List selection
    private int currentLayerIndex = 0;

    public LayerAdapter(Context context, LinkedList<Layer> data) {
        inflater = LayoutInflater.from(context);

        // Loads drawable resources here to avoid keeping the Context around (memory leak)
        layerVisible = context.getResources().getDrawable(R.drawable.ic_action_layer_visible);
        layerInvisible = context.getResources().getDrawable(R.drawable.tool_oval);
        layerUnlocked = context.getResources().getDrawable(R.drawable.tool_pen);
        layerLocked = context.getResources().getDrawable(R.drawable.tool_oval);

        // Transparency checkerboard background
        Bitmap checkerboardBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.checkerboard);
        checkerboardTile = new BitmapDrawable(context.getResources(), checkerboardBitmap);
        checkerboardTile.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);

        this.data = data;
    }

    public void setCurrentLayerIndex(int currentLayerIndex) {
        this.currentLayerIndex = currentLayerIndex;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        // Gets the recycled view (convertView) or creates a new one
        ViewHolder holder;
        if (convertView == null) {
            // Inflates convertView with item layout
            convertView = inflater.inflate(R.layout.layer_item, null);

            // Links to XML
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.tv_layer_title);
            holder.image = (ImageView) convertView.findViewById(R.id.iv_layer_image);
            holder.visibility = (ImageView) convertView.findViewById(R.id.iv_layer_visibility);
            holder.lock = (ImageView) convertView.findViewById(R.id.iv_layer_lock);
            holder.delete = (ImageView) convertView.findViewById(R.id.iv_layer_delete);

            holder.visibility.setOnClickListener(this);
            holder.lock.setOnClickListener(this);
            holder.delete.setOnClickListener(this);

            // Saves the holder and it's views for reuse
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (position == currentLayerIndex) {
            convertView.setBackgroundResource(R.color.highlight);
        } else {
            convertView.setBackgroundResource(0);
        }
        initialiseViews(holder, position);

        return convertView;
    }

    private void initialiseViews(ViewHolder holder, int position) {
        holder.title.setText(data.get(position).getTitile());
        holder.image.setImageBitmap(data.get(position).getImage());

        // Pre-Jellybean doesn't have setBackground()
        int sdk = Build.VERSION.SDK_INT;
        if (sdk < Build.VERSION_CODES.JELLY_BEAN) {
            holder.image.setBackgroundDrawable(checkerboardTile);
        } else {
            holder.image.setBackground(checkerboardTile);
        }

        if (data.get(position).isVisible()) {
            holder.visibility.setImageDrawable(layerVisible);
        } else {
            holder.visibility.setImageDrawable(layerInvisible);
        }

        if (data.get(position).isLocked()) {
            holder.lock.setImageDrawable(layerLocked);
        } else {
            holder.lock.setImageDrawable(layerUnlocked);
        }

        // Disables layer deletion when there's only a single layer
        if (data.size() == 1) {
            holder.delete.setEnabled(false);
        } else {
            holder.delete.setEnabled(true);
        }

        // Stores this view's position in the list within the View tag (for use with onClick())
        holder.title.setTag(position);
        holder.visibility.setTag(position);
        holder.lock.setTag(position);
        holder.delete.setTag(position);
    }

    @Override
    public void onClick(View view) {
        // Determines which row this view belongs to
        int position;
        if (view.getTag() != null) {
            position = (Integer) view.getTag();
        } else {
            return;
        }

        // TODO: Modifications to layer properties (such as visibility) aren't restored on undo/redo
        switch (view.getId()) {
            case R.id.iv_layer_visibility:
                data.get(position).setVisible(!data.get(position).isVisible());
                break;
            case R.id.iv_layer_lock:
                data.get(position).setLocked(!data.get(position).isLocked());
                break;
            case R.id.iv_layer_delete:
                if (data.size() > 1) {
                    // Deletion occurs in the Fragment to avoid accidentally deleting two items,
                    // once from this adapter and once from LayerFragment.deleteLayer()
                    if (layerListItemListener != null) {
                        layerListItemListener.onDeleteLayerFromList(position);
                    }
                }
                break;
        }

        notifyDataSetChanged();
    }

    static class ViewHolder {
        TextView title;
        ImageView image;
        ImageView visibility;
        ImageView lock;
        ImageView delete;
    }

    public void setLayerListItemListener(LayerListItemListener layerListItemListener) {
        this.layerListItemListener = layerListItemListener;
    }

    public interface LayerListItemListener {
        public void onDeleteLayerFromList(int i);
    }
}




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