Tag Archives: android

LogicGate cz5

Ciąg dalszy implementacji klas.

Implementacja klasy Wire

package com.adam_mistal.logicgateapp;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class Wire {
	//Pin wysyłający sygnał i odbierający
	private Pin pin_one;
	private Pin pin_two;
	private Paint wireBrush = null;
	private int WireWidth=10;
	private float[] pts;
	public Wire(Pin pin1,Pin pin2){
		pin_one=pin1;
		pin_two=pin2;

		if(pin_one.isMaster){
			//pin pierwszy  podaje sygnał
			pin_one.refPin=pin_two;

		};
		if(pin_two.isMaster){
			//pin drugi podaje sygnał
			pin_two.refPin=pin_one;
		};
		wireBrush = new Paint();
		wireBrush.setColor(Color.RED);
		wireBrush.setDither(true);
		wireBrush.setAlpha(150);
		wireBrush.setStrokeWidth(WireWidth);
		parse(pin_one.getPoint(), pin_two.getPoint());

	}
	/**
	 * rysuje przewód
	 * @param canvas
	 */
	public void draw(Canvas canvas){
		canvas.drawLines(pts, wireBrush);
	}
	/**
	 * rysuje linie miedzy pinami w ten sposób aby załąmanie lini było prostopadłe
	 * @param p1
	 * @param p2
	 */
	private void parse(Point p1,Point p2){
		Point n;
		if(p1.x!=p2.x){
			n=new Point(p2.x, p1.y);
			pts=new float[8];
			pts[0]=p1.x;
			pts[1]=p1.y;
			pts[2]=n.x;
			pts[3]=n.y;
			pts[4]=n.x;
			pts[5]=n.y;
			pts[6]=p2.x;
			pts[7]=p2.y;

		}else{
			pts=new float[4];
			pts[0]=p1.x;
			pts[1]=p1.y;
			pts[2]=p2.x;
			pts[3]=p2.y;

		}

	}

}

Implementacja klasy OnOffButton

package com.adam_mistal.logicgateapp;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;

public class OnOffButton {
	public static  int Width = 80;
	public  int Height = 80;
	private  String Text = "Off";
	private int TextSize=50;
	private int colorOn=Color.GREEN;
	private int colorOff=Color.GRAY;
	private int Left = 0;
	private int Top = 0;
	private int Right=0;
	private int Bootom=0;
	private int textX,textY;
	private boolean isOn=false;
	private Paint paintBrush = null;
	private Paint textBrush = null;
	private Pin pin=null;
	public OnOffButton() {
		paintBrush = new Paint();
		paintBrush.setColor(colorOff);	
		paintBrush.setAntiAlias(true);
		paintBrush.setDither(true);	
		
		textBrush = new Paint();
		textBrush.setColor(Color.BLACK);	
		textBrush.setAntiAlias(true);
		textBrush.setDither(true);
		textBrush.setTextSize(TextSize);
		setTopPin();
		
		
	}

	public void draw(Canvas canvas)
	   {	
		canvas.drawRect(Left, Top, Right, Bootom, paintBrush);	
		canvas.drawText(Text, textX, textY, textBrush);
		pin.draw(canvas);
	   }
   /*
    * usatwiam pin na górze elementu
    */
	public void setTopPin(){
		pin =new Pin(true);
		pin.setCenter(Left+Width/2, Top-Pin.Height);
	}
	
	
	
	public void checkIfClicked(Point clickPoint){
		//sprawdzam czy kliknięto przycisk
		if(Tools.checkIfClickedInRect(clickPoint, Left, Right, Bootom, Top)){
			onTouch();	
		}
				
	}

	private void onTouch() {
		//usatwieni pinów
		if(isOn){
			
			paintBrush.setColor(colorOff);
			pin.setValue(false);
			pin.refPin.setValue(false);
			Text = "Off";
			isOn=false;
			
		}else{
			paintBrush.setColor(colorOn);
			pin.setValue(true);
			pin.refPin.setValue(true);
			Text = "On";
			isOn=true;
		}
		
		Log.i("OnOffButton", "ison?"+isOn);
	
		
	}

	public void setRight(int r) {
		Right=r;
		Left=r-Width;
		textX=Left+5;
		setTopPin();
		
	}
	public void setLeft(int l) {	
		Left=l;	
		Right=l+Width;
		textX=Left+5;
		setTopPin();
		
	}

	public void setTop(int t) {
		Top=t;
		Bootom=t+Height;
		textY=Top+Width/2;
		setTopPin();
		
	}

	public Pin getPin() {
		
		return pin;
	}

}



Implementacja klasy Tools


package com.adam_mistal.logicgateapp;

public class Tools {
	//zwraca długość pomiędzy punktami
	public static double getLength2D(Point p1,Point p2){
		double sdx=p2.x-p1.x;
		double sdy=p2.y-p1.y;
		double slength2D=Math.sqrt(Math.pow(sdx, 2)+Math.pow(sdy, 2));
		
		return slength2D;
				
	}
	//zwraca true jeśli kliknięto w podany region
	public static boolean checkIfClickedInRect(Point p,int left,int right,int bootom,int top){
		double testX=p.x;
		double testY=p.y;
		if(testX>left&&testX<right)
			if(testY>top&&testY<bootom){
				return true;
			}
	
	
		
		return false;
				
	}

}


	

Implementacja klasy Lamp

package com.adam_mistal.logicgateapp;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class Lamp {
	private int centerW=0;
	private int centerH=0;
	private int radius=40;
	private Paint paintBrush = null;
	private Pin pin=null;
	public Lamp(){
		pin=new Pin(false);
		paintBrush = new Paint();
		paintBrush.setColor(Color.GREEN);
		paintBrush.setAntiAlias(true);
		paintBrush.setDither(true);		
	}
 
	public void draw(Canvas canvas) {
		updatePin();
		canvas.drawCircle(centerW, centerH, radius, paintBrush);
		pin.draw(canvas);
	}

	private void updatePin() {
		if(pin.value){
			switchOn();
		}else{
			switchOff();
		}
		
	}

	public void setCenter(int cW, int cH) {
		centerH=cH;
		centerW=cW;	
		pin.setCenter(cW, cH+radius);
	}

	public void setRadius(int i) {
		radius=i;		
	}
	private void switchOn(){
		paintBrush.setColor(Color.YELLOW);
		
		
	}
	private void switchOff(){
		paintBrush.setColor(Color.GRAY);
		
	}

	public Point getCenter() {
		
		return new Point(centerW, centerH);
	}

	public Pin getPin() {
		
		return pin;
	}

}


	

Utworzenie klasy Gate będącej klasą bazową dla szczególnych bramek logicznych

package com.adam_mistal.logicgateapp;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;

public abstract class Gate{
	protected int center_Y;
	protected int center_X;
	protected int left;
	protected int right;
	protected int top;
	protected int bottom;
	private Paint paintBrush = null;
	private Paint textBrush = null;
	protected Pin[] in_pins;
	protected int in_pin_count=0;
	private int pins=0;
	protected Pin outPin=null;
	protected Path path;
	private int TextSize;
	private int textX;
	private int textY;
	protected String gate_name="Gate";

	public Gate() {
		paintBrush = new Paint();
		setColor(Color.BLUE, 150);
		setTextColor(Color.YELLOW);
		setTextSize(40);		
		path=new Path();
		outPin=new Pin(true);
		
	}
	protected void setText(String t){
		gate_name=t;
	}
	
	public void setTextSize(int i) {
		TextSize=i;
		textBrush.setTextSize(TextSize);
		
		
	}
	public void setTextColor(int col) {
		textBrush = new Paint();
		textBrush.setColor(col);	
		textBrush.setAntiAlias(true);
		textBrush.setDither(true);
		
		
	}
	public void setColor(int color,int alfa){
		paintBrush.setColor(color);
		paintBrush.setAlpha(alfa);
		paintBrush.setStyle(Paint.Style.FILL_AND_STROKE);
		paintBrush.setStrokeWidth(2);
	}

	public void draw(Canvas canvas) {
		updateOutPin();
		canvas.drawPath(path, paintBrush);	
		draw_in_pins(canvas);
		outPin.draw(canvas);
		drawGateNAme(canvas);
	}
	
	private void drawGateNAme(Canvas canvas) {
		canvas.drawText(gate_name, textX, textY, textBrush);
		
	}
	private void draw_in_pins(Canvas canvas) {
		for(Pin p:in_pins){
			p.draw(canvas);
		}
		
	}
	/**
	 * W tej metodzie musimy zaimplementować rysowanie naszych bramek logicznych
	 * @param c_x
	 * @param c_y
	 */
	abstract protected void makeUI(int c_x,int c_y);
	/**
	 * W tej metodzie zaimplementujemy obliczenia które ma realizować bramka
	 */
	abstract protected void updateOutPin();
	/**
	 * Ustawienie bramki tak by miała dwa wejścia
	 */
	protected void setTwoInputGate(){
		in_pin_count=2;
		in_pins=new Pin[in_pin_count];
		addPin(new Pin(false));
		addPin(new Pin(false));
	}
	/**
	 * Ustawienie bramki tak by miała jedno wejście
	 */
	protected void setOneInputGate(){
		in_pin_count=1;
		in_pins=new Pin[in_pin_count];
		addPin(new Pin(false));
	}
	/**
	 * dodanie pinu
	 * @param p
	 */
	private void  addPin(Pin p) {
		if(pins<in_pin_count){
			in_pins[pins++]=p;
		}
	}

	public void setCenter(int x, int y) {
		makeUI(x, y);
		setUpPins();
		setTextPosCenter();
		
	}

	
	public void setTextPos(int x, int y) {
		textX=x;
		textY=y;
		
	}
	public void setTextPosCenter(){
		Paint paint = new Paint();
		paint.setTextSize(TextSize);
		float w=paint.measureText(gate_name);
		textX=(int) (center_X-w/2);
		textY=center_Y;
		
		
	}
	/**
	 * Usatwienia pinów
	 */
	private void setUpPins() {
		int width = right - left - 2 * Pin.Width;
		if (in_pin_count == 2) {
			int offset = width / (in_pin_count - 1);
			int i = 0;
			int l = left + Pin.Width;
			for (Pin p : in_pins) {
				p.setCenter(l + i, bottom);
				i += offset;
			}
		}else{
			in_pins[0].setCenter(center_X, bottom);
		}

		outPin.setCenter(center_X, top - Pin.Height);

	}
	public int getTop() {
		return top;
	}
	public int getLeft() {
		return left;
	}
	public int getBottom() {
		return bottom;
	}
	public int getRight() {
		return right;
	}
	public Pin getOutPin() {
		return outPin;
	}
	public Pin getInPin(int nr) {

		return in_pins[nr];
	}
	public int getPinCount(){
		return in_pin_count;
	}

	
	

}


	

Klasa Gate jest abstrakcyjna, czyli nie można tworzyć obiektów jej klasy.Wykorzystamy ją do dziedziczenia na klasy AndGate,OrGate itd.

Klasa AndGate

package com.adam_mistal.logicgateapp;

import android.graphics.RectF;
import android.graphics.Path.Direction;

public class AndGate extends Gate {
	static public final String[] TRUE_TABLE={"X1","X2","Y",
		                                      "0","0","0",
		                                      "0","1","0",
		                                      "1","0","0",
		                                      "1","1","1",
	                                           };
	public AndGate(){
		//usawiamy jako dwuwejsciowa
		setTwoInputGate();
		setText("AND");	
		
		
	}
	
   
	@Override
	protected void makeUI(int c_x, int c_y) {		
		int radius;
		int half_width=50;
		radius=half_width;
		int half_height=50;
		this.center_Y=c_y;
		this.center_X=c_x;
		left = center_X - half_width;
		right = center_X+ half_width;
		top = center_Y -half_height-radius;
		bottom = center_Y + half_height;	
		//rysujemy nasza bramkę
		path.addRect(new RectF(left, top+radius, right, bottom),Direction.CW);
		path.moveTo(left, top+radius);
		path.lineTo(center_X, top);
		path.lineTo(right, top+radius);
		path.addArc(new RectF(left, top, right, top+2*radius), 180f, 90f);
		path.addArc(new RectF(left, top, right, top+2*radius), 270f, 90f);	
        path.close();      		
	}

	@Override
	protected void updateOutPin() {
					  //tu własnie następuje ustawienie wyjściowego pinu na podstawie wejściowych pinów  	        
				if(in_pins[0].value && in_pins[1].value){
					outPin.setValue(true);				
				}else{
					outPin.setValue(false);					
				}
	}


}



	

Klasa NandGate

package com.adam_mistal.logicgateapp;

import android.graphics.RectF;
import android.graphics.Path.Direction;

public class NandGate extends Gate {
	static public final String[] TRUE_TABLE={"X1","X2","Y",
        "0","0","1",
        "0","1","1",
        "1","0","1",
        "1","1","0",
         };
	public NandGate(){
		setTwoInputGate();
		setText("NAND");	
		
	}
	

	@Override
	protected void makeUI(int c_x, int c_y) {		
		int radius;
		int half_width=50;
		radius=half_width;
		int radius2=radius/3;
		int half_height=50;
		this.center_Y=c_y;
		this.center_X=c_x;
		left = center_X - half_width;
		right = center_X+ half_width;
		top = center_Y -half_height-radius-2*radius2;
		bottom = center_Y + half_height;		
		path.addRect(new RectF(left, top+radius+2*radius2, right, bottom),Direction.CW);
		path.moveTo(left, top+radius+2*radius2);
		path.lineTo(center_X, top+2*radius2);
		path.lineTo(right, top+radius+2*radius2);
		path.addArc(new RectF(left, top+2*radius2, right, top+2*radius+2*radius2), 180f, 90f);
		path.addArc(new RectF(left, top+2*radius2, right, top+2*radius+2*radius2), 270f, 90f);
		path.addCircle(center_X, top+radius2, radius2,Direction.CW);
        path.close();      		
	}

	@Override
	protected void updateOutPin() {
				if(in_pins[0].value && in_pins[1].value){
					outPin.setValue(false);
				}else{
					outPin.setValue(true);
				}
	}


}

	

Klasa OrGate

package com.adam_mistal.logicgateapp;
import android.graphics.RectF;
import android.graphics.Path.Direction;

public class OrGate  extends Gate {
	static public final String[] TRUE_TABLE={"X1","X2","Y",
        "0","0","0",
        "0","1","1",
        "1","0","1",
        "1","1","1",
         };

	public OrGate() {
		setTwoInputGate();
		setText("OR");	
	}

	protected void updateOutPin() {
		if(in_pins[0].value || in_pins[1].value){
			outPin.setValue(true);
		}else{
			outPin.setValue(false);
		}
		
	}


	@Override
	protected void makeUI(int c_x, int c_y) {
		int radius;
		int half_width=50;
		radius=90;
		int half_height=50;
		this.center_Y=c_y;
		this.center_X=c_x;
		left = center_X - half_width;
		right = center_X+ half_width;
		top = center_Y -half_height-radius;
		bottom = center_Y + half_height;
		
		path.addRect(new RectF(left, top+radius, right, bottom),Direction.CW);
		path.moveTo(left, top+radius);
		path.lineTo(center_X, top);
		path.lineTo(right, top+radius);
		path.addArc(new RectF(left, top, right, top+2*radius), 180f, 90f);
		path.addArc(new RectF(left, top, right, top+2*radius), 270f, 90f);	
		path.addArc(new RectF(left, top, right, top+2*radius), 270f, 90f);
        path.close();
		
	}
	
	

}

	

Klasa XorGate

package com.adam_mistal.logicgateapp;

import android.graphics.RectF;
import android.graphics.Path.Direction;

public class XorGate extends Gate {
	static public final String[] TRUE_TABLE={"X1","X2","Y",
        "0","0","0",
        "0","1","1",
        "1","0","1",
        "1","1","0",
         };
	public XorGate(){
		setTwoInputGate();
		setText("XOR");	
		
	}
	

	@Override
	protected void makeUI(int c_x, int c_y) {		
		int radius;
		int half_width=50;
		radius=half_width;
		int half_height=50;
		this.center_Y=c_y;
		this.center_X=c_x;
		left = center_X - half_width;
		right = center_X+ half_width;
		top = center_Y -half_height-radius;
		bottom = center_Y + half_height;		
		path.addRect(new RectF(left, top+radius, right, bottom),Direction.CW);
		path.moveTo(left, top+radius);
		path.lineTo(center_X, top);
		path.lineTo(right, top+radius);
		path.addArc(new RectF(left, top, right, top+2*radius), 180f, 90f);
		path.addArc(new RectF(left, top, right, top+2*radius), 270f, 90f);	
        path.close();      		
	}

	@Override
	protected void updateOutPin() {
				if(in_pins[0].value != in_pins[1].value){
					outPin.setValue(true);
				}else{
					outPin.setValue(false);
				}
	}


}

	

Klasa Grid

package com.adam_mistal.logicgateapp;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
//klasa bedzie wyswietlac linie pionowa i poziomą
public class Grid {
	   private int width=60;
	   private int height=60;
	   private Paint paintBrushBlue = null;
	   private Paint paintBrushGreen = null;

	   public Grid(int w, int h) {
		   width=w;
		   height=h;

		   
		      paintBrushBlue = new Paint();
		      paintBrushBlue.setColor(Color.BLUE);

		      paintBrushGreen = new Paint();
		      paintBrushGreen.setColor(Color.WHITE);

		      paintBrushBlue.setAntiAlias(true);
		      paintBrushBlue.setDither(true);
		      paintBrushGreen.setAntiAlias(true);
		      paintBrushGreen.setDither(true);
		
	}

	public void draw(Canvas canvas)
	   {	
		
		canvas.drawLine(width/2, 0, width/2, height, paintBrushGreen);
		canvas.drawLine(0, height/2, width, height/2, paintBrushGreen);
	    
	   }
	}



	

LogicGateApp cz.3

Omówienie treści

W tym poście zajmiemy się stworzeniem nowych aktywności, które będą mogły być włączane przez menu główne nasze aplikacji. Tworzenie aktywności składać się będzie z:

  • utworzenia klasy dziedziczącej po Activity
  • utworzenia layoutu strony
  • dodania wpisu w AndroidManifest.xml

Na końcu postu podepniemy odpalanie aktywności do aktywności stworzonej w poprzednim poście.

Tworzymy layout naszych aktywności

chose_gate_layout.xml

Tworzymy nowy plik: chose_gate_layout.xml
Katalog pliku to: res->layout
Do katalogu drawable-hdpi wklejam zdjęcie które wykorzystam w stronie o autorze 🙂
layout
Edytujemy zawartość pliku strings.xml dodając napisy niezbędne dla przycisków.
strings2

Edytujemy zawartość pliku tak by osiągnąć zamierzony wygląd naszej aktywności.

layoutxml

Kod xml odpowiada za taki wygląd layoutu.

layout_graphic

about_author_layout.xml

Tworzymy nowy plik: about_author_layout.xml
Katalog pliku to: res->layout

aboutAuthorxml

Dodajemy napisy do strings.xml

strings3

Dodajemy style do app_styles.xml odpowiedzialne za formatowanie tekstu w layoucie.

styles2xml

Dodatkowo tworzymy plik color.xml w katalogu res->values. Plik zawiera  definicje kolorów które możemy używać w aplikacji.

color1

Wygląd:

aboutAuthorGraphic

Tworzymy klasy

ChoseGateActivity

Dodajemy do pakietu naszej aplikacji klasę ChoseGateActivity :

package com.adam_mistal.logicgateapp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class ChoseGateActivity extends Activity {
	 @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.chose_gate_layout);
	        initButtons();
	    }

	private void initButtons() {
		final Button andButton = (Button) findViewById(R.id.and_button);
		andButton.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					Log.i("ChoseGateActivity", "and");
				}
				return false;
			}
		});
		final Button orButton = (Button) findViewById(R.id.or_button);
		orButton.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					Log.i("ChoseGateActivity", "or");
				}
				return false;
			}
		});
		final Button notButton = (Button) findViewById(R.id.not_button);
		notButton.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					Log.i("ChoseGateActivity", "not");
				}
				return false;
			}
		});
		final Button nandButton = (Button) findViewById(R.id.nand_button);
		nandButton.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					Log.i("ChoseGateActivity", "nand");
				}
				return false;
			}
		});
		final Button xorButton = (Button) findViewById(R.id.xor_button);
		xorButton.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					Log.i("ChoseGateActivity", "xor");
				}
				return false;
			}
		});
		final Button returnButton = (Button) findViewById(R.id.return_button);
		returnButton.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					Log.i("ChoseGateActivity", "return");
                                        finish();
				}
				return false;
			}
		});

	}

}

Analogicznie jak w głównej aktywności w metodzie initControls() łącze kod xml z kodem java. W 81 linijce użyłem metody finish() która zgodnie z intencjią czyli po naciśnięciu przycisku “Wróć” zamknie aktywność po czym przywołana zostanie aktywność poprzednia czyli MainActivity

AboutAuthorActivity

Dodajemy do pakietu naszej aplikacji klasę AboutAuthorActivity :

package com.adam_mistal.logicgateapp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class AboutAuthorActivity extends Activity {
	 @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.about_author);
	        initButtons();
	    }

	private void initButtons() {
		final Button backButton = (Button) findViewById(R.id.return_button);
		backButton.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					Log.i("AboutAuthorActivity", "return");
					finish();
				}
				return false;
			}
		});
		
	}

}

Dodajemy wpisy w AndroidManifest.xml

LogicGateActivity

manifest1

AboutAuthorActivity

manifest2

Podpinamy włączenie aktywności do MainActivity

W klasie MainActivity edytujemy:

final Button startButton = (Button) findViewById(R.id.start_button);
		startButton.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					Log.i("MainActivity", "startButton");
					Intent intent=new Intent(getBaseContext(),ChoseGateActivity.class);
					startActivity(intent);
				}
				return false;
			}
		});

final Button authorButton = (Button) findViewById(R.id.author_button);
		authorButton.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					Log.i("MainActivity", "authorButton");
					Intent intent=new Intent(getBaseContext(),AboutAuthorActivity.class);
					startActivity(intent);
				}
				return false;
			}
		});
final Button closeButton = (Button) findViewById(R.id.close_button);
		closeButton.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					Log.i("MainActivity", "closeButton");
					finish();
				}
				return false;
			}
		});

Odpalenie nowej aktywności osiągamy poprzez wywołanie metody startActivity pobierającej jako parametr obiekt typu Intent.

Konstruktor klasy Intent pobiera jako parametry obiekt typu Context oraz klasę która ma być wykonana.

Test

Przykładowy wygląd okna LogCat

logcattest

LogicGateApp cz.1 -pierwszy projekt

Pomysł na pierwszą aplikacje – określenie funkcjonalności

W poprzednim poście uruchomiliśmy emulator android. Jesteśmy w stanie testować aplikacje. Przystępujemy do pracy 🙂 Napiszemy aplikacje która nietypowo nie będzie wyświetlać napisu hello world. Niech nasza aplikacja będzie użyteczną.
Nazwa aplikacji: LogicGateApp
Zadania aplikacji:

  • emulowanie działania bramek logicznych
  • wyświetlanie tablicy prawdy poszczególnych bramek logicznych

Pokrótce wyjaśnię że bramki logiczne to podstawowe elementy elektroniczne służące do konstrukcji układów cyfrowych. Każda bramka implementuje podstawową funkcję logiczną. Bramki logiczne  które będą implementowane w programie to:

  • AND iloczyn logiczny
  • OR suma logiczna
  • NOT negacja
  • NAND negacja iloczynu
  • XOR bramka równoważności

Skoro mam już określone wymagania zaprojektuje wygląd poszczególnych okien  aplikacji.

activities_plan

Wiemy już jak aplikacja będzie wyglądać od strony użytkownika, wiemy też jakie funkcje ma spełniać. Przystąpmy do kodowania.

Projekt w eclipse – pierwsze Activity

Tworzymy nowy projekt w eclipse: File->New->Android Aplication Project następnie w opcjach dodawania nowego projektu wypełniamy odpowiednie pola jak na rysunku poniżej.

new_android_project

Po przejściu dalej możemy wybrać ikonę dla naszego projektu. Można wybrać z pliku lub też z przygotowanych w eclipse clipartów. Do celów tego programu wybiorę clipart.

luncher_and

Koleje okna wyboru dotyczą rodzaju tworzonego Activity czyli odpowiednika okna w systemie Windows.

create_activity1

createActivity2

Klikamy Finish .Udało się nam stworzyć pierwszy projekt w eclipse. Już na tym etapie jesteśmy w stanie uruchomić naszą aplikację. Jedyną rzeczą którą nam się ukaże będzie napis Hello World. Włączmy nasz emulator który utworzyliśmy w poprzednim poście. Następnie musiby zbudować nasz projekt możemy to zrobić klikając Project->Build All (CTR+B). Klikamy PPM na węzeł naszego pakietu i wybieramy polecenie Run As-> Android Aplication

LogicGateApp1

Po kilku chwilach aplikacja instaluje się na emulatorze który włączyliśmy uprzednio. Mamy pewność że wszystko poszło tak jak trzeba.

firstActivity

W następnym wpisie opisze strukturę projektu w środowisku eclipse.

Instalacja narzędzi

Programowanie aplikacji na platformę Android, które będzie głównym tematem bloga  Rozpoczniemy od pobrania wymaganych narzędzi. Nasz warsztat będzie wymagał pobrania oraz skonfigurowania:

  1. Java JDK dla naszych celów wersja 6.0
  2. Eclipse
  3. Android SDK być może paczka SDK zawierać już Eclipse

Instalacja środowiska Java nie powinna nastręczać trudności. Po pobraniu oprogramowania przystępujemy do konfiguracji. Android SDK wypakujmy np. do katalogu C:\Program Files\android-sdk
Musimy skonfigurować Eclipse go do pracy z platformą Android. W tym celu musimy pobrać plug-in ADT- Android Development Toolkit. W Eclipse: Help->Install New Software…
adt

Następnie przycisk Add. W nowym oknie wypełniamy pola jak poniżej:

adt2

Zaznaczamy pokazane komponenty.

adt3

Kolejną czynnością jest wskazanie Eclipse ścieżki do Android SDK: Window->Preferences

sdk1

Po określeniu ścieżki do katalogu w którym znajduje się pobrane SDK Androida przystąpimy do pobrania odpowiednich pakietów zawierających potrzebne nam wersje SDK androida bądź tez aktualizacje bieżących: Window->Android SDK Manager

sdk2

Wybieramy interesujące nas wersje SDK oraz dodatków następnie instalujemy .

W tym momencie mamy już skonfigurowane niezbędne narzędzia. Możemy rozpocząć przygodę z Androidem.