如何设置透明背景?

时间:2014-04-15 08:02:29

标签: java android

我正在制作一个项目,我想在触摸时绘制图像,我能够在触摸时绘制图像,但我想设置我无法设置的透明背景,我使用的代码是:

在清单文件中:

<activity
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:name="com.trial.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>

代码:

public class MainActivity extends Activity implements OnTouchListener {

    MySurface oursurfaceview;
    float x, y;
    Bitmap goofy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        oursurfaceview = new MySurface(this);
        oursurfaceview.setOnTouchListener(this);
        setContentView(oursurfaceview);
        x = 0;
        y = 0;
        goofy = BitmapFactory.decodeResource(getResources(), R.drawable.goofy);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        oursurfaceview.pause();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        oursurfaceview.resume();
    }

    @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;
    }

    @Override
    public boolean onTouch(View arg0, MotionEvent event) {
        // TODO Auto-generated method stub
        x = event.getX();
        y = event.getY();
        return false;
    }

    public class MySurface extends SurfaceView implements Runnable {

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

        public MySurface(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            ourHolder = getHolder();

        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (isRunning) {
                if (!ourHolder.getSurface().isValid())
                    continue;

                Canvas canvas = ourHolder.lockCanvas();

                if (x != 0 && y != 0) {
                    Bitmap test = BitmapFactory.decodeResource(getResources(),
                            R.drawable.goofy);
                    canvas.drawBitmap(test, x - (goofy.getWidth() / 2), y
                            - (goofy.getHeight() / 2), null);

                }
                ourHolder.unlockCanvasAndPost(canvas);

            }
        }

        public void pause() {
            // TODO Auto-generated method stub
            isRunning = false;
            while (true) {
                try {
                    ourThread.join();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            }
            ourThread = null;
        }

        public void resume() {
            // TODO Auto-generated method stub
            isRunning = true;
            ourThread = new Thread(this);
            ourThread.start();
        }

    }

}

2 个答案:

答案 0 :(得分:1)

试试这个。

          public MySurface(Context context) {
                super(context);
                // TODO Auto-generated constructor stub

                    this.setBackgroundColor(Color.TRANSPARENT);                 
                    this.setZOrderOnTop(true); //necessary                
                    getHolder().setFormat(PixelFormat.TRANSPARENT); 
                    getHolder().addCallback(this); 
                    ourHolder = getHolder();

            }

答案 1 :(得分:-2)

您可以使用此代码。如果你不介意的话。

android:background="@android:color/transparent"

希望这会有所帮助。