亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

點擊不出棋子

package?com.imooc.wuziqi;

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

import?java.util.ArrayList;
import?java.util.List;

public?class?WuziqiPanel?extends?View?{
????private?int?mPanelWidth;
????private?float?mLineHeight;
????private?int?MAX_Line?=?10;

????private?Paint?mPaint?=?new?Paint();//設置所畫內容的顏色等;

????//棋子圖片變量
????private?Bitmap?mWhitePiece;
????private?Bitmap?mBlackPiece;
????//設置棋子格式變量
????private?float?ratioPieceOFLineHeight?=?3?*?1.0f?/?4;

????//白棋先手,當前輪到白棋
????private?boolean?mIsWhite?=?true;
????private?List<Point>?mWhiteArray?=?new?ArrayList<>();
????private?List<Point>?mBlackArray?=?new?ArrayList<>();

????public?WuziqiPanel(Context?context,?AttributeSet?attrs)?{
????????super(context,attrs);
????????setBackgroundColor(0x50C0C0C0);
????????init();
????}

????private?void?init()?{
????????//設置畫的圖形樣子,顏色
????????mPaint.setColor(0x88000000);
????????mPaint.setAntiAlias(true);//
????????mPaint.setDither(true);
????????mPaint.setStyle(Paint.Style.STROKE);

????????//對棋子初始化
????????mWhitePiece?=?BitmapFactory.decodeResource(getResources(),?R.drawable.stone_w);
????????mBlackPiece?=?BitmapFactory.decodeResource(getResources(),?R.drawable.stone_b);
????}

????protected?void?onMeasure(int?widthMeasureSpec,?int?heightMeasureSpec)?{
????????int?widthSize?=?MeasureSpec.getSize(widthMeasureSpec);
????????int?widthMode?=?MeasureSpec.getMode(widthMeasureSpec);

????????int?heightSize?=?MeasureSpec.getSize(widthMeasureSpec);
????????int?heightMode?=?MeasureSpec.getMode(widthMeasureSpec);


????????int?width?=?Math.min(widthSize,?heightSize);

????????if?(widthMode?==?MeasureSpec.UNSPECIFIED)?{
????????????width?=?heightSize;
????????}?else?if?(heightMode?==?MeasureSpec.UNSPECIFIED)?{
????????????width?=?heightSize;
????????}
????????setMeasuredDimension(width,?width);
????}
????@Override
????protected?void?onSizeChanged(int?w,?int?h,?int?oldw,?int?oldh)?{
????????super.onSizeChanged(w,?h,?oldw,?oldh);
????????mPanelWidth?=?w;
????????mLineHeight?=?mPanelWidth?*?1.0f?/?MAX_Line;

????????//修改棋子尺寸
????????int?pieceWidth?=?(int)?(mLineHeight?*?ratioPieceOFLineHeight);
????????mWhitePiece?=?Bitmap.createScaledBitmap(mWhitePiece,?pieceWidth,?pieceWidth,?false);
????????mBlackPiece?=?Bitmap.createScaledBitmap(mWhitePiece,?pieceWidth,?pieceWidth,?false);
????}

????public?boolean?onTouchEvent(MotionEvent?event)?{
????????int?action?=?event.getAction();
????????if?(action?==?MotionEvent.ACTION_UP)?{
????????????//獲取點擊坐標
????????????int?x?=?(int)?event.getX();
????????????int?y?=?(int)?event.getY();
????????????Point?p?=?getValidPoint(x,?y);
????????????if?(mWhiteArray.contains(p)?||?mBlackArray.contains(p))?{
????????????????return?false;
????????????}
????????????if?(mIsWhite)?{
????????????????mWhiteArray.add(p);
????????????}?else?{
????????????????mBlackArray.add(p);
????????????}
????????????mIsWhite?=?!mIsWhite;
????????????return?true;
????????}
????????return?true;
????}

????private?Point?getValidPoint(int?x,?int?y)?{
????????return?new?Point((int)?(x?/?mLineHeight),?(int)?(y?/?mLineHeight));
????}
????@Override
????protected?void?onDraw(Canvas?canvas)?{
????????super.onDraw(canvas);
????????drawBoard(canvas);
????????drawPiece(canvas);
????}

????private?void?drawPiece(Canvas?canvas)?{
????????for?(int?i?=?0,?j?=?mWhiteArray.size();?i?<?j;?i++)?{
????????????Point?whitePoint?=?mWhiteArray.get(i);
????????????canvas.drawBitmap(mWhitePiece,
????????????????????(whitePoint.x?+?(1?-?ratioPieceOFLineHeight)?/?2)?*?mLineHeight,
????????????????????(whitePoint.y?+?(1?-?ratioPieceOFLineHeight)?/?2)?*?mLineHeight,
????????????????????null);
????????}
????????for?(int?i?=?0,?j?=?mBlackArray.size();?i?<?j;?i++)?{
????????????Point?blackPoint?=?mBlackArray.get(i);
????????????canvas.drawBitmap(mBlackPiece,
????????????????????(blackPoint.x?+?(1?-?ratioPieceOFLineHeight)?/?2)?*?mLineHeight,
????????????????????(blackPoint.y?+?(1?-?ratioPieceOFLineHeight)?/?2)?*?mLineHeight,
????????????????????null);
????????}
????}

????private?void?drawBoard(Canvas?canvas)?{
????????int?w?=?mPanelWidth;
????????float?lineheight?=?mLineHeight;

????????for?(int?i?=?0;?i?<?MAX_Line;?i++)?{
????????????int?starX?=?(int)?lineheight?/?2;//左邊棋盤開始畫的x坐標
????????????int?endX?=?(int)?(w?-?lineheight?/?2);//右邊棋盤的最后終止坐標
????????????int?y?=?(int)?((0.5?+?i)?*?lineheight);
????????????canvas.drawLine(starX,?y,?endX,?y,?mPaint);
????????????canvas.drawLine(y,?starX,?y,?endX,?mPaint);
????????}
????}
}

正在回答

舉報

0/150
提交
取消
Android-五子連珠
  • 參與學習       38999    人
  • 解答問題       174    個

Android游戲開發-五子連珠,本教程通過UI與邏輯實現雙人對戰

進入課程

點擊不出棋子

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號