CS175
Chris Pollett
Nov 19, 1014
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *messageLabel; @property (weak, nonatomic) IBOutlet UILabel *tapsLabel; @property (weak, nonatomic) IBOutlet UILabel *touchesLabel; -(void)updateLabelsFromTouches: (NSSet *)touches; @end
#import "TouchAppViewController.h" @implementation TouchAppViewController -(void) updateLabelsFromTouches:(NSSet *)touches { NSUInteger numTaps = [[touches anyObject] tapCount]; NSString *tapsMessage = [[NSString alloc] initWithFormat:@"%lu taps detected", (unsigned long)numTaps]; _tapsLabel.text = tapsMessage; NSUInteger numTouches = [touches count]; NSString *touchMsg = [[NSString alloc] initWithFormat: @"%lu touches detected", (unsigned long)numTouches]; _touchesLabel.text = touchMsg; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { _messageLabel.text = @"Touches Began"; [self updateLabelsFromTouches:touches]; } - (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { _messageLabel.text = @"Touches Cancelled"; [self updateLabelsFromTouches:touches]; } - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { _messageLabel.text = @"Touches Ended"; [self updateLabelsFromTouches:touches]; } - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { _messageLabel.text = @"Drag detected"; [self updateLabelsFromTouches:touches]; } @end
case 2: //double tap [NSObject cancelPreviousPerformRequestWithTarget:self selector:@selector(singleTap) object:nil]; [self performSelector:@selector(doubleTap) withObject:nil afterDelay: .4] break; case 3: //triple tap [NSObject cancelPreviousPerformRequestWithTarget:self selector:@selector(doubleTap) object:nil]; [self performSelector:@selector(tripleTap) withObject:nil afterDelay: .4] break;in touchesBegan.
#import <UIKit/UIKit.h> #define kMinimumGestureLength 25 #define kMaximumVariance 5 @interface SwipeAppViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *label; @property CGPoint gestureStartPoint; -(void)eraseText; @end
#import "SwipeAppViewController.h" @implementation SwipeAppViewController -(void)eraseText { _label.text = @""; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } #pragma mark - -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; gestureStartPoint = [touch locationInView:self.view]; } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint currentPosition = [touch locationInView:self.view]; CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x); CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y); if(deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) { _label.text = @"Horizontal swipe detected"; [self performSelector:@selector(eraseText) withObject:nil afterDelay: 2]; } else if(deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) { _label.text = @"Vertical swipe detected"; [self performSelector:@selector(eraseText) withObject:nil afterDelay: 2]; } } @end
boolean onKey(View v, int keyCode, KeyEvent event)the key code tells you what key was pressed.
boolean onTouch(View v, MotionEvent event)
<canvas id="drawpad" /> <style type="text/css"> #drawpad { width: 200px; height: 100px; background-color: light-gray; } </style>
<script type="text/javascript"*gt; drawpad_object = document.getElementById("drawpad"); drawpad_context = drawpad_object.getContext("2d"); drawpad_context.strokeStyle = "blue"; drawpad_context.strokeRect(10,10,50,50); drawpad_context.fillStyle = "green"; drawpad_context.font = "32pt Arial"; max_width = 100; drawpad_context.rotate(3.14/3); //pi/3 drawpad_context.fillText("hello", 80, -10, max_width); </script>
<canvas id="drawpad" ontouchstart="startHandler(event);" ontouchmove="moveHandler(event);" ontouchend="endHandler(event);" ontouchcancel="cancelHandler(event);" />
drawpad_object.addEventListener("touchstart", startHandler2, false);
<video id="myvideo" width="320" height="240" controls="controls"> <source src="some_movie.mp4" type="video/mp4" /> <!-- firefox does not support mp4--> <source src="some_movie.ogg" type="video/ogg" /> <!-- firefox does support ogg but others don't--> Your browser does not support the video tag. </video>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Test Media Player" /> <Button android:id="@+id/playwarworlds" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Play Orson Welles" /> </LinearLayout>
import android.view.View; import android.view.View.OnClickListener; import android.media.MediaPlayer; import android.media.MediaPlayer.*; public class AudioTestActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); View button = findViewById(R.id.playwarworlds); button.setOnClickListener( new OnClickListener() { public void onClick(View v) { MediaPlayer player = MediaPlayer.create(AudioTestActivity.this, R.raw.warworlds); player.start(); player.setOnCompletionListener(new OnCompletionListener() { public void onCompletion(MediaPlayer m) { } }); } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <VideoView android:id="@+id/video" android:layout_width="320px" android:layout_height="240px" /> <Button android:id="@+id/play" android:text="Play Fred Ott" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
package org.pollett; import android.app.Activity; import android.graphics.PixelFormat; import android.os.Bundle; import android.view.View; import android.widget.MediaController; import android.widget.VideoView; public class VideoTestActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFormat(PixelFormat.TRANSLUCENT); setContentView(R.layout.main); video = (VideoView)findViewById(R.id.video); video.setVideoPath("/mnt/sdcard/fred_ott.mp4"); mc = new MediaController(this); mc.setMediaPlayer(video); video.setMediaController(mc); video.requestFocus(); View button = findViewById(R.id.play); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { VideoTestActivity.this.mc.show(); } } ); } VideoView video; MediaController mc; }
<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" />
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <SurfaceView android:id="@+id/surface" android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="fill_parent"> </SurfaceView> </LinearLayout>
package org.pollett; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.content.ContentValues; import android.content.Intent; import android.graphics.PixelFormat; import android.hardware.Camera; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore.MediaColumns; import android.provider.MediaStore.Images.ImageColumns; import android.provider.MediaStore.Images.Media; import android.view.KeyEvent; import android.view.MenuItem; import android.view.SurfaceHolder; import android.view.SurfaceView; public class CameraTest extends Activity implements SurfaceHolder.Callback { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFormat(PixelFormat.TRANSLUCENT); setContentView(R.layout.main); /* the surface holder is used to manage what is actually displayed on the surface */ surfaceView = (SurfaceView)findViewById(R.id.surface); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } /** Adds menu to view the photos that have already been taken. */ public boolean onCreateOptionsMenu(android.view.Menu menu) { MenuItem item = menu.add(0, 0, 0, "View Photos?"); item.setOnMenuItemClickListener( new MenuItem.OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { Intent intent = new Intent(Intent.ACTION_VIEW, CameraTest.this.targetResource); startActivity(intent); return true; } } ); return true; } /** Pushing the center button takes a picture, and sets up the callback for saving it. */ public boolean onKeyDown(int keyCode, KeyEvent event) { ImageCaptureCallback camDemo = null; if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { try { String filename= this.timeStampFormat.format(new Date()); ContentValues values = new ContentValues(); values.put(MediaColumns.TITLE, filename); values.put(ImageColumns.DESCRIPTION, "Image from emulator"); Uri uri = getContentResolver().insert( Media.EXTERNAL_CONTENT_URI, values); camDemo = new ImageCaptureCallback( getContentResolver().openOutputStream(uri) ); } catch (Exception e) { } } if(keyCode == KeyEvent.KEYCODE_BACK) { return super.onKeyDown(keyCode, event); } if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { this.camera.takePicture(this.mShutterCallback, this.mPictureCallbackRaw, camDemo); return true; } return false; } /** Start the camera after the surface is created */ public void surfaceCreated(SurfaceHolder holder) { this.camera = Camera.open(); } /** Actually, display the camera data in the surface */ public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { if(this.isPreviewRunning) { this.camera.stopPreview(); } Camera.Parameters p = this.camera.getParameters(); p.setPreviewSize(w, h); try { this.camera.setPreviewDisplay(holder); this.camera.startPreview(); this.isPreviewRunning = true; } catch (Exception e) { } } /** Shut things down when surface goes away */ public void surfaceDestroyed(SurfaceHolder holder) { this.camera.stopPreview(); this.isPreviewRunning = false; this.camera.release(); } //Handles raw data when picture taken Camera.PictureCallback mPictureCallbackRaw = new Camera.PictureCallback() { public void onPictureTaken(byte[] data, Camera c) { CameraTest.this.camera.startPreview(); } }; Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() { public void onShutter() { //could be used to play a sound } }; Camera camera; boolean isPreviewRunning = false; SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS"); SurfaceView surfaceView; SurfaceHolder surfaceHolder; Uri targetResource = Media.EXTERNAL_CONTENT_URI; }
package org.pollett; import java.io.OutputStream; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; public class ImageCaptureCallback implements PictureCallback { public ImageCaptureCallback(OutputStream fileoutputStream) { this.fileoutputStream = fileoutputStream; } public void onPictureTaken(byte[] data, Camera camera) { try { this.fileoutputStream.write(data); this.fileoutputStream.flush(); this.fileoutputStream.close(); } catch(Exception e) { e.printStackTrace(); } } OutputStream fileoutputStream; }