2013-08-20 126 views
0

所以我創建了一個棋盤遊戲,它使用了一張9x9棋盤,其邊緣/角落和棋盤中間的圖像不同。在做了大量研究之後,人們似乎建議使用帶有按鈕或圖像按鈕的TableLayout用於電路板上的每個單獨空間。Android棋盤遊戲實現

我想知道的是,在我的遊戲中,這些棋子也可以每回合旋轉45度。我最初的計劃是簡單地將這些作品作爲imageButton的一部分,但我不知道如何旋轉它。我能想到的一種選擇是每個45deg旋轉只有一個單獨的圖像,但這看起來非常低效,因爲它需要每張8張圖像。

問題:

  • 是一個tablelayout落實我局的正確方法?
  • 我應該爲我的主板上的每個空間使用imagebuttons嗎?
  • 什麼是最好的方式來旋轉我的作品?我應該在整個遊戲中使用畫布方式嗎?

謝謝,請讓我知道,如果有什麼不清楚。

回答

1
  • 是表格佈局是這種佈局IMO
  • 如果你有推你可以使用ImageButtons在圖像上,否則只是使用的ImageView的一個很好的形式給出。
  • 您可以通過以下方式旋轉drawable。

    private void updateImageOrientation(final float rotationAngle) { 
    
        // rotate compass to right orientation 
        final ImageView img = (ImageView) findViewById(R.id.actMyDrawableImage); 
        // only if imageView in layout 
    
        if (img != null) { 
        final Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.act_my_drawable); 
        // Getting width & height of the given image. 
        final int w = bmp.getWidth(); 
        final int h = bmp.getHeight(); 
        // Setting post rotate to rotation angle 
        final Matrix mtx = new Matrix(); 
        // Log.v(LOG_TAG, "Image rotation angle: " + rotationAngle); 
        mtx.postRotate(rotationAngle, (float) (w/2.0), (float) (h/2.0)); 
        // Rotating Bitmap 
        final Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true); 
        final BitmapDrawable bmd = new BitmapDrawable(getResources(), rotatedBMP); 
    
        img.setImageDrawable(bmd); 
        } 
    
    } 
    

編輯1

要使用的ImageButton只是ImageButton的在他的代碼替換上述ImageView的。

final ImageButton img = (ImageButton) findViewById(R.id.actMyDrawableImage); 

img.setImageDrawable(drawable) 

編輯2

要顯示你的作品您的主板上面,你可以使用的FrameLayout爲您的每個細胞。背景將被設定:使用的ImageView如下

  • 與對的FrameLayout背景標誌

    • (安卓背景)
    • ,如果你想爲你的主板與父背景標誌一個背景TableLayout

    你可以讓你的作品顯示/隱藏編程:

    img.setVisibility(View.VISIBLE); 
    
    img.setVisibility(View.INVISIBLE); 
    

    <FrameLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" > 
    
        <ImageView 
         android:id="@+id/actMyDrawableButton" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:visibility="visible" > 
        </ImageView> 
    
        <ImageButton 
         android:id="@+id/actMyDrawableButton" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:visibility="invisible" > 
        </ImageButton> 
    </FrameLayout> 
    
  • +0

    有沒有辦法將我的遊戲板瓷磚與實際的棋子分開?你可以把ImageView放在ImageButton上面嗎?我需要能夠點擊一塊,並希望通過切換棋子可以移動到的棋盤上的棋盤圖像來突出顯示可能的棋步。 – Kinru

    +0

    我不明白你的第一個問題,對不起。不,您不必將ImageView放在ImageButton的頂部,只需使用ImageButton而不是ImageView,我就會更新我的答案。 –

    +0

    對不起,我會盡力解釋我的第一個問題。我的意思是,我的遊戲板顯然有實際板子的瓷磚,而且我也有獨立的棋子。董事會的瓷磚將永遠在那裏,我試圖找出一種方法讓我的作品坐在瓷磚上。對於旋轉部分,我只希望碎片而不是碎片旋轉。這將要求圖像是分開的。我想知道是否有辦法做到這一點。謝謝您的幫助。 – Kinru