android surfaceView 透明

引用:http://www.linuxidc.com/Linux/2011-11/47026.htm

  1. <span style="font-size:18px;color:#ff0000;">透明//Transparent  
  2. 半透明//Translucent</span>  

  1. package rw.Trans;  
  2.   
  3. import Android.app.Activity;  
  4. import Android.content.Context;  
  5. import Android.graphics.Bitmap;  
  6. import Android.graphics.BitmapFactory;  
  7. import Android.graphics.Canvas;  
  8. import Android.graphics.PixelFormat;  
  9. import Android.os.Bundle;  
  10. import Android.view.MotionEvent;  
  11. import Android.view.SurfaceHolder;  
  12. import Android.view.SurfaceHolder.Callback;  
  13. import Android.view.SurfaceView;  
  14.   
  15. public class TransParentActivity extends Activity {  
  16.     /** Called when the activity is first created. */  
  17.     class MySurfaceView extends SurfaceView implements Callback{  
  18.         float left,top;  
  19.         Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.reen);  
  20.         public MySurfaceView(Context context) {  
  21.             super(context);  
  22.             // TODO Auto-generated constructor stub   
  23.             <span style="color:#ff0000;">getHolder().setFormat(PixelFormat.TRANSPARENT);//设置为透明   
  24.             //getHolder().setFormat(PixelFormat.TRANSLUCENT);//半透明</span>   
  25.             getHolder().addCallback(this);  
  26.             setFocusable(true);  
  27.         }  
  28.   
  29.         @Override  
  30.         public void surfaceChanged(SurfaceHolder holder, int format, int width,  
  31.                 int height) {  
  32.             // TODO Auto-generated method stub   
  33.               
  34.         }  
  35.   
  36.         @Override  
  37.         public void surfaceCreated(SurfaceHolder holder) {  
  38.             // TODO Auto-generated method stub   
  39.          doDraw();    
  40.         }  
  41.   
  42.         private void doDraw() {  
  43.             // TODO Auto-generated method stub   
  44.             Canvas canvas=getHolder().lockCanvas();  
  45.             canvas.drawBitmap(bitmap, left, top, null);  
  46.             getHolder().unlockCanvasAndPost(canvas);  
  47.         }  
  48.   
  49.         @Override  
  50.         public void surfaceDestroyed(SurfaceHolder holder) {  
  51.             // TODO Auto-generated method stub   
  52.               
  53.         }  
  54.           
  55.         /* (non-Javadoc) 
  56.          * @see Android.app.Activity#onTouchEvent(android.view.MotionEvent) 
  57.          */  
  58.         @Override  
  59.         public boolean onTouchEvent(MotionEvent event) {  
  60.             // TODO Auto-generated method stub   
  61.             left=event.getX();  
  62.             top=event.getY();  
  63.             doDraw();  
  64.               
  65.             return super.onTouchEvent(event);  
  66.         }  
  67.     }  
  68.     @Override  
  69.     public void onCreate(Bundle savedInstanceState) {  
  70.         super.onCreate(savedInstanceState);  
  71.         setContentView(new MySurfaceView(getApplicationContext()));  
  72.           
  73.     }  
  74.       
  75. }  

AndroidManifest.xml

  1. <p><?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:Android="<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"  
  3.       package="rw.Trans"  
  4.       Android:versionCode="1"  
  5.       Android:versionName="1.0">  
  6.     <uses-sdk Android:minSdkVersion="10" /></p><p>    <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity Android:name=".TransParentActivity"  
  8.                   Android:label="Transparent SurfaceView"  
  9.                  <span style="color:#ff0000;"> Android:theme="@android:style/Theme.Translucent"  
  10. </span>                  ></p><p>                    
  11.             <intent-filter>  
  12.                 <action Android:name="android.intent.action.MAIN" />  
  13.                 <category Android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.         </activity></p><p>    </application>  
  16. </manifest></p>
posted @ 2012-09-28 08:08  镇水古月  阅读(248)  评论(0编辑  收藏  举报