I want to capture images using the front facing camera "flash" trick similar to Fornt Flash.
I know that it might involves another layer on top of the camera preview
as suggested here and here.
Can anyone post a working solution that includes the camera?
Thanks.
Found out by myself (and some little help from a friend).
Basically you need to create a flash emulator View that has a white background with alpha of 0.7 and visibility gone.
when clicked you need to:
show the view - View.VISIBLE
flashEmulator.setVisibility(View.VISIBLE);
adjust the screenBrightness to the max value
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
lastScreenBrightness = params.screenBrightness;
params.screenBrightness = 1F;
window.setAttributes(params);
convert the background of the flash emulator to HSV
int color = Color.WHITE;
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= 1f;
color = Color.HSVToColor(hsv);
flashEmulator.setBackgroundColor(color);
Complete Code for android front flash app
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.frontflash.android">
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
<uses-feature android:name="android.hardware.camera.flash" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#android:color/black"
tools:context=".MainActivity">
<com.frontflash.android.CameraPreview
android:id="#+id/cameraPreview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:visibility="gone"
android:alpha="0.7"
android:id="#+id/flashEmulator"
android:background="#android:color/white"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="On"
android:id="#+id/button"
android:layout_centerVertical="false"
android:layout_centerHorizontal="true" />
MainActivity.java
package com.frontflash.android;
import android.app.Activity;
import android.graphics.Color;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
public class MainActivity extends Activity {
private Button button;
private Camera camera;
private boolean isFlashOn;
private FrameLayout flashEmulator;
private float lastScreenBrightness;
private CameraPreview cameraPreview;
#Override
protected void onCreate(Bundle savedInstanceState) {
int fullScreen = WindowManager.LayoutParams.FLAG_FULLSCREEN;
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(fullScreen, fullScreen);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
flashEmulator = (FrameLayout) findViewById(R.id.flashEmulator);
cameraPreview = (CameraPreview)findViewById(R.id.cameraPreview);
startCamera();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isFlashOn) {
turnOffFlash();
button.setText("On");
} else {
turnOnFlash();
button.setText("Off");
}
}
});
}
private void startCamera() {
if (camera == null) {
try {
camera = Camera.open(1);
Camera.Parameters params = camera.getParameters();
params.setPreviewSize(1280, 720);
camera.setDisplayOrientation(90);
camera.setParameters(params);
cameraPreview.connectCamera(camera);
cameraPreview.startPreview();
} catch (Exception e) {
}
}
}
private void turnOnFlash() {
if (!isFlashOn) {
int color = Color.WHITE;
float[] hsv = new float[3];
final Window window = getWindow();
final WindowManager.LayoutParams params = window.getAttributes();
lastScreenBrightness = params.screenBrightness;
params.screenBrightness = 1F;
window.setAttributes(params);
flashEmulator.setVisibility(View.VISIBLE);
Color.colorToHSV(color, hsv);
hsv[2] *= 1f;
color = Color.HSVToColor(hsv);
flashEmulator.setBackgroundColor(color);
isFlashOn = true;
}
}
private void turnOffFlash() {
if (isFlashOn) {
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.screenBrightness = lastScreenBrightness;
window.setAttributes(params);
flashEmulator.setVisibility(View.GONE);
isFlashOn = false;
}
}
#Override
protected void onDestroy() {
super.onDestroy();
}
#Override
protected void onPause() {
super.onPause();
turnOffFlash();
}
#Override
protected void onRestart() {
super.onRestart();
}
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onStart() {
super.onStart();
startCamera();
}
#Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
camera = null;
}
}
}
CameraPreview.java
package com.frontflash.android;
import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/**
* Created by sagi on 2/8/2017.
*/
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private Camera camera;
private SurfaceHolder surfaceHolder;
private final String LOG_TAG = "CameraPreview";
public CameraPreview(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public CameraPreview(Context context) {
super(context);
}
public void connectCamera(Camera camera) {
this.camera = camera;
surfaceHolder = getHolder();
surfaceHolder.addCallback(this);
startPreview();
}
public void releaseCamera() {
if (camera != null) {
stopPreview();
camera = null;
}
}
void startPreview() {
if (camera != null && surfaceHolder.getSurface() != null) {
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
Log.e(LOG_TAG, String.format("Error setting preview display: %s", e.getMessage()));
}
}
}
void stopPreview() {
if (camera != null) {
try {
camera.stopPreview();
} catch (Exception e) {
Log.e(LOG_TAG, String.format("Error stopping preview: %s", e.getMessage()));
}
}
}
public void surfaceCreated(SurfaceHolder surfaceHolder) {
startPreview();
}
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
stopPreview();
startPreview();
}
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
stopPreview();
}
}
Related
This is the code i used to have camera preview in my app, but it shows a java runtime error but not specifies at which the line the error is at. I found this code on https://github.com/commonsguy/cw-advandroid/tree/master/Camera/Preview/,which was given as a solution for one of the questions regarding camera preview, but it shows a blank screen and the app terminates. Any help will be appreciated.
package com.example.smartmeasure;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
public class CameraPreview extends Activity {
private SurfaceView preview=null;
private SurfaceHolder previewHolder=null;
private Camera camera=null;
private boolean inPreview=false;
private boolean cameraConfigured=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_preview);
preview=(SurfaceView)findViewById(R.id.camera_preview);
previewHolder=preview.getHolder();
previewHolder.addCallback(surfaceCallback);
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public void onResume() {
super.onResume();
camera=Camera.open();
startPreview();
}
#Override
public void onPause() {
if (inPreview) {
camera.stopPreview();
}
camera.release();
camera=null;
inPreview=false;
super.onPause();
}
private Camera.Size getBestPreviewSize(int width, int height,
Camera.Parameters parameters) {
Camera.Size result=null;
for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
if (size.width<=width && size.height<=height) {
if (result==null) {
result=size;
}
else {
int resultArea=result.width*result.height;
int newArea=size.width*size.height;
if (newArea>resultArea) {
result=size;
}
}
}
}
return(result);
}
private void initPreview(int width, int height) {
if (camera!=null && previewHolder.getSurface()!=null) {
try {
camera.setPreviewDisplay(previewHolder);
}
catch (Throwable t) {
Log.e("PreviewDemo-surfaceCallback",
"Exception in setPreviewDisplay()", t);
Toast
.makeText(CameraPreview.this, t.getMessage(), Toast.LENGTH_LONG)
.show();
}
if (!cameraConfigured) {
Camera.Parameters parameters=camera.getParameters();
Camera.Size size=getBestPreviewSize(width, height,
parameters);
if (size!=null) {
parameters.setPreviewSize(size.width, size.height);
camera.setParameters(parameters);
cameraConfigured=true;
}
}
}
}
private void startPreview() {
if (cameraConfigured && camera!=null) {
camera.startPreview();
inPreview=true;
}
}
SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {
// no-op -- wait until surfaceChanged()
}
public void surfaceChanged(SurfaceHolder holder,
int format, int width,
int height) {
initPreview(width, height);
startPreview();
}
public void surfaceDestroyed(SurfaceHolder holder) {
// no-op
}
};
}
This is the Error:
05-27 10:34:37.812: E/AndroidRuntime(2559): FATAL EXCEPTION: main 05-27 10:34:37.812: E/AndroidRuntime(2559): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.smartmeasure/com.example.smartmeasure.CameraPreview}: java.lang.ClassCastException: android.widget.FrameLayout 05-27 10:34:37.812: E/AndroidRuntime(2559): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
This is the xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="241dp" />
</LinearLayout>
camera_preview is FrameLayout and you are casting it into SurfaceView thats the error ..cast it into FrameLayout
You are casting FrameLayout to SurfaceView in your Activity onCreate, put SurfaceView instead of FrameLayout in XML and then name its id to camera_preview and initialize in Activity.
Your XML code will look like this after editing:
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<SurfaceView
android:id="#+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="241dp"/>
</LinearLayout>
Just to add on to the suggestions above, we are supposed to camera user permissions in AndroidManifest.xml file to access the camera. The permission lines are:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
I am implementing a AR app demo and it can work. but i found that this app launch very slow, i did some debugging and found that it may be caused by "onSensorChanged" which runed many times.
Does anyone can help me out? thanks in advance!
here is the code!
package com.example.mazhi_000.cameraapplication;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.view.Menu;
import android.view.MenuItem;
import android.hardware.Camera;
import android.util.Log;
import android.widget.TextView;
public class CameraActivity extends Activity {
SurfaceView cameraPreview;
SurfaceHolder previewHolder;
Camera camera;
boolean inPreview;
final static String TAG = "CameraSurfaceView";
SensorManager sensorManager;
int orientationSensor;
float headingAngle;
float pitchAngle;
float rollAngle;
int accelerometrSensor;
float xAxis;
float yAxis;
float zAxis;
LocationManager locationManager;
double latitude;
double longitude;
double altitude;
TextView xAxisValue;
TextView yAxisValue;
TextView zAxisValue;
TextView headingValue;
TextView pitchValue;
TextView rollValue;
TextView altitudeValue;
TextView latitudeValue;
TextView longitudeValue;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
// cache textview
xAxisValue = (TextView) findViewById(R.id.xAxisValue);
yAxisValue = (TextView) findViewById(R.id.yAxisValue);
zAxisValue = (TextView) findViewById(R.id.zAxisValue);
headingValue = (TextView) findViewById(R.id.headingValue);
pitchValue = (TextView) findViewById(R.id.pitchValue);
rollValue = (TextView) findViewById(R.id.rollValue);
altitudeValue = (TextView) findViewById(R.id.altitudeValue);
longitudeValue = (TextView) findViewById(R.id.longitudeValue);
latitudeValue = (TextView) findViewById(R.id.latitudeValue);
locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
altitude = location.getAltitude();
Log.d(TAG, "Latitude: " + String.valueOf(latitude));
Log.d(TAG, "longitude: " + String.valueOf(longitude));
Log.d(TAG, "Altitude: " + String.valueOf(altitude));
latitudeValue.setText(String.valueOf(latitude));
longitudeValue.setText(String.valueOf(longitude));
altitudeValue.setText(String.valueOf(altitude));
}
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 2000, 2, locationListener);
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
orientationSensor = Sensor.TYPE_ORIENTATION;
accelerometrSensor = Sensor.TYPE_ACCELEROMETER;
sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(orientationSensor), SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(accelerometrSensor),SensorManager.SENSOR_DELAY_NORMAL);
inPreview = false;
cameraPreview = (SurfaceView)findViewById(R.id.cameraPreview);
previewHolder = cameraPreview.getHolder();
previewHolder.addCallback(surfaceCallback);
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
LocationListener locationListener = new LocationListener()
{
#Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
altitude = location.getAltitude();
Log.d(TAG, "Latitude: " + String.valueOf(latitude));
Log.d(TAG, "longitude: " + String.valueOf(longitude));
Log.d(TAG, "Altitude: " + String.valueOf(altitude));
latitudeValue.setText(String.valueOf(latitude));
longitudeValue.setText(String.valueOf(longitude));
altitudeValue.setText(String.valueOf(altitude));
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
};
final SensorEventListener sensorEventListener = new SensorEventListener()
{
#Override
public void onSensorChanged(SensorEvent sensorEvent)
{
if(sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
headingAngle = sensorEvent.values[0];
pitchAngle = sensorEvent.values[1];
rollAngle = sensorEvent.values[2];
Log.d(TAG, "headingAngle: " + String.valueOf(headingAngle));
Log.d(TAG, "pitchAngle: " + String.valueOf(pitchAngle));
Log.d(TAG, "rollAngle: " + String.valueOf(rollAngle));
headingValue.setText(String.valueOf(headingAngle));
pitchValue.setText(String.valueOf(pitchAngle));
rollValue.setText(String.valueOf(rollAngle));
}
else
{
if(sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
{
xAxis = sensorEvent.values[0];
yAxis = sensorEvent.values[1];
zAxis = sensorEvent.values[2];
Log.d(TAG, "xAxis: " + String.valueOf(xAxis));
Log.d(TAG, "yAxis: " + String.valueOf(yAxis));
Log.d(TAG, "zAxis: " + String.valueOf(zAxis));
xAxisValue.setText(String.valueOf(xAxis));
yAxisValue.setText(String.valueOf(yAxis));
zAxisValue.setText(String.valueOf(zAxis));
}
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int i)
{
// not used
}
};
SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback()
{
public void surfaceCreated(SurfaceHolder holder)
{
try
{
camera.setPreviewDisplay(previewHolder);
}
catch (Throwable t)
{
Log.e("ProAndroidAR2Activity", "Exception in setPreviewDisplay()", t);
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = getBestPreviewSize(width, height, parameters);
if(size != null)
{
parameters.setPreviewSize(size.width, size.height);
camera.setParameters(parameters);
camera.startPreview();
inPreview = true;
}
}
public void surfaceDestroyed(SurfaceHolder holder)
{
// not userd
// camera.stopPreview();
// camera.release();
// camera = null;
}
};
private Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters)
{
Camera.Size result=null;
//Camera.Parameters p = parameters;
for (Camera.Size size : parameters.getSupportedPreviewSizes())
{
if (size.width <= width && size.height <= height)
{
if (result==null)
{
result=size;
}
else
{
int resultArea = result.width * result.height;
int newArea = size.width * size.height;
if (newArea>resultArea) {
result=size;
}
}
}
}
return result;
}
#Override
public void onResume()
{
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 2, locationListener);
sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(orientationSensor), SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(accelerometrSensor), SensorManager.SENSOR_DELAY_NORMAL);
camera = Camera.open();
}
#Override
public void onPause()
{
if(inPreview)
{
camera.stopPreview();
}
locationManager.removeUpdates(locationListener);
sensorManager.unregisterListener(sensorEventListener);
camera.release();
camera = null;
inPreview = false;
super.onPause();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.camera, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SurfaceView
android:id="#+id/cameraPreview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="#+id/xAxisLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="15dp"
android:text="#string/xAxis" />
<TextView
android:id="#+id/yAxisLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/xAxisLabel"
android:layout_below="#+id/xAxisLabel"
android:text="#string/yAxis" />
<TextView
android:id="#+id/zAxisLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/yAxisLabel"
android:layout_below="#+id/yAxisLabel"
android:text="#string/zAxis" />
<TextView
android:id="#+id/headingLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/zAxisLabel"
android:layout_below="#+id/zAxisLabel"
android:layout_marginTop="19dp"
android:text="#string/heading" />
<TextView
android:id="#+id/pitchLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/headingLabel"
android:layout_below="#+id/headingLabel"
android:text="#string/pitch" />
<TextView
android:id="#+id/rollLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/pitchLabel"
android:layout_below="#+id/pitchLabel"
android:text="#string/roll" />
<TextView
android:id="#+id/latitudeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/rollLabel"
android:layout_below="#+id/rollLabel"
android:layout_marginTop="34dp"
android:text="#string/latitude" />
<TextView
android:id="#+id/longitudeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/latitudeLabel"
android:layout_below="#+id/latitudeLabel"
android:text="#string/longitude" />
<TextView
android:id="#+id/altitudeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/longitudeLabel"
android:layout_below="#+id/longitudeLabel"
android:text="#string/altitude" />
<TextView
android:id="#+id/xAxisValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/xAxisLabel"
android:layout_marginLeft="56dp"
android:layout_toRightOf="#+id/longitudeLabel"
android:text="#string/empty" />
<TextView
android:id="#+id/yAxisValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/yAxisLabel"
android:layout_alignBottom="#+id/yAxisLabel"
android:layout_alignLeft="#+id/xAxisValue"
android:text="#string/empty" />
<TextView
android:id="#+id/zAxisValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/headingLabel"
android:layout_alignLeft="#+id/yAxisValue"
android:text="#string/empty" />
<TextView
android:id="#+id/headingValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/headingLabel"
android:layout_alignBottom="#+id/headingLabel"
android:layout_alignLeft="#+id/zAxisValue"
android:text="#string/empty" />
<TextView
android:id="#+id/pitchValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/pitchLabel"
android:layout_alignBottom="#+id/pitchLabel"
android:layout_alignLeft="#+id/headingValue"
android:text="#string/empty" />
<TextView
android:id="#+id/rollValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/latitudeLabel"
android:layout_alignLeft="#+id/pitchValue"
android:text="#string/empty" />
<TextView
android:id="#+id/latitudeValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/latitudeLabel"
android:layout_alignLeft="#+id/rollValue"
android:text="#string/empty" />
<TextView
android:id="#+id/longitudeValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/longitudeLabel"
android:layout_alignBottom="#+id/longitudeLabel"
android:layout_alignLeft="#+id/latitudeValue"
android:text="#string/empty" />
<TextView
android:id="#+id/altitudeValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/altitudeLabel"
android:layout_alignBottom="#+id/altitudeLabel"
android:layout_alignLeft="#+id/longitudeValue"
android:text="#string/empty" />
</RelativeLayout>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mazhi_000.cameraapplication" >
<uses-sdk android:minSdkVersion="7"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".CameraActivity"
android:screenOrientation = "landscape"
android:configChanges="keyboardHidden|orientation"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-feature android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>
Your code looks OK except you do not need
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 2, locationListener);
sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(orientationSensor), SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(accelerometrSensor), SensorManager.SENSOR_DELAY_NORMAL);
in onCreate, because you have it onResume.
Also onSensorChanged is supposed to run many times:) try to set some fillers...
I'm following the Android Game Programming for Dummies by: Derek James. It looks outdated too me because in the book it doesn't include the fragment_main. So I just copied the fragment_main and replaced it in the activity_main and deleted the fragment_main (I followed the how to get rid of fragment_main tutorial on this forum).
I'm trying to make the whack a mole game in the book.
Anyways I get two errors when I followed the book and it said I can run the program now but I am unable to do that because of:
background cannot be resolved or is not a field
title cannot be resolved or is not a field
I have everything the same as the book but why do I get these errors, I checked all over Google to find an answer or something similar but I can't find the error, I would really appreciate if someone can help me out. Sorry for writing a whole paragraph but I should let you know what I did.
This is my WhackAMoleView.java
package com.whackamole;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class WhackAMoleView extends SurfaceView implements
SurfaceHolder.Callback {
private Context myContext;
private SurfaceHolder mySurfaceHolder;
private Bitmap backgroundImg;
private int screenW = 1;
private int screenH = 1;
private boolean running = false;
private boolean onTitle = true;
private WhackAMoleThread thread;
public WhackAMoleView(Context context, AttributeSet attrs) {
super(context, attrs);
SurfaceHolder holder = getHolder();
holder.addCallback(this);
thread = new WhackAMoleThread(holder, context, new Handler()
{
#Override
public void handleMessage(Message m) {
}
});
setFocusable(true);
}
public WhackAMoleThread getThread() {
return thread;
}
class WhackAMoleThread extends Thread {
public WhackAMoleThread(SurfaceHolder surfaceHolder, Context context,
Handler handler) {
mySurfaceHolder = surfaceHolder;
myContext = context;
backgroundImg = BitmapFactory.decodeResource(
context.getResources(), R.drawable.title);
}
#Override
public void run() {
while (running) {
Canvas c = null;
try {
c = mySurfaceHolder.lockCanvas(null);
synchronized (mySurfaceHolder) {
draw(c);
}
} finally {
if (c != null) {
mySurfaceHolderunlockCanvasAndPost(c);
}
}
}
}
private void mySurfaceHolderunlockCanvasAndPost(Canvas c) {
// TODO Auto-generated method stub
}
private void draw(Canvas canvas) {
try {
canvas.drawBitmap(backgroundImg, 0, 0, null);
} catch (Exception e) {
}
}
boolean doTouchEvent(MotionEvent event) {
synchronized (mySurfaceHolder) {
int eventaction = event.getAction();
int x = (int) event.getX();
int Y = (int) event.getY();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
if (onTitle) {
backgroundImg = BitmapFactory
.decodeResource(myContext.getResources(),
R.drawable.background);
backgroundImg = Bitmap.createScaledBitmap(
backgroundImg, screenW, screenH, true);
onTitle = false;
}
break;
}
}
return true;
}
public void setSurfaceSize(int width, int height) {
synchronized (mySurfaceHolder) {
screenW = width;
screenH = height;
backgroundImg = Bitmap.createScaledBitmap(backgroundImg, width,
height, true);
}
}
public void setRunning(boolean b) {
running = b;
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
return thread.doTouchEvent(event);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
thread.setSurfaceSize(width, height);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
thread.setRunning(true);
if (thread.getState() == Thread.State.NEW) {
thread.start();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
thread.setRunning(false);
}
}
This is my MainActivity.java
package com.whackamole;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
private WhackAMoleView myWhackAMoleView;
/**Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags
(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.whackamole_layout);
myWhackAMoleView = (WhackAMoleView)
findViewById(R.id.mole);
myWhackAMoleView.setKeepScreenOn(true);
}
}
This is my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.whackamole"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<activity
android:name="com.whackamole.MainActivity"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
mySurfaceHolderunlockCanvasAndPost(c);
}
}
}
}
mySurfaceHolderunlockCanvasAndPost should be mySurfaceHolder.unlockCanvasAndPost
*private void mySurfaceHolderunlockCanvasAndPost(Canvas c) {
// TODO Auto-generated method stub
}* should not be here
I have researched for a few days now and my last resort is to ask others that know way more than I do. I having been following tutorials and code picked up along the way. I have not been able to perform the simple function of creating a camera preview. I am not interested in saving snap shots, just simply viewing the onboard camera in an app. Any help would be greatly appreciated. My ultimate goal is to have a preview of an IP camera. Is ther a full code out there for this application that I canplay around with or can someone let me know why this code does not work?
Here is the main:
package com.example.cameracontrol;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
The CameraPreview Class:
import java.io.IOException;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
#SuppressLint("ViewConstructor")
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "CameraPreview";
private SurfaceHolder mHolder;
private Camera mCamera;
private Size mPreviewSize;
#SuppressWarnings("deprecation")
public CameraPreview(Context context, Camera camera, Activity activity) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
}
catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try{
mCamera.stopPreview();
}
catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
/**Camera.Parameters parameters = mCamera.getParameters();
List<Size> localSizes = mCamera.getParameters().getSupportedPreviewSizes();
mPreviewSize = localSizes.get(0);
Log.d(TAG, "Width " + mPreviewSize.width);
Log.d(TAG, "Height " + mPreviewSize.height);
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height );
requestLayout();
mCamera.setParameters(parameters);*/
Camera.Parameters p = mCamera.getParameters();
p.setPreviewSize(w, h);
mCamera.setParameters(p);
//start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
}
catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
#SuppressLint("NewApi")
public static void setCameraDisplayOrientation(Activity activity, int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
int result;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = -90; break;
case Surface.ROTATION_180: degrees = 0; break;
case Surface.ROTATION_270: degrees = -90; break;
}
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
}
else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
}
The CameraActivity:
import com.example.cameracontrol.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;
public class CameraActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
private FrameLayout preview;
private static final String TAG = "CameraActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (checkCameraHardware(getBaseContext())){
// Create an instance of Camera
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera, CameraActivity.this);
preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
}
/** Check if this device has a camera */
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
Log.d(TAG, "Camera Available");
return true;
} else {
Log.d(TAG, "No Camera Found");
return false;
}
}
/** A safe way to get an instance of the Camera object. */
#SuppressLint("NewApi")
public Camera getCameraInstance(){
Camera c = null;
try {
int i = Camera.getNumberOfCameras();
releaseCamera(); //in case camera is being accessed by any other app.
Log.d(TAG, "Number of Cameras "+i +"\n");
c = Camera.open(); // attempt to get a Camera instance
Log.d(TAG, "Camera Opened");
}
catch (Exception e){
Log.d(TAG, "Camera Can't Be Accessed");
}
return c; // returns null if camera is unavailable
}
#Override
protected void onPause() {
super.onPause();
releaseCamera(); // release the camera immediately on pause event
}
private void releaseCamera(){
if (mCamera != null){
//mCamera.setPreviewCallback(null);
mPreview.getHolder().removeCallback(mPreview);
mCamera.release(); // release the camera for other applications
}
}
}
The activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CameraActivity" >
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight = "1" />
</LinearLayout>
The manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cameracontrol"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.cameracontrol.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CameraActivity"
android:label="#string/app_name"
android:screenOrientation="landscape">
<!-- configure this activity to use landscape orientation -->
</activity>
</application>
</manifest>
What is wrong with my coding clicking settings will crash app.
I cant read default settings values from prefs.xml
image sequence working like i want and now it's
moving right to left while images continue change (like a video)
someone genious see any problems ? :)
AndroidManifest.xml
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mywallpaper"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="12" />
<uses-feature android:name="android.software.live_wallpaper" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.BIND_WALLPAPER" />
<application
android:label="#string/app_name"
android:icon="#drawable/ic_launcher"
android:theme="#style/AppTheme">
<service
android:label="#string/app_name"
android:name=".MyWallpaperService"
android:enabled="true"
android:permission="android.permission.BIND_WALLPAPER">
<intent-filter android:priority="1">
<action
android:name="android.service.wallpaper.WallpaperService">
</action>
</intent-filter>
<meta-data
android:name="android.service.wallpaper"
android:resource="#xml/wallpaper" >
</meta-data>
</service>
<activity
android:label="myPrefs"
android:name="com.example.mywallpaper.Prefs"
android:permission="android.permission.BIND_WALLPAPER"
android:enabled="true"
android:exported="true">
<intent-filter>
<category android:name="android.intent.category.PREFERENCE" />
</intent-filter>
</activity>
</application>
</manifest>
wallpaper.xml
<wallpaper
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="something"
android:thumbnail="#drawable/ic_launcher"
android:description="#string/app_name"
android:settingsActivity="com.example.mywallpaper.Prefs">
</wallpaper>
prefs.xml
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Title Preference"
android:key="myPrefs">
<CheckBoxPreference
android:title="Touch"
android:key="touch"
android:summary="Touch enable"
android:defaultValue="true"
/>
<EditTextPreference
android:title="Speed"
android:key="speed"
android:summary="Animation speed"
android:defaultValue="500"
/>
</PreferenceScreen>
Prefs.java
package com.example.mywallpaper;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Prefs extends PreferenceActivity
implements
SharedPreferences.OnSharedPreferenceChangeListener {
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName(MyWallpaperService.SHARED_PREFS_NAME);
addPreferencesFromResource(R.xml.prefs);
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
#Override
protected void onResume() {
super.onResume();
}
#SuppressWarnings("deprecation")
#Override
protected void onDestroy() {
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
}
}
MyWallpaperService.java
package com.example.mywallpaper;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
public class MyWallpaperService extends WallpaperService {
public static final String SHARED_PREFS_NAME = "myPrefs";
//drawable image array
int[] myImageList = new int[]{
R.drawable.bg,
R.drawable.bg1,
R.drawable.bg2,
R.drawable.bg3,
R.drawable.bg4,
R.drawable.bg5,
R.drawable.bg6,
R.drawable.bg7,
R.drawable.bg8,
R.drawable.bg9,
R.drawable.bg10,
R.drawable.bg11,
R.drawable.bg12,
R.drawable.bg13,
R.drawable.bg14,
R.drawable.bg15,
R.drawable.bg16
};
public boolean runUp = true;
public boolean touchEnabled = true;
public int mSpeed = 100;
public int bgNumber = 0;
public int bgFrame = 0;
public int bgFrameMax = 20;
#Override
public Engine onCreateEngine() {
return new WallpaperEngine();
}
private class WallpaperEngine extends Engine
implements SharedPreferences.OnSharedPreferenceChangeListener {
private SharedPreferences mPrefs;
private boolean mVisible = false;
private Bitmap backgroundImage;
private final Handler mHandler = new Handler();
private final Runnable mUpdateDisplay = new Runnable() {
public void run() {
draw();
}
};
private void draw() {
SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try {
c = holder.lockCanvas();
if (c != null) {
//paint black
Paint p = new Paint();
p.setColor(Color.BLACK);
c.drawRect(0, 0, c.getWidth(), c.getHeight(), p);
drawBG(c);
}
} finally {
if (c != null)
holder.unlockCanvasAndPost(c);
}
mHandler.removeCallbacks(mUpdateDisplay);
if (mVisible) {
mHandler.postDelayed(mUpdateDisplay, mSpeed);
}
}
WallpaperEngine() {
mPrefs = MyWallpaperService.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
mPrefs.registerOnSharedPreferenceChangeListener(this);
onSharedPreferenceChanged(mPrefs, null);
}
#Override
public void onTouchEvent(MotionEvent event) {
//super.onTouchEvent(event);
if(touchEnabled){
bgNumber = 0;
}
}
public void drawBG(Canvas c){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
Resources res = getResources();
backgroundImage = BitmapFactory.decodeResource(res,myImageList[bgNumber], options);
//move looping imagas (video) right to left
int CH = c.getHeight();
int CW = c.getWidth();
double BH = backgroundImage.getHeight();
double BW = backgroundImage.getWidth();
double hScale = (BH/BW);
int bgSlideX = (CW/bgFrameMax);
int wallPH = (int) (CW*hScale);
int wLeft = 0+(CW-(bgSlideX*bgFrame));
int wTop = (int) ((CH/2)-(BH/2));
int wRight = CW+(CW-(bgSlideX*bgFrame));
int wBottom = wTop+wallPH;
Rect dest = new Rect(wLeft, wTop, wRight, wBottom);
Paint paint = new Paint();
paint.setFilterBitmap(true);
c.drawBitmap(backgroundImage, null, dest, paint);
bgFrame+=1;
if (bgFrame == bgFrameMax*2){
bgFrame = 0;
}
//Loop images up and down
if (runUp = true){
bgNumber += 1;
if (bgNumber == myImageList.length){
bgNumber=0;
runUp = false;
}
}
else if(runUp = false){
bgNumber -= 1;
if (bgNumber == 0){
bgNumber+=1;
runUp = true;
}
}
backgroundImage.recycle();
}
#Override
public void onVisibilityChanged(boolean visible) {
mVisible = visible;
if (visible) {
draw();
}
else {
mHandler.removeCallbacks(mUpdateDisplay);
}
}
#Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
draw();
}
#Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
mVisible = false;
mHandler.removeCallbacks(mUpdateDisplay);
}
#Override
public void onDestroy() {
super.onDestroy();
mVisible = false;
mHandler.removeCallbacks(mUpdateDisplay);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
mSpeed = Integer.valueOf(sharedPreferences.getString("speed", "5000"));
//sharedPreferences.getInt("speed", 100);
touchEnabled = sharedPreferences.getBoolean("touch", true);
//mHandler.post(mUpdateDisplay);
}
}
public void onPause(){
super.onDestroy();
}
}
Based on the code you posted I can already see that you never closed the tag in your prefs.xml. That would cause a problem. You need to add a at the end of your prefs.xml to close it.