ITI0011:Android praktikum 2

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti

Lähtekood

24. praktikumis tehtud kood: Meedia:ITI0011-Android-Mull.zip

Põhilised koodifailid

MainActivity.java

<source lang="java"> package ee.ttu.mull;

import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.RelativeLayout;

import java.util.Random;


public class MainActivity extends Activity {

private RelativeLayout gameBoard; private int width; private int height;

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

gameBoard = (RelativeLayout) findViewById(R.id.gameBoard);

startGame(); }

@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus);

width = gameBoard.getWidth(); height = gameBoard.getHeight(); }

private void addButton() { final SquareButton button = new SquareButton(this, null);

button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { gameBoard.removeView(button); } });

int x = randomInt(width - button.getSize()); int y = randomInt(height - button.getSize());

button.setX(x); button.setY(y);

gameBoard.addView(button); }

private int randomInt(int max) { return new Random().nextInt(max + 1); }

private void startGame() { Thread t = new Thread() { @Override public void run() { try { while (true) { Thread.sleep(2000);

runOnUiThread(new Runnable() { @Override public void run() { addButton(); } }); } } catch (InterruptedException e) {} } }; t.start(); } }

</source>

SquareButton.java

<source lang="java"> package ee.ttu.mull;

import android.content.Context; import android.util.AttributeSet; import android.widget.Button;

public class SquareButton extends Button {

private int size = 280;

public SquareButton(Context context, AttributeSet attrs) { super(context, attrs);

setBackgroundResource(R.drawable.square_button); }

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec);

setMeasuredDimension(size, size); }

public int getSize() { return size; }

}

</source>

res/drawable/square_button.xml

<source lang="xml"> <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"

   android:shape="rectangle">
   <solid android:color="#ff4d79b9"/>

</shape> </source>

res/layout/activity_main.xml

<source lang="xml"> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

               xmlns:tools="http://schemas.android.com/tools"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:paddingLeft="@dimen/activity_horizontal_margin"
               android:paddingRight="@dimen/activity_horizontal_margin"
               android:paddingTop="@dimen/activity_vertical_margin"
               android:paddingBottom="@dimen/activity_vertical_margin"
               tools:context=".MainActivity">
   <RelativeLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_alignParentTop="true"
       android:layout_alignParentStart="true"
       android:id="@+id/gameBoard">
   </RelativeLayout>

</RelativeLayout>

</source>