android surfaceView 背景透明2

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

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

android surfaceView 背景透明2

  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>  
上一篇:IDEA设置酷炫界面透明度-Background Image Plus插件


下一篇:《iOS 6高级开发手册(第4版)》——1.7节同步获取当前的加速计角度