Processing 2. Креативное программирование
.pdfAndroid
, - USBUSB ., Android SDK . Processing .Run on Device Sketch, Processing.
Android , Android. , , , DPI . Android..
Junction, , The League of Moveable Type. http:// www.theleagueofmoveabletype.com/ junction.
Junction.otf Processing, .
Android DisplayMetrics, String,:
import android.util.DisplayMetrics;
String density;
String dpi;
String w;
String h;
PFont junction;
230
10
setup() DisplayMetricsString. Junction 32:
void setup()
{
size( displayWidth, displayHeight ); smooth();
DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics( metrics );
density = "Density: " + metrics.density; dpi = "DPI: " + metrics.densityDpi;
w = "Width: " + width; h = "height: " + height;
junction = createFont( "Junction.otf", 32 );
textFont( junction, 32 );
println( PFont.list() );
}
draw() String :
void draw()
{
background( 255, 225, 23 ); fill( 0 );
textAlign( CENTER );
text( density, width/2, 300 ); text( dpi, width/2, 360 ); text( w, width/2, 420 ); text( h, width/2, 480 );
}
231
Android
, HTC desire , ., .
DisplayMetrics Android,. ,
DisplayMetrics :
DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics( metrics );
.
metrics.density;
metrics.densityDpi;
DisplayMetricshttp://developer.android.com/reference/android/util/ DisplayMetrics.html.
., , ., , .
232
10
setup() . -- , - .
boolean touching = false;
void setup()
{
size( displayWidth, displayHeight ); smooth();
background( 0 );
}
surfaceTouchevent(). , touching true:
public boolean surfaceTouchEvent( MotionEvent event )
{
if ( event.getAction() == 2 ) { touching = true;
}else {
touching = false;
}
return super.surfaceTouchEvent( event );
}
draw() mouseX, mouseY, pmouseX pmouseY , :
void draw()
{
if ( touching ) { stroke( 255, 128 ); noFill();
float d = dist( mouseX, mouseY, pmouseX, pmouseY ); float s = map( d, 0, 200, 1, 10 );
strokeWeight( s );
line( mouseX, mouseY, pmouseX, pmouseY ); fill( 255, 255, 0, 16 );
stroke( 0, 128 ); strokeWeight( 1 );
ellipse( mouseX, mouseY, motionPressure * 100, motionPressure * 100 );
}
}
233
Android
, ., :
Android , Standard.. .surfaceTouchEvent():
public boolean surfaceTouchEvent( MotionEvent event ) {}
MotionEvent. getAction() , . 2, :
if ( event.getAction() == 2 ) { touching = true;
}else {
touching = false;
}
234
10
, , ,super.surfaceTouchEvent():
return super.surfaceTouchEvent( event );
draw() motionPressure..
. Android. ,. surfaceTouchEvent()event.getPointerCount()., x y. :
public boolean surfaceTouchEvent( MotionEvent event )
{
int numTouches = event.getPointerCount();
for ( int i = 0; i < numTouches; i++ ) { int touchID = event.getPointerId( i ); float x = event.getX( i );
float y = event.getY( i ); float r = event.getSize( i );
}
return super.surfaceTouchEvent( event );
}
Android . -. ,, ..
accelerometer.pde. File | Examples, Sensors ., AccelerometerManager.java.. .
235
Android
setup():
AccelerometerManager acc; float ax;
float ay; float az; float r;
PVector loc; color bgcolor;
void setup()
{
acc = new AccelerometerManager( this );
size( displayWidth, displayHeight ); orientation( PORTRAIT );
smooth();
loc = new PVector( width/2, height/2 );
r = 40;
bgcolor = color( 255, 0, 0 );
}
accelerationEvent().. redraw() :
public void accelerationEvent( float x, float y, float z ) { ax = x;
ay = y; az = z; redraw();
}
236
10
draw() ax ay . , .
void draw()
{
float speedX = -(ax / 2); float speedY = ay / 2;
loc.x += speedX; loc.y += speedY;
if ( loc.x < r ) {
bgcolor = color( 255, 128, 0, 32 );
}
f ( loc.x > width - r ) {
bgcolor = color( 128, 255, 0, 32 );
}
if ( loc.y < r ) {
bgcolor = color( 0, 225, 255, 32 );
}
if ( loc.y > height - r ) {
bgcolor = color( 255, 0, 128, 32 );
}
loc.x = constrain( loc.x, r, width - r ); loc.y = constrain( loc.y, r, height - r );
fill( bgcolor );
rect( 0, 0, width, height );
fill( 255 ); noStroke();
ellipse( loc.x, loc.y, r*2, r*2 );
}
237
Android
. ,. ,:
- AccelerometerManager, Processing.accelerationEvent() -ax, ay az.
draw() .
ax, ,. ,, . -ax ay.
238
10
Android,Ketai. , , . Google Code: https://code.google.com/p/ketai/.
GPS
GPS. ,. .
Junction - The League of Moveable Type. : http://www.theleagueofmoveabletype.com/ junction. , Processing.
,:
import android.content.Context; import android.location.*; import android.os.Bundle;
LocationManager manager;
GPSLocationListener gps;
float latitude; float longitude; float accuracy; String provider;
PFont junction;
239