android自定义游戏闯关图,GameStage

/**

* Let me draw all the items

*

* @param text item text

*/

private void drawItems(String text) {

drawRoundRect(canvas);

drawRoundRectText(canvas, text);

drawHorizontalDivider(canvas);

drawVerticalDivider(canvas);

drawHeads(canvas);

}

/**

* Draw the rounded rectangle item.

*

* @param canvas

*/

private void drawRoundRect(Canvas canvas) {

paint = new Paint();

paint.setColor(itemCountRecord <= nowPosition ? itemNormalBackgroundColor : itemLockBackgroundColor);

RectF rectF = new RectF();

rectF.left = paddingLeft + itemHorizontalMove;

rectF.right = rectF.left + itemWidth;

rectF.top = paddingTop + itemVerticalMove;

rectF.bottom = rectF.top + itemHeight;

canvas.drawRoundRect(rectF, 16, 16, paint);

//item坐标记录

itemCoordinates.put(itemCountRecord + "", rectF.left + "," + rectF.top + "&" + rectF.right + "," + rectF.bottom);

}

/**

* Draw the round corner font item.

*

* @param canvas

* @param text

*/

private void drawRoundRectText(Canvas canvas, String text) {

paint = new Paint();

paint.setColor(itemCountRecord <= nowPosition ? textNormalColor : textLockColor);

paint.setTextSize(textSize);

Rect rect = new Rect();

paint.getTextBounds(text, 0, text.length(), rect);

int textWidth = rect.width();

int textHeight = rect.height();

canvas.drawText(text, paddingLeft + itemHorizontalMove + itemWidth / 2 - textWidth / 2, paddingTop + itemVerticalMove + (itemHeight - textHeight / 2), paint);

}

/**

* Draw the horizontal divider

*

* @param canvas

*/

private void drawHorizontalDivider(Canvas canvas) {

//每行最后一个不再需要在右边的分割线

if (linePosition % lineColumns == 0 || (nowRow == totalRow && linePosition == modNum)) {

return;

}

paint = new Paint();

paint.setColor(itemCountRecord <= nowPosition ? itemNormalBackgroundColor : itemLockBackgroundColor);

Rect rect = new Rect();

rect.left = paddingLeft + itemWidth + itemHorizontalMove;

rect.right = rect.left + dividerHorizontalWidth;

rect.top = paddingTop + itemHeight / 2 - dividerHorizontalHeight / 2 + itemVerticalMove;

rect.bottom = rect.top + dividerHorizontalHeight;

canvas.drawRect(rect, paint);

}

/**

* Draw the vertical divider

*

* @param canvas

*/

private void drawVerticalDivider(Canvas canvas) {

//基数行最后一个Item显示,偶数行第一个Item显示,数据的最后一个不显示

if ((nowRow % 2 == 0 && linePosition == 1) || (nowRow % 2 != 0 && linePosition == lineColumns)) {

if (itemCountRecord == totalColumns) {

return;

}

paint = new Paint();

paint.setColor(itemCountRecord <= nowPosition ? itemNormalBackgroundColor : itemLockBackgroundColor);

Rect rect = new Rect();

rect.left = paddingLeft + itemWidth / 2 - dividerHorizontalHeight / 2 + itemHorizontalMove;

rect.right = rect.left + dividerVerticalWidth;

rect.top = paddingTop + itemHeight + itemVerticalMove;

rect.bottom = rect.top + dividerVerticalHeight;

canvas.drawRect(rect, paint);

}

}

/**

* Draw the round picture in the head view,

* the round border of the picture and the inverted triangle.

*

* @param canvas

*/

private void drawHeads(Canvas canvas) {

if (itemCountRecord != nowPosition) {

return;

}

int headXmove = paddingLeft + headSize / 2 + itemHorizontalMove;

int headYmove = paddingTop - headSize - headSize / 5 + itemVerticalMove;

//记录头像坐标范围

headCoordinates.put(itemCountRecord + "", (headXmove - headSize / 2) + "," + (headYmove - headSize / 2) + "&" + (headXmove + headSize) + "," + (headYmove + headSize));

paint = new Paint();

//圆形头像

Bitmap output = drawCircleHead();

//画生成的图片

canvas.drawBitmap(output, headXmove, headYmove, paint);

//三角形

drawTriangle(canvas, headXmove, headYmove);

//边框

drawCircleStroke(canvas, headXmove, headYmove);

}

/**

* Generate round images

*

* @return round image bitmap

*/

private Bitmap drawCircleHead() {

paint = new Paint();

Bitmap output = Bitmap.createBitmap(headSize, headSize, Bitmap.Config.ARGB_8888);

Canvas headCanvas = new Canvas(output);

paint.setAntiAlias(true);

headCanvas.drawARGB(0, 0, 0, 0);

paint.setColor(headCircleColor);

//圆圈

headCanvas.drawCircle(headSize / 2, headSize / 2, headSize / 2, paint);

//设置重叠模式

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

//矩形范围

Rect rect = new Rect(0, 0, headSize, headSize);

Bitmap bitmap = headBitmap != null ? headBitmap : BitmapFactory.decodeResource(getResources(), itemHeadSrc);

headCanvas.drawBitmap(bitmap, rect, rect, paint);

//对大图进行缩放

Matrix matrix = new Matrix();

float sx = headSize / bitmapWidthHeight(R.mipmap.ic_head_girl)[0];

float sy = headSize / bitmapWidthHeight(R.mipmap.ic_head_girl)[1];

matrix.setScale(sx, sy, 0, 0);

headCanvas.drawBitmap(bitmap, matrix, paint);

//重置画笔

paint.reset();

return output;

}

/**

* Draw the triangle

*

* @param canvas

* @param headXmove head of item x move

* @param headYmove item of item y move

*/

private void drawTriangle(Canvas canvas, int headXmove, int headYmove) {

Paint paint = new Paint();

paint.setColor(triangleColor);

paint.setAntiAlias(true);

Path path = new Path();

path.moveTo(headSize / 5 * 2 + headXmove, headSize + headArrowPaddingTop + headYmove);// 此点为多边形的起点

path.lineTo(headSize / 5 * 3 + headXmove, headSize + headArrowPaddingTop + headYmove);// 此点为多边形的起点

path.lineTo(headSize / 10 * 5 + headXmove, headSize + headSize / 5 + headArrowPaddingTop + headYmove);

path.close(); // 使这些点构成封闭的多边形

canvas.drawPath(path, paint);

}

/**

* Round head edge.

*

* @param canvas

* @param headXmove head of item x move

* @param headYmove item of item y move

*/

private void drawCircleStroke(Canvas canvas, int headXmove, int headYmove) {

Paint paint = new Paint();

paint.setAntiAlias(true);

paint.setStyle(Paint.Style.STROKE);

paint.setStrokeWidth(headStrokeWidth);

paint.setColor(headStrokeColor);

canvas.drawCircle(headSize / 2 + headXmove, headSize / 2 + headYmove, headSize / 2, paint);

paint.reset();

}

/**

* Get the picture width and height.

*

* @param resId

* @return

*/

private float[] bitmapWidthHeight(int resId) {

BitmapFactory.Options options = new BitmapFactory.Options();

options.inJustDecodeBounds = true;

BitmapFactory.decodeResource(getResources(), resId, options);

float width = options.outWidth;

float height = options.outHeight;

//设置为false,解析Bitmap对象加入到内存中

options.inJustDecodeBounds = false;

return new float[]{width, height};

}

/**

* The screen of width.

*

* @return

*/

public static int getScreenWidth() {

return Resources.getSystem().getDisplayMetrics().widthPixels;

}

/**

* The screen of height.

*

* @return

*/

public static int getScreenHeight() {

return Resources.getSystem().getDisplayMetrics().heightPixels;

}

public void setItemNormalBackgroundColor(int itemNormalBackgroundColor) {

this.itemNormalBackgroundColor = itemNormalBackgroundColor;

}

public void setItemLockBackgroundColor(int itemLockBackgroundColor) {

this.itemLockBackgroundColor = itemLockBackgroundColor;

}

public void setTriangleColor(int triangleColor) {

this.triangleColor = triangleColor;

}

public void setPaddingLeft(int paddingLeft) {

this.paddingLeft = paddingLeft;

}

public void setPaddingTop(int paddingTop) {

this.paddingTop = paddingTop;

}

public void setPaddingRight(int paddingRight) {

this.paddingRight = paddingRight;

}

public void setItemHeight(int itemHeight) {

this.itemHeight = itemHeight;

}

public void setItemWidth(int itemWidth) {

this.itemWidth = itemWidth;

}

public void setTextSize(int textSize) {

this.textSize = textSize;

}

public void setTextNormalColor(int textNormalColor) {

this.textNormalColor = textNormalColor;

}

public void setTextLockColor(int textLockColor) {

this.textLockColor = textLockColor;

}

public void setDividerHorizontalWidth(int dividerHorizontalWidth) {

this.dividerHorizontalWidth = dividerHorizontalWidth;

}

public void setDividerHorizontalHeight(int dividerHorizontalHeight) {

this.dividerHorizontalHeight = dividerHorizontalHeight;

}

public void setDividerVerticalWidth(int dividerVerticalWidth) {

this.dividerVerticalWidth = dividerVerticalWidth;

}

public void setDividerVerticalHeight(int dividerVerticalHeight) {

this.dividerVerticalHeight = dividerVerticalHeight;

}

public void setLineColumns(int lineColumns) {

this.lineColumns = lineColumns;

}

public void setTotalColumns(int totalColumns) {

this.totalColumns = totalColumns;

}

public void setHeadCircleColor(int headCircleColor) {

this.headCircleColor = headCircleColor;

}

public void setHeadSize(int headSize) {

this.headSize = headSize;

}

public void setHeadArrowPaddingTop(int headArrowPaddingTop) {

this.headArrowPaddingTop = headArrowPaddingTop;

}

public void setNowPosition(int nowPosition) {

this.nowPosition = nowPosition;

}

public void setHeadStrokeColor(int headStrokeColor) {

this.headStrokeColor = headStrokeColor;

}

public void setHeadStrokeWidth(int headStrokeWidth) {

this.headStrokeWidth = headStrokeWidth;

}

public void setItemHeadSrc(int itemHeadSrc) {

this.itemHeadSrc = itemHeadSrc;

}

public void setHeadBitmap(Bitmap headBitmap) {

this.headBitmap = headBitmap;

}

/**

* Px to dp

*

* @param px

* @return

*/

public static float pxToDp(float px) {

return px / getScreenDensity();

}

/**

* dp to px

*

* @param dp

* @return

*/

public static float dpToPx(float dp) {

return dp * getScreenDensity();

}

/**

* Get the screen of density

*

* @return

*/

public static float getScreenDensity() {

return Resources.getSystem().getDisplayMetrics().density;

}

public OnGameHeadClickListener onGameHeadClickListener;

/**

* Set the avatar click to monitor the event.

*

* @param onGameHeadClickListener

*/

public void setOnGameHeadClickListener(OnGameHeadClickListener onGameHeadClickListener) {

this.onGameHeadClickListener = onGameHeadClickListener;

}

/**

* Head click on the monitor event.

*/

public interface OnGameHeadClickListener {

void onGameHeadClick(int position, boolean isLock);

}

public OnGameItemClickListener onGameItemClickListener;

/**

* Set item to listen for events.

*

* @param onGameItemClickListener

*/

public void setOnGameItemClickListener(OnGameItemClickListener onGameItemClickListener) {

this.onGameItemClickListener = onGameItemClickListener;

}

/**

* Item click on the listener event.

*/

public interface OnGameItemClickListener {

void onGameItemClick(int position, boolean isLock);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值