SurfaceView 透明和半透明

  1. 透明//Transparent  
  2. 半透明//Translucent

  1.  
  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-generateconstructor stub  
  23.             <span style="color:#ff0000;">getHolder().setFormat(PixelFormat.TRANSPARENT);//设置为透明  
  24.             //getHolder().setFormat(PixelFormat.TRANSLUCENT);//半透明   
  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-generatemethod stub  
  33.               
  34.         }  
  35.   
  36.         @Override  
  37.         public void surfaceCreated(SurfaceHolder holder) {  
  38.             // TODO Auto-generatemethod stub  
  39.          doDraw();    
  40.         }  
  41.   
  42.         private void doDraw() {  
  43.             // TODO Auto-generatemethod stub  
  44.             Canvas canvas=getHolder().lockCanvas();  
  45.             canvas.drawBitmap(bitmap, left, topnull);  
  46.             getHolder().unlockCanvasAndPost(canvas);  
  47.         }  
  48.   
  49.         @Override  
  50.         public void surfaceDestroyed(SurfaceHolder holder) {  
  51.             // TODO Auto-generatemethod 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-generatemethod 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. <?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" />     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".TransParentActivity"  
  8.                   android:label="Transparent SurfaceView"  
  9.                  android:theme="@android:style/Theme.Translucent"  
  10.                   >                     
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.         </activity>     </application>  
  16. </manifest>   

posted on 2012-01-01 23:06 ①块腹肌 阅读(...) 评论(...) 编辑 收藏

导航