0

My app consist on moving a picture when the user speaks. I have done this, but what I want to do now is that I want to set a image as background for my app. I am working with canvas, as you can see in the class I have included below. So how can I set a background using canvas and not influencing the movement of my picture. Or is there any possibility to connect this class with a xml file where I can define the background?

Thanks in advance

Here is the class:

package com.example.prova1;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;

import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MoveBalloon extends Activity {
 Bitmap balloon;
 DrawBalloon myView;
 float x,y,sensorX, sensorY;
 SensorManager sm;
 Microphone mic;



public class DrawBalloon extends SurfaceView implements Runnable {

 SurfaceHolder ourHolder ;
 Thread ourThread = null;
 boolean isRunning=true;

 public DrawBalloon(Context context) {
  super(context);
  ourHolder= getHolder();
 }

 public void pause() {
  isRunning=false;
  while(true){
   try{
    ourThread.join();
   } catch (InterruptedException e){
    e.printStackTrace();
   }
   break;
  }
  ourThread=null;
 }

 public void resume(){
  isRunning=true;
  ourThread = new Thread(this);
  ourThread.start();


 }
 @Override
 public void run (){
  while(isRunning){
   if(!ourHolder.getSurface().isValid())
    continue;
            Canvas canvas = ourHolder.lockCanvas();

   updateMic();
    canvas.drawColor(Color.BLACK);
   canvas.drawBitmap(balloon, sensorX, sensorY,null);
   ourHolder.unlockCanvasAndPost(canvas);
  }
 }
}



@Override
protected void onCreate(Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 super.onCreate(savedInstanceState);
 /*sm= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
 if(sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size()!=0){
  Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(o);
  sm.registerListener(this,s ,SensorManager.SENSOR_DELAY_NORMAL);
 }*/
mic = new Microphone();
balloon = BitmapFactory.decodeResource(getResources(), R.drawable.images);
sensorX=150;
sensorY=350;
//x=y=sensorX=sensorY=0;
myView= new DrawBalloon (this);
myView.resume();
setContentView(myView);
}



/*@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
 // TODO Auto-generated method stub

}



@Override
public void onSensorChanged(SensorEvent event) {
 // TODO Auto-generated method stub
 try {
  Thread.sleep(20);
 } catch (InterruptedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 sensorX=event.values[0];
 sensorY=event.values[1];

}

*/
public void updateMic(){
int level = mic.getLevel();

 sensorY-=level;
}

@Override
public void onBackPressed() {


 finish();


    }


}
user2804038
  • 1,093
  • 3
  • 15
  • 17

1 Answers1

0

You can use an xml layout instead of setting content view with a java object :

setContentView(R.layout.move_balloon);

and add your custom view (DrawBalloon) in the xml layout by specifying it's package location, and change the background by setting an image to the background of the root element :

<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"
    tools:context=".MoveBalloon"
    android:background="@drawable/ic_launcher" 
>

    <com.example.prova1.MoveBalloon.DrawBalloon
        android:id="@+id/drawBalloon1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="182dp" />

</RelativeLayout>

but for this to work , your custom view must have a special constructor in able to be inflated from xml layout file :

public DrawBalloon(Context context, AttributeSet attrs) {
    super(context, attrs);
    ourHolder= getHolder();
}
Tourki
  • 1,785
  • 15
  • 22
  • i forgot to say that you should put the background color of your custom view to translparent : android:background="#00000000" – Tourki Sep 22 '13 at 12:22
  • I try what you said but I hav the error inflating class. com. I posted the logcat above – user2804038 Sep 22 '13 at 12:38
  • What can i do to avoid this error? It seems that it is in th exml file in com.example.prova1.MoveBalloon.DrawBalloon. I am trying but till I havn`t solve it. – user2804038 Sep 22 '13 at 12:46
  • This is the line of the logcat: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prova1/com.example.prova1.MoveBalloon}: android.view.InflateException: Binary XML file line #8: Error inflating class com.example.prova1.MoveBalloon.DrawBalloon – user2804038 Sep 22 '13 at 12:56
  • have you tried to extract the sub class DrawBalloon to a separate class in a separate java file ? – Tourki Sep 22 '13 at 21:38