0

我的应用程序包括在用户说话时移动图片。我已经这样做了,但我现在想做的是我想将图像设置为我的应用程序的背景。我正在使用画布,您可以在我下面的课程中看到。那么如何使用画布设置背景而不影响图片的移动。或者有没有可能将这个类与一个我可以定义背景的xml文件连接起来?

提前致谢

这是课程:

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();


    }


}
4

1 回答 1

0

您可以使用 xml 布局而不是使用 java 对象设置内容视图:

setContentView(R.layout.move_balloon);

并通过指定其包位置在 xml 布局中添加您的自定义视图 (DrawBalloon),并通过将图像设置为根元素的背景来更改背景:

<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>

但要使其正常工作,您的自定义视图必须有一个特殊的构造函数才能从 xml 布局文件中扩展:

public DrawBalloon(Context context, AttributeSet attrs) {
    super(context, attrs);
    ourHolder= getHolder();
}
于 2013-09-22T11:57:44.973 回答