編輯:關於Android編程
主要實現簡單的互動
主要代碼:
package com.adolph.wuzhiqi; import android.content.Context; import android.content.pm.ProviderInfo; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Point; import android.text.style.TtsSpan; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.Toast; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2016/9/25. */ public class WuzhiqiPanel extends View{ private int mPanelWidth; private float mlineHeight; private int MAX_LINE = 10; private int MAX_COUNT_IN_LINE=5; private Paint mPaint = new Paint();//得到畫筆 //BitMap代表一張位圖 private Bitmap mWhitePiece; private Bitmap mBlackPiece; private float ratioPieceOfLineHeight = 3 * 1.0f /4;//設置棋子比例 //白棋先下 private boolean mIsWite = true;//標志那個棋子在下 private ListmWiteArray = new ArrayList<>(); private List mBlackArray = new ArrayList<>(); private boolean mIsGameOver; private boolean mIsWhiteWinner; public WuzhiqiPanel(Context context, AttributeSet attrs){ super(context,attrs); setBackgroundColor(0x44ff0000); init(); } //初始化畫筆 private void init(){ //初始化棋盤 mPaint.setColor(0x8800000);//設置畫筆顏色 mPaint.setAntiAlias(true);//設置畫筆的鋸齒效果 mPaint.setDither(true);//設定是否使用圖像抖動處理,會使繪制出來的圖片顏色更加平滑和飽滿,圖像更加清晰 mPaint.setStyle(Paint.Style.STROKE);//設置畫筆風格,空心或者實心。 //初始化棋子,得到圖片 mWhitePiece = BitmapFactory.decodeResource(getResources(),R.drawable.stone_w2); mBlackPiece = BitmapFactory.decodeResource(getResources(),R.drawable.stone_b1); } //當控件的父元素正要放置該控件時調用,測量 /*支持match_parent*/ protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) { //邊界參數——widthMeasureSpec和heightMeasureSpec ,效率的原因以整數的方式傳入。 //在它們使用之前,首先要做的是使用MeasureSpec類的靜態方法getMode和getSize來譯解 int widthSize = MeasureSpec.getSize(widthMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int width = Math.min(widthSize,heightSize);//保證是正方形,取最小值作邊 /*如果寬等於0就取高的值,如果高等於0就取寬的值*/ if (widthMode == MeasureSpec.UNSPECIFIED){ width = heightSize; }else if (heightMode == MeasureSpec.UNSPECIFIED){ width = widthSize; } 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(mBlackPiece,pieceWidth,pieceWidth,false); } //觸發執行,繪制棋子 @Override public boolean onTouchEvent(MotionEvent event) { //游戲結束,表示不感興趣 if (mIsGameOver) return false; //得到這個事件 int action = event.getAction(); /*ACTION_UP,ACTION_DOWN,ACTION_CANCEL,ACTION_MOVE*/ if (action == MotionEvent.ACTION_UP){ //得到點擊的坐標 int x = (int)event.getX(); int y = (int)event.getY(); //處理點擊誤差 Point p = getValidPoint(x,y); //判斷當前位置是否有棋子 if (mWiteArray.contains(p)||mBlackArray.contains(p)){ return false; } //Point p = new Point(x,y); if (mIsWite){ mWiteArray.add(p); }else { mBlackArray.add(p); } //重繪 invalidate(); mIsWite = !mIsWite; } 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); drawPieces(canvas); CheckGameOvwe(); } private void CheckGameOvwe() { //判斷白棋是否贏了 boolean whiteWin = checkFiveInLine(mWiteArray); //判斷黑棋是否贏了 boolean blackWin = checkFiveInLine(mBlackArray); if(whiteWin || blackWin){ mIsGameOver = true; mIsWhiteWinner = whiteWin; String text = mIsWhiteWinner ? "白棋勝利" : "黑棋勝利"; Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show(); } } private boolean checkFiveInLine(List points) { for (Point p : points){ int x = p.x; int y = p.y; boolean win; win = checkHorizontal(x,y,points); if (win) return true; win = checkLeftDiagonal(x,y,points); if (win) return true; win = checkRightDiagonal(x,y,points); if (win) return true; win = checkVertical(x,y,points); if (win) return true; } return false; } //判斷橫向相鄰五個是否相同棋子 private boolean checkHorizontal(int x, int y, List points) { int count =1; //檢查是否達到五個 for (int i=1; i points) { int count =1; //檢查是否達到五個 for (int i=1; i points) { int count =1; //檢查是否達到五個 for (int i=1; i points) { int count =1; //檢查是否達到五個 for (int i=1; i
本文實例為大家詳細介紹了Android開發之HTTP訪問網絡的相關代碼,供大家參考,具體內容如下代碼1:package com.ywhttpurlconnection;i
直接代碼 package com.example.demoemail; import android.net.Uri; import android.os.Bundl
本篇文章的主要內容是說圖片的內存緩存和從網絡異步獲取圖片的過程工程效果圖如下: 代碼中部分不常見的屬性解釋如下(百度也可以找到的)android:numCo
很久沒有空更新博客了,以至於挺多東西都用過之後就忘記了,沒有很好的記錄下來,之前在工作的時候也是這樣,用完就忘記,所以覺得還是很有必要把自己用過的一些東西,