記錄

Rob2) Animation, tic tac toc 본문

Mobile/Android

Rob2) Animation, tic tac toc

surhommejk 2018. 1. 30. 21:28

Animation fade되면서 사진 교체

public void changeSimson(View view){

ImageView bart = (ImageView) findViewById(R.id.bart);
ImageView homer = (ImageView) findViewById(R.id.homer);

bart.animate().alpha(0f).setDuration(2000);
homer.animate().alpha(1f).setDuration(2000);
// 여기서 0f, 1f를 해준 이유는 정확히 float값으로 인식하도록 하기 위해서
// 그냥 숫자만 쓰면 가끔 어떤 경우에는 원하는 수치가 정확히 인식이 안된다고 함
// 그냥 습관화 할 것

}




Animation 아래로 내려감

ImageView bart = (ImageView) findViewById(R.id.bart);
bart.animate().translationYBy(1000f).setDuration(2000);




tic tac toc

package com.example.bit.tik;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

int activeplayer = 0;
int[] playState = {2, 2, 2, 2, 2, 2, 2, 2, 2};
int[][] winningPositions = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6},
{1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}};
boolean gameIsActive = true;

public void dropIn(View view) {

ImageView counter = (ImageView) view;
System.out.println(counter.getTag().toString());
int tappedCounter = Integer.parseInt(counter.getTag().toString());

if ((playState[tappedCounter] == 2) && (gameIsActive == true)) {

counter.setTranslationY(-1000f);

playState[tappedCounter] = activeplayer;

if (activeplayer == 0) {
counter.setImageResource(R.drawable.yellow);
activeplayer = 1;
} else {
counter.setImageResource(R.drawable.red);
activeplayer = 0;
}

counter.animate().translationYBy(1000f).setDuration(300);

for (int[] a : winningPositions) {
if ((playState[a[0]] == playState[a[1]]) &&
(playState[a[1]] == playState[a[2]]) && playState[a[0]] != 2) {
System.out.println(playState[a[0]]);
gameIsActive = false;

String winner = "Red";

if (playState[a[0]] == 0) winner = "Yellow";

TextView tv = (TextView) findViewById(R.id.winnertext);
tv.setText(winner + " Win!");

LinearLayout li = (LinearLayout) findViewById(R.id.Linearlay);
li.setVisibility(View.VISIBLE);


} else {

boolean gameisgoingon = true;

for (int i = 0; i < playState.length; i++) {
if (playState[i] == 2) gameisgoingon = false;
}

if (gameisgoingon) {
TextView tv = (TextView) findViewById(R.id.winnertext);
tv.setText("It's a draw");

LinearLayout li = (LinearLayout) findViewById(R.id.Linearlay);
li.setVisibility(View.VISIBLE);
}

}
}
}

} // end-dromIn

public void playagain(View view) {
gameIsActive = true;
LinearLayout li = (LinearLayout) findViewById(R.id.Linearlay);
li.setVisibility(View.INVISIBLE);

activeplayer = 0;
for (int i = 0; i < playState.length; i++) {
playState[i] = 2;
}

GridLayout gri = (GridLayout) findViewById(R.id.gridlay);
for (int i = 0; i < gri.getChildCount(); i++) {
((ImageView) gri.getChildAt(i)).setImageResource(0);
}

}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bit.tik.MainActivity">

<GridLayout
android:id="@+id/gridlay"
android:layout_width="wrap_content"
android:layout_height="360dp"
android:columnCount="3"
android:rowCount="3"
android:background="@drawable/board"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>

<ImageView
android:onClick="dropIn"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_row="0"
android:layout_column="0"
android:tag="0"
/>

<ImageView
android:onClick="dropIn"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="40dp"
android:layout_row="0"
android:layout_column="1"
android:tag="1"
/>
<ImageView
android:onClick="dropIn"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="40dp"
android:layout_row="0"
android:layout_column="2"
android:tag="2"
/>
<ImageView
android:onClick="dropIn"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginTop="25dp"
android:layout_marginLeft="10dp"
android:layout_row="1"
android:layout_column="0"
android:tag="3"
/>

<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_column="1"
android:layout_marginLeft="40dp"
android:layout_marginTop="25dp"
android:layout_row="1"
android:onClick="dropIn"
android:tag="4"/>
<ImageView
android:onClick="dropIn"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginTop="25dp"
android:layout_marginLeft="40dp"
android:layout_row="1"
android:layout_column="2"
android:tag="5"
/>
<ImageView
android:onClick="dropIn"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:layout_row="2"
android:layout_column="0"
android:tag="6"
/>
<ImageView
android:onClick="dropIn"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="40dp"
android:layout_row="2"
android:layout_column="1"
android:tag="7"
/>
<ImageView
android:onClick="dropIn"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="40dp"
android:layout_row="2"
android:layout_column="2"
android:tag="8"
/>





</GridLayout>
<LinearLayout
android:id="@+id/Linearlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:background="@color/colorPrimary"
android:orientation="vertical"
android:visibility="invisible">

<TextView
android:id="@+id/winnertext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You Win!!"
android:textSize="30dp"
android:textAlignment="center"/>

<Button
android:id="@+id/playAgainButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="playagain"
android:text="play again"
android:textSize="20dp" />

</LinearLayout>
</RelativeLayout>


Comments