我怎么设置透明背景?

时间:2014-04-15 07:32:27

标签: 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();
        }

    }

}

1 个答案:

答案 0 :(得分:0)

在res / values / styles.xml文件中添加以下样式(如果没有,请创建它。)这是一个完整的文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
  </style>
</resources>

(@ color / transparent的值是我放在res / values / color.xml文件中的颜色值#00000000。你也可以在以后的Android版本中使用@android:color / transparent)

然后将样式应用于您的活动,例如:

<activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent">
...
</activity>