Add image to HoneycombView

时间:2015-11-12 11:29:51

标签: android

I have this code wich allow me to change the color of the every cell when I click on it.Firstly, I want to be able to put an image in every cell , which I clicked . And after that I will try to make a listview with images and when I will click on some ceel to be able to choose what image I want to put in there ! Thank you!

package com.example.cosmin.catanadvice;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.squareup.picasso.Picasso;

  public class HoneycombView extends View 
{
private Paint wallPaint = new Paint();
private Paint fillPaint = new Paint();
private Paint cachePaint = new Paint();
private Path combPath;
private Bitmap cacheBmp;
private Canvas cacheCan;

private int cellWidth;
private int columns;
private int rows;
private boolean[][] cellSet;

private OnCellClickListener listener;

 public HoneycombView(Context context)
{
    this(context, null);
}

public HoneycombView(Context context, AttributeSet attrs)
{
    super(context, attrs);

    wallPaint.setColor(Color.YELLOW);
    wallPaint.setStyle(Paint.Style.STROKE);
    wallPaint.setStrokeWidth(5f);

    fillPaint.setStyle(Paint.Style.FILL);
    cachePaint.setStyle(Paint.Style.FILL);
}

public void initialize(int columns, int rows)
{
    this.columns = columns;
    this.rows = rows;

    this.cellSet = new boolean[columns][rows];
}

public interface OnCellClickListener
{
    public void onCellClick(int column, int row);
}

public void setOnCellClickListener(OnCellClickListener listener)
{
    this.listener = listener;
}

public void setCell(int column, int row, boolean isSet)
{
    cellSet[column][row] = isSet;
    invalidate();
}

public boolean isCellSet(int column, int row)
{
    return cellSet[column][row];
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
    super.onSizeChanged(w, h, oldw, oldh);

    cacheBmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    cacheCan = new Canvas(cacheBmp);

    cellWidth = 2 * w / (2 * columns + columns - 1);
}

@Override
protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);

    canvas.drawColor(Color.GREEN);

    boolean oddRow;
    int xOff;

    combPath = getHexPath(cellWidth / 2f, cellWidth / 2f, (float) (cellWidth * Math.sqrt(3) / 4));
int verific =0;
    int verific2=0;
    for (int r = 0; r < rows; r++)
    {
        oddRow = (r & 1) == 1;
        xOff = 0;


        for (int c = 0; c < columns; c++)
        {
            if (!(oddRow && c == columns -1))
            {
                verific++;
                if(verific!=1&&verific!=3&&verific!=21&&verific!=23) {

                    fillPaint.setColor(cellSet[c][r] ? Color.BLUE : Color.WHITE);
                    canvas.drawPath(combPath, fillPaint);
wallPaint.setColor(Color.YELLOW);
                    canvas.drawPath(combPath, wallPaint);
                //    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.airplane);


                    cachePaint.setColor(Color.argb(255, 1, c, r));
                    cacheCan.drawPath(combPath, cachePaint);

                    combPath.offset((int) (1.5f * cellWidth), 0);
                    xOff += 1.5f * cellWidth;
                }else{
                    fillPaint.setColor(cellSet[c][r] ? Color.GREEN : Color.GREEN);
                    canvas.drawPath(combPath, fillPaint);
                    wallPaint.setColor(Color.GREEN);

                    canvas.drawPath(combPath, wallPaint);

                    cachePaint.setColor(Color.argb(100, 1, c, r));
                    cacheCan.drawPath(combPath, cachePaint);

                    combPath.offset((int) (1.5f * cellWidth), 0);
                    xOff += 1.5f * cellWidth;
                }
            }
        }

        combPath.offset(-xOff + (oddRow ? -1 : 1) * 3 * cellWidth / 4, (float) (cellWidth * Math.sqrt(3) / 4));
    }
}

@Override
public boolean onTouchEvent(MotionEvent event)
{
    if (event.getAction() != MotionEvent.ACTION_DOWN)
        return true;

    int pixel = cacheBmp.getPixel((int) event.getX(), (int) event.getY());

    int r = Color.red(pixel);
    if (r == 1)
    {
        int g = Color.green(pixel);
        int b = Color.blue(pixel);

        if (listener != null)
        {
            listener.onCellClick(g, b);
        }
    }
    return true;
}

private Path getHexPath(float size, float centerX, float centerY)
{
    Path path = new Path();

    for (int j = 0; j <= 6; j++)
    {
        double angle = j * Math.PI / 3;
        float x = (float) (centerX + size * Math.cos(angle));
        float y = (float) (centerY + size * Math.sin(angle));
        if (j == 0)
        {
            path.moveTo(x, y);
        }
        else
        {
            path.lineTo(x, y);
        }
    }

    return path;
}

}

And this is the mainActivity:

public class Activitate extends Activity
    implements HoneycombView.OnCellClickListener {
HoneycombView honey;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    honey = new HoneycombView(this);
    honey.initialize(3, 9);
    honey.setOnCellClickListener(this);

    setContentView(honey);
}

@Override
public void onCellClick(int column, int row) {
    honey.setCell(column, row, !honey.isCellSet(column, row));
}

}

0 个答案:

没有答案