2014-04-15 168 views
0

我正在製作一個項目,我想要在觸摸時繪製圖像,並且我可以通過觸摸來繪製圖像,但是我想設置我無法設置的透明背景,我使用的代碼是..我如何設置透明背景?

在manifest文件中

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

代碼...

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

    } 

} 

回答

0

添加下面的風格在你RES /價值/ styles.xml文件(如果你沒有一個,創建它)。這是一個完整的文件:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="Theme.Transparent" parent="android:Theme"> 
    <item name="android:windowIsTranslucent">true</item> 
    <item name="android:windowBackground">@android:color/transparent</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowIsFloating">true</item> 
    <item name="android:backgroundDimEnabled">false</item> 
    </style> 
</resources> 

(值@彩色/透明是色彩值#00000000我把在res /值/ color.xml文件。您還可以使用@android:彩色/在以後的Android版本透明)

然後將該樣式應用到你的活動,例如:

<activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent"> 
... 
</activity> 
+0

嘗試。它會幫助你設置透明背景 –

+0

'android:theme =「@ android:style/Theme.Translucent」' 添加此清單將工作良好,直到android 2.3 –

+0

沒有區別,它不設置透明背景。使用上面的代碼一次,你會知道什麼是你使用上面的代碼 –