最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android自定義View實現(xiàn)簡單五子棋游戲

 更新時間:2022年05月06日 09:08:32   作者:SakuraMashiro  
這篇文章主要為大家詳細介紹了android自定義View實現(xiàn)簡單五子棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

做一個五子棋練練手,沒什么特別的,再復習一下自定義View的知識,onMeasure,MeasureSpec , onDraw以及OnTouchEvent方法等。

效果圖

代碼如下:

package com.fivechess;

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.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

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

public class GamePanel extends View {

? ? //棋盤寬度
? ? private int mPanelWidth ;
? ? //每一個棋格的寬
? ? private float mLineHeight ;

? ? //棋盤最大的行數(shù)
? ? private int MAX_LINE = 10 ;
? ? //最多連線的棋子個數(shù)
? ? private int MAX_COUNT_IN_LINE = 5 ;

? ? private Paint mPaint = new Paint();

? ? //定義黑白棋子的Bitmap
? ? private Bitmap mWhitePiece ;
? ? private Bitmap mBlackPiece ;

? ? //棋子占一個棋格的比例,這里是3/4
? ? private float ratioPieceOfLineHeight = 3 * 1.0f / 4 ;

? ? private boolean isWhite = false ;

? ? //存放已下過的棋子的數(shù)組
? ? private ArrayList<Point> mWhiteArray = new ArrayList<>();
? ? private ArrayList<Point> mBlackArray = new ArrayList<>();

? ? //標識對局是否結束
? ? private boolean isGameOver ;
? ? //判斷白棋是否獲勝
? ? private boolean isWhiteWinner ;

? ? //構造方法
? ? public GamePanel(Context context, AttributeSet attrs) {
? ? ? ? super(context, attrs);
? ? ? ? setBackgroundColor(0x80f8c866);
? ? ? ? init();
? ? }

? ? //初始化畫筆及Bitmap
? ? private void init() {
? ? ? ? mPaint.setColor(0x88000000);
? ? ? ? mPaint.setAntiAlias(true);
? ? ? ? mPaint.setDither(true);
? ? ? ? mPaint.setStyle(Paint.Style.STROKE);

? ? ? ? mWhitePiece = BitmapFactory.decodeResource(getResources() , R.drawable.white_chess) ;
? ? ? ? mBlackPiece = BitmapFactory.decodeResource(getResources() , R.drawable.black_chess) ;
? ? }

? ? //測量過程
? ? @Override
? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
? ? ? ? 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 );

? ? ? ? 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
? ? protected void onDraw(Canvas canvas) {
? ? ? ? super.onDraw(canvas);

? ? ? ? drawBoard(canvas);
? ? ? ? drawPieces(canvas) ;
? ? ? ? checkGameOver();
? ? }

? ? //判斷是否結束
? ? private void checkGameOver() {

? ? ? ? boolean whiteWin = checkFiveInLine( mWhiteArray );
? ? ? ? boolean blackWin = checkFiveInLine( mBlackArray );

? ? ? ? if( whiteWin || blackWin ){
? ? ? ? ? ? isGameOver = true ;
? ? ? ? ? ? isWhiteWinner = whiteWin ;
? ? ? ? ? ? String text = isWhiteWinner ? "白棋勝利" : "黑棋勝利" ;
? ? ? ? ? ? Toast.makeText(getContext() , text , Toast.LENGTH_SHORT).show();
? ? ? ? }
? ? }


? ? //判斷是否五子連珠
? ? private boolean checkFiveInLine(List<Point> points) {

? ? ? ? for( Point p : points ){
? ? ? ? ? ? int x = p.x ;
? ? ? ? ? ? int y = p.y ;

? ? ? ? ? ? boolean win = checkHorizontal( x , y , points) ;
? ? ? ? ? ? if( win ) return true ;
? ? ? ? ? ? win = checkVertical( x , y , points) ;
? ? ? ? ? ? if( win ) return true ;
? ? ? ? ? ? win = checkLeftDown( x , y , points) ;
? ? ? ? ? ? if( win ) return true ;
? ? ? ? ? ? win = checkRightDown( x , y , points) ;
? ? ? ? ? ? if( win ) return true ;

? ? ? ? }

? ? ? ? return false;

? ? }

? ? /**
? ? ?* 判斷x,y位置的棋子是否橫向有相鄰的5個一致
? ? ?* @param x
? ? ?* @param y
? ? ?* @param points
? ? ?* @return
? ? ?*/
? ? private boolean checkHorizontal(int x, int y, List<Point> points) {

? ? ? ? //五個子的計數(shù)器
? ? ? ? int count = 1 ;

? ? ? ? //判斷左邊
? ? ? ? for( int i = 1 ; i < MAX_COUNT_IN_LINE ; i ++ ){

? ? ? ? ? ? if( points.contains(new Point(x - i , y ))){
? ? ? ? ? ? ? ? count ++ ;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? break ;
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? if( count == MAX_COUNT_IN_LINE ) return true ;

? ? ? ? //判斷右邊
? ? ? ? for( int i = 1 ; i < MAX_COUNT_IN_LINE ; i ++ ){

? ? ? ? ? ? if( points.contains(new Point(x + i , y ))){
? ? ? ? ? ? ? ? count ++ ;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? break ;
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? if( count == MAX_COUNT_IN_LINE ) return true ;

? ? ? ? return false ;
? ? }

? ? /**
? ? ?* 判斷x,y位置的棋子是否垂直方向有相鄰的5個一致
? ? ?* @param x
? ? ?* @param y
? ? ?* @param points
? ? ?* @return
? ? ?*/
? ? private boolean checkVertical(int x, int y, List<Point> points) {

? ? ? ? //五個子的計數(shù)器
? ? ? ? int count = 1 ;

? ? ? ? //判斷上邊
? ? ? ? for( int i = 1 ; i < MAX_COUNT_IN_LINE ; i ++ ){

? ? ? ? ? ? if( points.contains(new Point(x , y - i ))){
? ? ? ? ? ? ? ? count ++ ;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? break ;
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? if( count == MAX_COUNT_IN_LINE ) return true ;

? ? ? ? //判斷下邊
? ? ? ? for( int i = 1 ; i < MAX_COUNT_IN_LINE ; i ++ ){

? ? ? ? ? ? if( points.contains(new Point(x , y + i ?))){
? ? ? ? ? ? ? ? count ++ ;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? break ;
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? if( count == MAX_COUNT_IN_LINE ) return true ;

? ? ? ? return false ;
? ? }

? ? private boolean checkLeftDown(int x, int y, List<Point> points) {

? ? ? ? //五個子的計數(shù)器
? ? ? ? int count = 1 ;

? ? ? ? //判斷左下方
? ? ? ? for( int i = 1 ; i < MAX_COUNT_IN_LINE ; i ++ ){

? ? ? ? ? ? if( points.contains(new Point(x - i ?, y + i ))){
? ? ? ? ? ? ? ? count ++ ;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? break ;
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? if( count == MAX_COUNT_IN_LINE ) return true ;

? ? ? ? //判斷右上方
? ? ? ? for( int i = 1 ; i < MAX_COUNT_IN_LINE ; i ++ ){

? ? ? ? ? ? if( points.contains(new Point(x + i , y - i ?))){
? ? ? ? ? ? ? ? count ++ ;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? break ;
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? if( count == MAX_COUNT_IN_LINE ) return true ;

? ? ? ? return false ;
? ? }

? ? private boolean checkRightDown(int x, int y, List<Point> points) {

? ? ? ? //五個子的計數(shù)器
? ? ? ? int count = 1 ;

? ? ? ? //判斷左上方
? ? ? ? for( int i = 1 ; i < MAX_COUNT_IN_LINE ; i ++ ){

? ? ? ? ? ? if( points.contains(new Point(x - i ?, y - i ))){
? ? ? ? ? ? ? ? count ++ ;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? break ;
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? if( count == MAX_COUNT_IN_LINE ) return true ;

? ? ? ? //判斷右下方
? ? ? ? for( int i = 1 ; i < MAX_COUNT_IN_LINE ; i ++ ){

? ? ? ? ? ? if( points.contains(new Point(x + i , y + i ?))){
? ? ? ? ? ? ? ? count ++ ;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? break ;
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? if( count == MAX_COUNT_IN_LINE ) return true ;

? ? ? ? return false ;
? ? }

? ? //繪制棋子的方法
? ? private void drawPieces(Canvas canvas) {

? ? ? ? for( int i = 0 , n = mWhiteArray.size() ; i < n ; 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 , n = mBlackArray.size() ; i < n ; 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 startX = (int) (lineHeight/2);
? ? ? ? ? ? int endX = (int) (w - lineHeight/2);
? ? ? ? ? ? int y = (int) (( 0.5 + i ) * lineHeight);

? ? ? ? ? ? //繪制棋盤的橫線
? ? ? ? ? ? canvas.drawLine(startX, y , endX , y , mPaint);
? ? ? ? ? ? //繪制棋盤的豎線
? ? ? ? ? ? canvas.drawLine(y , startX , y , endX , mPaint );
? ? ? ? }
? ? }

? ? @Override
? ? public boolean onTouchEvent(MotionEvent event) {

? ? ? ? if( isGameOver ) return false ;

? ? ? ? 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( isWhite ){
? ? ? ? ? ? ? ? mWhiteArray.add(p);
? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? mBlackArray.add(p);
? ? ? ? ? ? }
? ? ? ? ? ? invalidate();

? ? ? ? ? ? isWhite = !isWhite ;


? ? ? ? }
? ? ? ? return true;
? ? }

? ? //獲取點擊的有效地址
? ? private Point getValidPoint(int x, int y) {

? ? ? ? return new Point( (int )(x / mLineHeight) , (int ) (y / mLineHeight) );
? ? }

? ? //再來一局
? ? public void restart(){

? ? ? ? mWhiteArray.clear();
? ? ? ? mBlackArray.clear();
? ? ? ? isGameOver = false ;
? ? ? ? isWhiteWinner = false ;
? ? ? ? invalidate();
? ? }

/*

? ? //定義常量
? ? private static final String INSTANCE = "instance" ;
? ? private static final String INSTANCE_GAME_OVER = "instance_game_over";
? ? private static final String INSTANCE_WHITE_ARRAY = "instance_white_array";
? ? private static final String INSTANCE_BLACK_ARRAY = "instance_black_array";

? ? //保存當前游戲狀態(tài)
? ? @Override
? ? protected Parcelable onSaveInstanceState() {
? ? ? ? Bundle bundle = new Bundle();
? ? ? ? bundle.putParcelable(INSTANCE , super.onSaveInstanceState());
? ? ? ? bundle.putBoolean(INSTANCE_GAME_OVER , isGameOver);
? ? ? ? bundle.putParcelableArray(INSTANCE_WHITE_ARRAY , ?mWhiteArray);
? ? ? ? bundle.putParcelableArray(INSTANCE_BLACK_ARRAY , ?mBlackArray);
? ? ? ? return bundle ;
? ? }

? ? //恢復棋局狀態(tài)
? ? @Override
? ? protected void onRestoreInstanceState(Parcelable state) {
? ? ? ? if( state instanceof ?Bundle ){

? ? ? ? ? ? Bundle bundle = (Bundle) state;
? ? ? ? ? ? isGameOver = bundle.getBoolean( INSTANCE_GAME_OVER);
? ? ? ? ? ? mWhiteArray = bundle.getParcelableArrayList( INSTANCE_WHITE_ARRAY );
? ? ? ? ? ? mBlackArray = bundle.getParcelableArrayList( INSTANCE_BLACK_ARRAY );

? ? ? ? ? ? super.onRestoreInstanceState(bundle.getParcelable(INSTANCE));
? ? ? ? ? ? return;
? ? ? ? }

? ? ? ? super.onRestoreInstanceState(state);
? ? }*/
}

在布局文件中引入該View

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:id="@+id/activity_main"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? tools:context="com.fivechess.MainActivity"
? ? android:background="@drawable/board_bg">

? ? <com.fivechess.GamePanel
? ? ? ? android:id="@+id/game_panel"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="match_parent" />
? ? <Button
? ? ? ? android:id="@+id/button"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:text="再來一局"
? ? ? ? android:layout_alignParentBottom="true"/>
</RelativeLayout>

MainActivity的代碼

package com.fivechess;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

? ? private Button button ;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? button = (Button) findViewById(R.id.button);
? ? ? ? final GamePanel gamePanel = (GamePanel) findViewById(R.id.game_panel);
? ? ? ? button.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? gamePanel.restart();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 使用ListView實現(xiàn)網(wǎng)上訂餐首頁

    使用ListView實現(xiàn)網(wǎng)上訂餐首頁

    這篇文章主要為大家詳細介紹了使用ListView實現(xiàn)網(wǎng)上訂餐首頁,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • Android開發(fā)之天氣趨勢折線圖

    Android開發(fā)之天氣趨勢折線圖

    在開發(fā)天氣APP的時候會要顯示多天的信息,所以加一個折線圖來顯示一下天氣變化趨勢是很不錯的效果,本文詳細介紹了開發(fā)過程,下面一起來看看。
    2016-08-08
  • Android簡單實現(xiàn)圓盤抽獎界面

    Android簡單實現(xiàn)圓盤抽獎界面

    這篇文章主要介紹了Android簡單實現(xiàn)圓盤抽獎界面的相關資料,需要的朋友可以參考下
    2016-01-01
  • Android實現(xiàn)文本排版

    Android實現(xiàn)文本排版

    這篇文章主要介紹了Android實現(xiàn)文本排版,對多行文本進行排版布局,每一行的內容又分為兩部分,左邊為標題,右邊為描述,左邊內容長度不確定,右邊的內容需要對齊,需要的朋友可以參考下
    2016-04-04
  • XListView實現(xiàn)網(wǎng)絡加載圖片和下拉刷新

    XListView實現(xiàn)網(wǎng)絡加載圖片和下拉刷新

    這篇文章主要為大家詳細介紹了XListView實現(xiàn)網(wǎng)絡加載圖片和下拉刷新,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Android錄音并且輸出為Mp4文件的方法教程

    Android錄音并且輸出為Mp4文件的方法教程

    這篇文章主要給大家介紹了關于Android錄音并且輸出為Mp4文件的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-08-08
  • Android adb logcat 命令查看日志詳細介紹

    Android adb logcat 命令查看日志詳細介紹

    這篇文章主要介紹了Android adb logcat 命令詳細介紹的相關資料,這里對logcat 命令進行了詳細介紹,并介紹了過濾日志輸出的知識,需要的朋友可以參考下
    2016-12-12
  • Android如何通過組合的方式自定義View

    Android如何通過組合的方式自定義View

    這篇文章主要介紹了Android如何通過組合的方式自定義View,文章通過圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • Android通過Webservice操作sqlserver數(shù)據(jù)庫實例代碼

    Android通過Webservice操作sqlserver數(shù)據(jù)庫實例代碼

    這篇文章主要介紹了Android通過Webservice操作sqlserver數(shù)據(jù)庫的相關知識,對webservice操作數(shù)據(jù)庫相關知識感興趣的朋友一起學習吧
    2016-01-01
  • Android支付寶和微信支付集成

    Android支付寶和微信支付集成

    這篇文章主要為大家詳細介紹了Android支付寶和微信支付集成的相關資料,需要的朋友可以參考下
    2016-07-07

最新評論

英德市| 利川市| 丽江市| 武夷山市| 全州县| 福泉市| 昭通市| 太原市| 陕西省| 宁都县| 垣曲县| 绥化市| 和龙市| 南宁市| 辽宁省| 甘肃省| 鄱阳县| 白沙| 正宁县| 榕江县| 兴和县| 南川市| 遂昌县| 云安县| 鄂托克前旗| 鄂伦春自治旗| 商城县| 正镶白旗| 剑川县| 韩城市| 大庆市| 乌兰县| 齐齐哈尔市| 凤山市| 连平县| 吉安市| 长子县| 建始县| 克拉玛依市| 涟水县| 武冈市|