android surfaceView 透明
引用:http://www.linuxidc.com/Linux/2011-11/47026.htm
- <span style="font-size:18px;color:#ff0000;">透明//Transparent
- 半透明//Translucent</span>
- package rw.Trans;
- import Android.app.Activity;
- import Android.content.Context;
- import Android.graphics.Bitmap;
- import Android.graphics.BitmapFactory;
- import Android.graphics.Canvas;
- import Android.graphics.PixelFormat;
- import Android.os.Bundle;
- import Android.view.MotionEvent;
- import Android.view.SurfaceHolder;
- import Android.view.SurfaceHolder.Callback;
- import Android.view.SurfaceView;
- public class TransParentActivity extends Activity {
- /** Called when the activity is first created. */
- class MySurfaceView extends SurfaceView implements Callback{
- float left,top;
- Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.reen);
- public MySurfaceView(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- <span style="color:#ff0000;">getHolder().setFormat(PixelFormat.TRANSPARENT);//设置为透明
- //getHolder().setFormat(PixelFormat.TRANSLUCENT);//半透明</span>
- getHolder().addCallback(this);
- setFocusable(true);
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- // TODO Auto-generated method stub
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- doDraw();
- }
- private void doDraw() {
- // TODO Auto-generated method stub
- Canvas canvas=getHolder().lockCanvas();
- canvas.drawBitmap(bitmap, left, top, null);
- getHolder().unlockCanvasAndPost(canvas);
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- }
- /* (non-Javadoc)
- * @see Android.app.Activity#onTouchEvent(android.view.MotionEvent)
- */
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- // TODO Auto-generated method stub
- left=event.getX();
- top=event.getY();
- doDraw();
- return super.onTouchEvent(event);
- }
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(new MySurfaceView(getApplicationContext()));
- }
- }
AndroidManifest.xml
- <p><?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:Android="<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"
- package="rw.Trans"
- Android:versionCode="1"
- Android:versionName="1.0">
- <uses-sdk Android:minSdkVersion="10" /></p><p> <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity Android:name=".TransParentActivity"
- Android:label="Transparent SurfaceView"
- <span style="color:#ff0000;"> Android:theme="@android:style/Theme.Translucent"
- </span> ></p><p>
- <intent-filter>
- <action Android:name="android.intent.action.MAIN" />
- <category Android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity></p><p> </application>
- </manifest></p>