简体   繁体   中英

How to set a transparent background?

I am making a project in which I want to draw a image on touch and I am able to draw the image on touch but I want to set the transparent background that I am unable to set, the code that I used is:

In the manifest file:

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

Code:

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

    }

}

Try this.

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

            }

You can use this code. Ff you dont mind.

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

Hope this helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM