使用画布将图像设置为背景android

时间:2013-09-22 11:40:04

标签: android android-canvas android-background

我的应用程序包括在用户说话时移动图片。我已经这样做了,但我现在要做的是我想将图像设置为我的应用程序的背景。我正在使用canvas,正如你在下面的课程中看到的那样。那么如何使用画布设置背景而不影响我的图片的移动。或者是否有可能将此类与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();


    }


}

1 个答案:

答案 0 :(得分: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();
}