2013-09-22 43 views
6

Tôi đang cố gắng thay đổi màu nền của một TextureView. Tôi mong đợi màu này sẽ được nhìn thấy thông qua xem trước camera bán trong suốt. Đối với một số lý do nó không. Nếu tôi xóa hoàn toàn xem trước máy ảnh từ onSurfaceTextureAvailable chế độ xem vẫn giữ màu trắng (hoặc không thể nhìn thấy). Bất kỳ ý tưởng? Tôi đang cố gắng để đạt được một hộp TextureView mà đôi khi có xem trước máy ảnh và thời gian khác là một màu sắc đơn giản của sự lựa chọn của tôi. Cảm ơn.TextureView setBackgroundColor không hoạt động

import android.graphics.Color; 
import android.graphics.SurfaceTexture; 
import android.hardware.Camera; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.Surface; 
import android.view.TextureView; 
import android.widget.FrameLayout; 
import android.widget.RelativeLayout; 

import java.io.IOException; 

public class MainActivity extends Activity { 

    private TextureView mTextureView; 
    FrameLayout screenFrame; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     screenFrame = (FrameLayout)findViewById(R.id.screenFrame); 

     setVideoScreens(); 
    } 


    @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; 
    } 

    private void setVideoScreens() 
    { 
     final LiveCameraActivity livecam = new LiveCameraActivity(); 

     mTextureView = new TextureView(this); 
     mTextureView.setSurfaceTextureListener(livecam); 

     FrameLayout mainFrame = (FrameLayout)findViewById(R.id.mainFrame); 
     mainFrame.addView(mTextureView); 

     int width = 640; 
     int height = 480; 

     mTextureView.setLayoutParams(new FrameLayout.LayoutParams(
       width, height)); 
     mTextureView.setY(200); 
     mTextureView.setX(200); 
     mTextureView.setBackgroundColor(Color.BLUE); 
     mTextureView.setAlpha(.5f); 
    } 

    public class LiveCameraActivity extends Activity implements TextureView.SurfaceTextureListener 
    { 
     private Camera mCamera; 

     protected void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
     } 


     public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) 
     { 
      mCamera = Camera.open(); 

      try 
      { 
       mCamera.setPreviewTexture(surface); 
       mCamera.startPreview(); 
      } 
      catch (IOException ioe) 
      { 
       // Something bad happened 
      } 
     } 

     public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) 
     { 
      // Ignored, Camera does all the work for us 
     } 

     public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) 
     { 
      mCamera.stopPreview(); 
      mCamera.release(); 
      return true; 
     } 

     public void onSurfaceTextureUpdated(SurfaceTexture surface) 
     { 
      // Invoked every time there's a new Camera preview frame 

     } 
    } 
} 
+0

Vui lòng chia sẻ mã hoàn chỉnh để chúng tôi có thể trợ giúp đúng cách. –

+0

bạn đã sửa chưa? – sandrstar

+0

cùng một vấn đề. Bất kì giải pháp nào? – Alexis

Trả lời

1

Giải pháp của tôi là để thiết lập đục false:

textureView.setOpaque(false); 

nhưng, khi bạn làm điều đó màu nền của TextureView sẽ không thích hợp. Vì vậy, tôi đã đặt TextureView của mình bên trong một FrameLayout và đặt màu nền của nó. Khi bạn làm điều đó, màu nền FrameLayout là màu bạn nhìn thấy 'phía sau' hoặc nếu số TextureView trống.