2011-12-05 75 views
7

我有簡單的佈局如何使透明背景的SurfaceView?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/backgroundtimer" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/TextView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="@string/hello" /> 

    <com.fmech.zenclock.surface.ZenClockSurface 
     android:id="@+id/zenClockSurface1" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true"/> 

</RelativeLayout> 

和A ZenClockSurface類

package com.fmech.zenclock.surface; 

import android.content.Context; 
import android.content.res.Resources; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Matrix; 
import android.graphics.Paint; 
import android.graphics.Paint.Style; 
import android.graphics.PixelFormat; 
import android.util.AttributeSet; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

public class ZenClockSurface extends SurfaceView implements SurfaceHolder.Callback{ 

    private DrawClock drawClock; 

    public ZenClockSurface(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     this.getHolder().setFormat(PixelFormat.TRANSLUCENT); 
     getHolder().addCallback(this); 
    } 

    public ZenClockSurface(Context context) { 
     super(context); 
     this.getHolder().setFormat(PixelFormat.TRANSLUCENT); 
     getHolder().addCallback(this); 
    } 

    public ZenClockSurface(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.getHolder().setFormat(PixelFormat.TRANSLUCENT); 
     getHolder().addCallback(this); 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
      int height) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     //this.getHolder().setFormat(PixelFormat.TRANSPARENT); 
     drawClock = new DrawClock(getHolder(), getResources()); 
     drawClock.setRunning(true); 
     drawClock.start(); 

    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     boolean retry = true; 
     // завершаем работу потока 
     drawClock.setRunning(false); 
     while (retry) { 
      try { 
       drawClock.join(); 
       retry = false; 
      } catch (InterruptedException e) { 
      } 
     } 

    } 

    class DrawClock extends Thread{ 
     private boolean runFlag = false; 
     private SurfaceHolder surfaceHolder; 
     private Bitmap picture; 
     private Matrix matrix; 
     private long prevTime; 
     private Paint painter; 

     public DrawClock(SurfaceHolder surfaceHolder, Resources resources){ 
      this.surfaceHolder = surfaceHolder; 

      this.surfaceHolder.setFormat(PixelFormat.TRANSLUCENT); 
      picture = BitmapFactory.decodeResource(resources, R.drawable.ic_launcher); 

      matrix = new Matrix(); 
      this.painter=new Paint(); 
      this.painter.setStyle(Paint.Style.FILL); 

     } 

     public void setRunning(boolean run) { 
      runFlag = run; 
     } 

     @Override 
     public void run() { 
      Canvas canvas; 
      while (runFlag) { 

        matrix.preRotate(1.0f, picture.getWidth()/2, picture.getHeight()/2); 
       canvas = null; 
       try { 
        //surfaceHolder.getSurface().setAlpha(0.5f); 
        canvas = surfaceHolder.lockCanvas(null); 
        synchronized (surfaceHolder) { 

         canvas.drawColor(Color.TRANSPARENT); 

         canvas.drawBitmap(picture, matrix, this.painter); 
        } 
       } 
       finally { 
        if (canvas != null) { 
         surfaceHolder.unlockCanvasAndPost(canvas); 
        } 
       } 
      } 
     } 
    } 
} 

和活動的代碼

package com.fmech.zenclock.surface; 

import android.app.Activity; 
import android.graphics.PixelFormat; 
import android.os.Bundle; 

public class ZenClockSurfaceActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getWindow().setFormat(PixelFormat.TRANSLUCENT); 
     setContentView(R.layout.main); 


    } 
} 

我想要什麼的Android畫面的背景色是tranparent,但我得到的黑色背景。

我在RelativeLayout上添加了一些圖片背景SurfaceView旋轉了Android圖標,沒有透明。

我如何做透明?

+0

我忘了添加this.getHolder()。setFormat(PixelFormat.TRANSLUCENT);在這兩個構造函數 – Rasel

回答

25

是的,我做到了我解決問題

活動代碼

package com.fmech.zenclock.surface; 

import android.app.Activity; 
import android.graphics.PixelFormat; 
import android.os.Bundle; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

public class ZenClockSurfaceActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     ZenClockSurface sfvTrack = (ZenClockSurface)findViewById(R.id.zenClockSurface1); 
     sfvTrack.setZOrderOnTop(true); // necessary 
     SurfaceHolder sfhTrack = sfvTrack.getHolder(); 
     sfhTrack.setFormat(PixelFormat.TRANSLUCENT); 

    } 
} 

表面代碼

package com.fmech.zenclock.surface; 

import android.content.Context; 
import android.content.res.Resources; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Matrix; 
import android.graphics.Paint; 
import android.graphics.Paint.Style; 
import android.graphics.PixelFormat; 
import android.graphics.Region; 
import android.util.AttributeSet; 
import android.view.Surface.OutOfResourcesException; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

public class ZenClockSurface extends SurfaceView implements 
     SurfaceHolder.Callback { 

    private DrawClock drawClock; 

    public ZenClockSurface(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     getHolder().addCallback(this); 
    } 

    public ZenClockSurface(Context context) { 
     super(context); 
     getHolder().addCallback(this); 
    } 

    public ZenClockSurface(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     getHolder().addCallback(this); 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
      int height) { 

    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     drawClock = new DrawClock(getHolder(), getResources()); 
     drawClock.setRunning(true); 
     drawClock.start(); 

    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     boolean retry = true; 
     drawClock.setRunning(false); 
     while (retry) { 
      try { 
       drawClock.join(); 
       retry = false; 
      } catch (InterruptedException e) { 
      } 
     } 

    } 

    class DrawClock extends Thread { 
     private boolean runFlag = false; 
     private SurfaceHolder surfaceHolder; 
     private Bitmap picture; 
     private Matrix matrix; 
     private Paint painter; 

     public DrawClock(SurfaceHolder surfaceHolder, Resources resources) { 
      this.surfaceHolder = surfaceHolder; 

      picture = BitmapFactory.decodeResource(resources, 
        R.drawable.ic_launcher); 
      matrix = new Matrix(); 
      this.painter = new Paint(); 
      this.painter.setStyle(Paint.Style.FILL); 
      this.painter.setAntiAlias(true); 
      this.painter.setFilterBitmap(true); 
     } 

     public void setRunning(boolean run) { 
      runFlag = run; 
     } 

     @Override 
     public void run() { 
      Canvas canvas; 
      while (runFlag) { 


       matrix.preRotate(1.0f, picture.getWidth()/2, 
         picture.getHeight()/2); 
       canvas = null; 
       try { 
        canvas = surfaceHolder.lockCanvas(null); 
        synchronized (surfaceHolder) { 
         canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR); 
         canvas.drawBitmap(picture, matrix, this.painter); 
        } 
       } catch (IllegalArgumentException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } finally { 
        if (canvas != null) { 
         surfaceHolder.unlockCanvasAndPost(canvas); 
        } 
       } 
      } 
     } 
    } 
} 

這是工作。

+1

drawColor(0,android.graphics。PorterDuff.Mode.CLEAR);非常感謝你,我一直在尋找這個小時!我以爲drawColor(Color.TRANSPARENT);將工作....但它沒有,你的解決方案救了我..! – tom91136

+0

它沒有爲我工作。我將佈局的背景設置爲顏色#f0f0f0,但表面視圖的背景仍然保持黑色。 – Genry

+0

@Dmitry Nelepov:我嘗試了相同的代碼。但沒有爲我工作。表面視圖的顏色仍然是黑色。 – user2085965

3

在API 3和4中,您不能在SurfaceView之後放置任何東西。引用Dianne Hackborn:

表面視圖實際上是在你的窗口後面,並且在窗口中打了一個洞讓你看到它。因此,您可以在窗口中將其放置在頂部,但窗口中不會出現任何窗口。

http://groups.google.com/group/android-developers/browse_thread/thread/8d88ef9bb22da574


從AP 5日起,您可以使用setZOrderOnTop。訣竅是,該視圖連接到窗口之前,你必須這樣做,在你的構造函數,因此它被稱爲:

public ZenClockSurface(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    setZOrderOnTop(true); 

    SurfaceHolder holder = getHolder(); 
    holder.setFormat(PixelFormat.TRANSLUCENT); 
} 
+0

我都不正確。下一個活動的變化使SorfaceView透明 public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); ZenClockSurface sfvTrack =(ZenClockSurface)findViewById(R.id.zenClockSurface1); sfvTrack.setZOrderOnTop(true); //必要 SurfaceHolder sfhTrack = sfvTrack.getHolder(); sfhTrack.setFormat(PixelFormat.TRANSLUCENT); } –

+0

但我仍然有麻煩,現在它變成 - 圖片是黑色的網站。 –

+0

感謝您指出'setZOrderOnTop',Rainwork!我編輯了我的答案。請讓我知道這對你有沒有用。 – chiuki