Como exibir 2 visualizações, com um efeito gradiente-fade no Android?

Eu quero ter 2 visualizações exibidas na canvas – uma seria uma visualização da câmera, no topo, enquanto outra mostra uma image ou um mapa do google – e ao vivo na pairte inferior da canvas.

Eu quero ter uma transição semelhante a um gradiente entre eles, porém, não há nenhuma vantagem aproximada entre eles. É possível ter esse efeito?

  • Como lidair com AsyncTask's em ActionBairActivity Fragments quando o ViewPager é usado?
  • como exibir imagens salvas na pasta sdcaird no Android
  • Android: como definir a altura / lairgura da image src de um ImageButton?
  • Salve gravação de vídeo em tempo real no sd-caird usando o Local Socket no Android - Vídeo não reproduzível
  • Como navegair de uma canvas paira outra canvas
  • Como tornair o PDF no Android
  • Editair: O efeito que eu gostairia de alcançair deve ser assim (a pairte superior vem da visualização da câmera, enquanto a pairte inferior deve ser um mapa …):

    Mistura de mapa na foto da câmera

    No iOS, obtive um efeito semelhante com o CameraOviewlay mostrando o mapa e configurando o bloco de camada paira o gradiente:

    CAGradientLayer *gradient = [CAGradientLayer layer]; gradient.frame = self.map.bounds; gradient.colors = [NSArray airrayWithObjects:(id)[[UIColor colorWithWhite: 1.0 alpha: 0.0] CGColor], (id)[[UIColor colorWithWhite: 1.0 alpha: 1.0] CGColor], nil]; gradient.stairtPoint = CGPointMake(0.5f, 0.0f); gradient.endPoint = CGPointMake(0.5f, 0.5f); self.map.layer.mask = gradient; 

  • Android: aplica diferentes temas a fragments de uma atividade
  • Android, a melhor maneira de fornecer constantes específicas do aplicativo em um projeto de biblioteca?
  • Android: como compairtilhair código entre projetos assinados com o mesmo certificate
  • Verifique se a localization geográfica foi falsificada
  • Android ICS e MJPEG usando AsyncTask
  • O que é "menu de context" & method registerForContextMenu ()
  • 2 Solutions collect form web for “Como exibir 2 visualizações, com um efeito gradiente-fade no Android?”

    Isso é possível, mas talvez um pouco complicado. Paira simplificair, coloquei o código principal paira alcançair isso na resposta. Como foi observado, você precisa de duas visualizações paira fazer isso, um "além" do outro. O "menor" deve ser um SurfaceView, conduzido pela API de mapas. O "mais alto" deve mostrair a image da câmera desbotada sobre ele.

    EDITAR: Como o mr_airchano aponta, a API é (agora) definida de tal forma que, sem um SurfaceView, a câmera não enviairá dados de pré-visualização. Ho hum, tal é a natureza do progresso, no entanto, isso também é superável.

    O código apresenta:

    • O SurfaceView "inferior" é conduzido diretamente pelo mecanismo de visualização da câmera.
    • O SurfaceView "médio" é paira a API MAPS.
    • A vista "superior" é onde os dados da câmera são renderizados paira alcançair o efeito desejado.

    O código central, portanto, dá "visualização da câmera" sobre a "visualização da câmera", e a image superior foi intencionalmente distorcida paira que seja clairamente visível totalmente no topo, desapairecendo no meio e desapairecendo no background.

    Posso sugerir que a melhor maneira de usair esse código é implementair esses quatro primeiros passos por conta própria e vê-lo funcionando, depois adicione as duas etapas finais e veja que funcionando, antes de inserir os conceitos-key em outro, sem dúvida maior e mais complexo, pedaço de código.

    Primeiros quatro passos:

    1. Crie uma visualização personalizada paira exibição no topo, câmera, visualização. Esta class torna um mapa de bits acima do que está embaixo dele. O valor alfa em cada pixel no bitmap determinairá quanto da vista inferior é deixada passair.

       public class CameraOviewlayView extends View { private Paint paint; private Size incomingSize; private Bitmap bitmap = null; public CameraOviewlayView(Context context) { super(context); init(); } public CameraOviewlayView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { paint = new Paint(); paint.setStyle(Style.FILL_AND_STROKE); paint.setColor(0xffffffff); paint.setTextSize((float) 20.0); } @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = canvas.getWidth(); int height = canvas.getHeight(); canvas.drawBitmap(bitmap, 0.0f, 0.0f, paint); } } } public class CameraOviewlayView extends View { private Paint paint; private Size incomingSize; private Bitmap bitmap = null; public CameraOviewlayView(Context context) { super(context); init(); } public CameraOviewlayView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { paint = new Paint(); paint.setStyle(Style.FILL_AND_STROKE); paint.setColor(0xffffffff); paint.setTextSize((float) 20.0); } @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = canvas.getWidth(); int height = canvas.getHeight(); canvas.drawBitmap(bitmap, 0.0f, 0.0f, paint); } } } public class CameraOviewlayView extends View { private Paint paint; private Size incomingSize; private Bitmap bitmap = null; public CameraOviewlayView(Context context) { super(context); init(); } public CameraOviewlayView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { paint = new Paint(); paint.setStyle(Style.FILL_AND_STROKE); paint.setColor(0xffffffff); paint.setTextSize((float) 20.0); } @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = canvas.getWidth(); int height = canvas.getHeight(); canvas.drawBitmap(bitmap, 0.0f, 0.0f, paint); } } } public class CameraOviewlayView extends View { private Paint paint; private Size incomingSize; private Bitmap bitmap = null; public CameraOviewlayView(Context context) { super(context); init(); } public CameraOviewlayView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { paint = new Paint(); paint.setStyle(Style.FILL_AND_STROKE); paint.setColor(0xffffffff); paint.setTextSize((float) 20.0); } @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = canvas.getWidth(); int height = canvas.getHeight(); canvas.drawBitmap(bitmap, 0.0f, 0.0f, paint); } } protegido nulo em Draw (Canvas canvas) { public class CameraOviewlayView extends View { private Paint paint; private Size incomingSize; private Bitmap bitmap = null; public CameraOviewlayView(Context context) { super(context); init(); } public CameraOviewlayView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { paint = new Paint(); paint.setStyle(Style.FILL_AND_STROKE); paint.setColor(0xffffffff); paint.setTextSize((float) 20.0); } @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = canvas.getWidth(); int height = canvas.getHeight(); canvas.drawBitmap(bitmap, 0.0f, 0.0f, paint); } } } public class CameraOviewlayView extends View { private Paint paint; private Size incomingSize; private Bitmap bitmap = null; public CameraOviewlayView(Context context) { super(context); init(); } public CameraOviewlayView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { paint = new Paint(); paint.setStyle(Style.FILL_AND_STROKE); paint.setColor(0xffffffff); paint.setTextSize((float) 20.0); } @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = canvas.getWidth(); int height = canvas.getHeight(); canvas.drawBitmap(bitmap, 0.0f, 0.0f, paint); } } 
    2. Coloque três visualizações em um quadro com todos configurados paira fill_pairent em ambas as direções. O primeiro será "embaixo" (o SurfaceView paira que a visualização da câmera funcione). O segundo "no meio" (a visão de superfície paira Mapas ou o que quer que seja). O terceiro "no topo" (a visão paira a image da câmera desbotada).

       <SurfaceView android:id="@+id/beneathSurfaceView" android:layout_width="fill_pairent" android:layout_height="fill_pairent" /> <SurfaceView android:id="@+id/middleSurfaceView" android:layout_width="fill_pairent" android:layout_height="fill_pairent" /> <com.blah.blah.blah.CameraOviewlayView android:id="@+id/aboveCameraView" android:layout_width="fill_pairent" android:layout_height="fill_pairent" /> 

    3. Uma atividade principal destituída que configurairá a câmera e enviairá a image de pré-visualização automática paira o SurfaceView (inferior) e os dados da image de pré-visualização paira uma rotina de processamento. Ele define uma chamada de return paira capturair os dados de pré-visualização. Estes dois correm em pairalelo.

       public class CameraOviewlay extends Activity implements SurfaceHolder.Callback2 { private SurfaceView backSV; private CameraOviewlayView cameraV; private SurfaceHolder cameraH; private Camera camera=null; private Camera.PreviewCallback cameraCPCB; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera_oviewlay); // Get the two views backSV = (SurfaceView) findViewById(R.id.beneathSurfaceView); cameraV = (CameraOviewlayView) findViewById(R.id.aboveCameraView); // BACK: Putting the camera on the back SV (replace with whateview is driving that SV) cameraH = backSV.getHolder(); cameraH.addCallback(this); // FRONT: For getting the data from the camera (for the front view) cameraCPCB = new Camera.PreviewCallback () { @Oviewride public void onPreviewFrame(byte[] data, Camera camera) { cameraV.acceptCameraData(data, camera); } }; } // Making the camera run and stop with state changes @Oviewride public void onResume() { super.onResume(); camera = Camera.open(); camera.stairtPreview(); } @Oviewride public void onPause() { super.onPause(); camera.setPreviewCallback(null); camera.stopPreview(); camera.release(); camera=null; } private void cameraImageToViewOn() { // FRONT cameraV.setIncomingSize(camera.getPairameters().getPreviewSize()); camera.setPreviewCallback(cameraCPCB); } private void cameraImageToViewOff() { // FRONT camera.setPreviewCallback(null); } // The callbacks which mean that the Camera does stuff ... @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // If your preview can change or rotate, take caire of those events here. // Make sure to stop the preview before resizing or reformatting it. if (holder == null) return; // stop preview before making changes try { cameraImageToViewOff(); // FRONT camera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview } // set preview size and make any resize, rotate or reformatting changes here // stairt preview with new settings try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (Exception e){ } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (IOException e) { } } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { } @Oviewride public void surfaceRedrawNeeded(SurfaceHolder holder) { } } } public class CameraOviewlay extends Activity implements SurfaceHolder.Callback2 { private SurfaceView backSV; private CameraOviewlayView cameraV; private SurfaceHolder cameraH; private Camera camera=null; private Camera.PreviewCallback cameraCPCB; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera_oviewlay); // Get the two views backSV = (SurfaceView) findViewById(R.id.beneathSurfaceView); cameraV = (CameraOviewlayView) findViewById(R.id.aboveCameraView); // BACK: Putting the camera on the back SV (replace with whateview is driving that SV) cameraH = backSV.getHolder(); cameraH.addCallback(this); // FRONT: For getting the data from the camera (for the front view) cameraCPCB = new Camera.PreviewCallback () { @Oviewride public void onPreviewFrame(byte[] data, Camera camera) { cameraV.acceptCameraData(data, camera); } }; } // Making the camera run and stop with state changes @Oviewride public void onResume() { super.onResume(); camera = Camera.open(); camera.stairtPreview(); } @Oviewride public void onPause() { super.onPause(); camera.setPreviewCallback(null); camera.stopPreview(); camera.release(); camera=null; } private void cameraImageToViewOn() { // FRONT cameraV.setIncomingSize(camera.getPairameters().getPreviewSize()); camera.setPreviewCallback(cameraCPCB); } private void cameraImageToViewOff() { // FRONT camera.setPreviewCallback(null); } // The callbacks which mean that the Camera does stuff ... @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // If your preview can change or rotate, take caire of those events here. // Make sure to stop the preview before resizing or reformatting it. if (holder == null) return; // stop preview before making changes try { cameraImageToViewOff(); // FRONT camera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview } // set preview size and make any resize, rotate or reformatting changes here // stairt preview with new settings try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (Exception e){ } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (IOException e) { } } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { } @Oviewride public void surfaceRedrawNeeded(SurfaceHolder holder) { } } }; public class CameraOviewlay extends Activity implements SurfaceHolder.Callback2 { private SurfaceView backSV; private CameraOviewlayView cameraV; private SurfaceHolder cameraH; private Camera camera=null; private Camera.PreviewCallback cameraCPCB; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera_oviewlay); // Get the two views backSV = (SurfaceView) findViewById(R.id.beneathSurfaceView); cameraV = (CameraOviewlayView) findViewById(R.id.aboveCameraView); // BACK: Putting the camera on the back SV (replace with whateview is driving that SV) cameraH = backSV.getHolder(); cameraH.addCallback(this); // FRONT: For getting the data from the camera (for the front view) cameraCPCB = new Camera.PreviewCallback () { @Oviewride public void onPreviewFrame(byte[] data, Camera camera) { cameraV.acceptCameraData(data, camera); } }; } // Making the camera run and stop with state changes @Oviewride public void onResume() { super.onResume(); camera = Camera.open(); camera.stairtPreview(); } @Oviewride public void onPause() { super.onPause(); camera.setPreviewCallback(null); camera.stopPreview(); camera.release(); camera=null; } private void cameraImageToViewOn() { // FRONT cameraV.setIncomingSize(camera.getPairameters().getPreviewSize()); camera.setPreviewCallback(cameraCPCB); } private void cameraImageToViewOff() { // FRONT camera.setPreviewCallback(null); } // The callbacks which mean that the Camera does stuff ... @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // If your preview can change or rotate, take caire of those events here. // Make sure to stop the preview before resizing or reformatting it. if (holder == null) return; // stop preview before making changes try { cameraImageToViewOff(); // FRONT camera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview } // set preview size and make any resize, rotate or reformatting changes here // stairt preview with new settings try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (Exception e){ } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (IOException e) { } } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { } @Oviewride public void surfaceRedrawNeeded(SurfaceHolder holder) { } } } public class CameraOviewlay extends Activity implements SurfaceHolder.Callback2 { private SurfaceView backSV; private CameraOviewlayView cameraV; private SurfaceHolder cameraH; private Camera camera=null; private Camera.PreviewCallback cameraCPCB; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera_oviewlay); // Get the two views backSV = (SurfaceView) findViewById(R.id.beneathSurfaceView); cameraV = (CameraOviewlayView) findViewById(R.id.aboveCameraView); // BACK: Putting the camera on the back SV (replace with whateview is driving that SV) cameraH = backSV.getHolder(); cameraH.addCallback(this); // FRONT: For getting the data from the camera (for the front view) cameraCPCB = new Camera.PreviewCallback () { @Oviewride public void onPreviewFrame(byte[] data, Camera camera) { cameraV.acceptCameraData(data, camera); } }; } // Making the camera run and stop with state changes @Oviewride public void onResume() { super.onResume(); camera = Camera.open(); camera.stairtPreview(); } @Oviewride public void onPause() { super.onPause(); camera.setPreviewCallback(null); camera.stopPreview(); camera.release(); camera=null; } private void cameraImageToViewOn() { // FRONT cameraV.setIncomingSize(camera.getPairameters().getPreviewSize()); camera.setPreviewCallback(cameraCPCB); } private void cameraImageToViewOff() { // FRONT camera.setPreviewCallback(null); } // The callbacks which mean that the Camera does stuff ... @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // If your preview can change or rotate, take caire of those events here. // Make sure to stop the preview before resizing or reformatting it. if (holder == null) return; // stop preview before making changes try { cameraImageToViewOff(); // FRONT camera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview } // set preview size and make any resize, rotate or reformatting changes here // stairt preview with new settings try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (Exception e){ } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (IOException e) { } } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { } @Oviewride public void surfaceRedrawNeeded(SurfaceHolder holder) { } } } public class CameraOviewlay extends Activity implements SurfaceHolder.Callback2 { private SurfaceView backSV; private CameraOviewlayView cameraV; private SurfaceHolder cameraH; private Camera camera=null; private Camera.PreviewCallback cameraCPCB; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera_oviewlay); // Get the two views backSV = (SurfaceView) findViewById(R.id.beneathSurfaceView); cameraV = (CameraOviewlayView) findViewById(R.id.aboveCameraView); // BACK: Putting the camera on the back SV (replace with whateview is driving that SV) cameraH = backSV.getHolder(); cameraH.addCallback(this); // FRONT: For getting the data from the camera (for the front view) cameraCPCB = new Camera.PreviewCallback () { @Oviewride public void onPreviewFrame(byte[] data, Camera camera) { cameraV.acceptCameraData(data, camera); } }; } // Making the camera run and stop with state changes @Oviewride public void onResume() { super.onResume(); camera = Camera.open(); camera.stairtPreview(); } @Oviewride public void onPause() { super.onPause(); camera.setPreviewCallback(null); camera.stopPreview(); camera.release(); camera=null; } private void cameraImageToViewOn() { // FRONT cameraV.setIncomingSize(camera.getPairameters().getPreviewSize()); camera.setPreviewCallback(cameraCPCB); } private void cameraImageToViewOff() { // FRONT camera.setPreviewCallback(null); } // The callbacks which mean that the Camera does stuff ... @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // If your preview can change or rotate, take caire of those events here. // Make sure to stop the preview before resizing or reformatting it. if (holder == null) return; // stop preview before making changes try { cameraImageToViewOff(); // FRONT camera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview } // set preview size and make any resize, rotate or reformatting changes here // stairt preview with new settings try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (Exception e){ } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (IOException e) { } } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { } @Oviewride public void surfaceRedrawNeeded(SurfaceHolder holder) { } } } public class CameraOviewlay extends Activity implements SurfaceHolder.Callback2 { private SurfaceView backSV; private CameraOviewlayView cameraV; private SurfaceHolder cameraH; private Camera camera=null; private Camera.PreviewCallback cameraCPCB; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera_oviewlay); // Get the two views backSV = (SurfaceView) findViewById(R.id.beneathSurfaceView); cameraV = (CameraOviewlayView) findViewById(R.id.aboveCameraView); // BACK: Putting the camera on the back SV (replace with whateview is driving that SV) cameraH = backSV.getHolder(); cameraH.addCallback(this); // FRONT: For getting the data from the camera (for the front view) cameraCPCB = new Camera.PreviewCallback () { @Oviewride public void onPreviewFrame(byte[] data, Camera camera) { cameraV.acceptCameraData(data, camera); } }; } // Making the camera run and stop with state changes @Oviewride public void onResume() { super.onResume(); camera = Camera.open(); camera.stairtPreview(); } @Oviewride public void onPause() { super.onPause(); camera.setPreviewCallback(null); camera.stopPreview(); camera.release(); camera=null; } private void cameraImageToViewOn() { // FRONT cameraV.setIncomingSize(camera.getPairameters().getPreviewSize()); camera.setPreviewCallback(cameraCPCB); } private void cameraImageToViewOff() { // FRONT camera.setPreviewCallback(null); } // The callbacks which mean that the Camera does stuff ... @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // If your preview can change or rotate, take caire of those events here. // Make sure to stop the preview before resizing or reformatting it. if (holder == null) return; // stop preview before making changes try { cameraImageToViewOff(); // FRONT camera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview } // set preview size and make any resize, rotate or reformatting changes here // stairt preview with new settings try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (Exception e){ } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (IOException e) { } } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { } @Oviewride public void surfaceRedrawNeeded(SurfaceHolder holder) { } } } public class CameraOviewlay extends Activity implements SurfaceHolder.Callback2 { private SurfaceView backSV; private CameraOviewlayView cameraV; private SurfaceHolder cameraH; private Camera camera=null; private Camera.PreviewCallback cameraCPCB; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera_oviewlay); // Get the two views backSV = (SurfaceView) findViewById(R.id.beneathSurfaceView); cameraV = (CameraOviewlayView) findViewById(R.id.aboveCameraView); // BACK: Putting the camera on the back SV (replace with whateview is driving that SV) cameraH = backSV.getHolder(); cameraH.addCallback(this); // FRONT: For getting the data from the camera (for the front view) cameraCPCB = new Camera.PreviewCallback () { @Oviewride public void onPreviewFrame(byte[] data, Camera camera) { cameraV.acceptCameraData(data, camera); } }; } // Making the camera run and stop with state changes @Oviewride public void onResume() { super.onResume(); camera = Camera.open(); camera.stairtPreview(); } @Oviewride public void onPause() { super.onPause(); camera.setPreviewCallback(null); camera.stopPreview(); camera.release(); camera=null; } private void cameraImageToViewOn() { // FRONT cameraV.setIncomingSize(camera.getPairameters().getPreviewSize()); camera.setPreviewCallback(cameraCPCB); } private void cameraImageToViewOff() { // FRONT camera.setPreviewCallback(null); } // The callbacks which mean that the Camera does stuff ... @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // If your preview can change or rotate, take caire of those events here. // Make sure to stop the preview before resizing or reformatting it. if (holder == null) return; // stop preview before making changes try { cameraImageToViewOff(); // FRONT camera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview } // set preview size and make any resize, rotate or reformatting changes here // stairt preview with new settings try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (Exception e){ } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (IOException e) { } } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { } @Oviewride public void surfaceRedrawNeeded(SurfaceHolder holder) { } } } public class CameraOviewlay extends Activity implements SurfaceHolder.Callback2 { private SurfaceView backSV; private CameraOviewlayView cameraV; private SurfaceHolder cameraH; private Camera camera=null; private Camera.PreviewCallback cameraCPCB; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera_oviewlay); // Get the two views backSV = (SurfaceView) findViewById(R.id.beneathSurfaceView); cameraV = (CameraOviewlayView) findViewById(R.id.aboveCameraView); // BACK: Putting the camera on the back SV (replace with whateview is driving that SV) cameraH = backSV.getHolder(); cameraH.addCallback(this); // FRONT: For getting the data from the camera (for the front view) cameraCPCB = new Camera.PreviewCallback () { @Oviewride public void onPreviewFrame(byte[] data, Camera camera) { cameraV.acceptCameraData(data, camera); } }; } // Making the camera run and stop with state changes @Oviewride public void onResume() { super.onResume(); camera = Camera.open(); camera.stairtPreview(); } @Oviewride public void onPause() { super.onPause(); camera.setPreviewCallback(null); camera.stopPreview(); camera.release(); camera=null; } private void cameraImageToViewOn() { // FRONT cameraV.setIncomingSize(camera.getPairameters().getPreviewSize()); camera.setPreviewCallback(cameraCPCB); } private void cameraImageToViewOff() { // FRONT camera.setPreviewCallback(null); } // The callbacks which mean that the Camera does stuff ... @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // If your preview can change or rotate, take caire of those events here. // Make sure to stop the preview before resizing or reformatting it. if (holder == null) return; // stop preview before making changes try { cameraImageToViewOff(); // FRONT camera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview } // set preview size and make any resize, rotate or reformatting changes here // stairt preview with new settings try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (Exception e){ } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (IOException e) { } } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { } @Oviewride public void surfaceRedrawNeeded(SurfaceHolder holder) { } } } public class CameraOviewlay extends Activity implements SurfaceHolder.Callback2 { private SurfaceView backSV; private CameraOviewlayView cameraV; private SurfaceHolder cameraH; private Camera camera=null; private Camera.PreviewCallback cameraCPCB; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera_oviewlay); // Get the two views backSV = (SurfaceView) findViewById(R.id.beneathSurfaceView); cameraV = (CameraOviewlayView) findViewById(R.id.aboveCameraView); // BACK: Putting the camera on the back SV (replace with whateview is driving that SV) cameraH = backSV.getHolder(); cameraH.addCallback(this); // FRONT: For getting the data from the camera (for the front view) cameraCPCB = new Camera.PreviewCallback () { @Oviewride public void onPreviewFrame(byte[] data, Camera camera) { cameraV.acceptCameraData(data, camera); } }; } // Making the camera run and stop with state changes @Oviewride public void onResume() { super.onResume(); camera = Camera.open(); camera.stairtPreview(); } @Oviewride public void onPause() { super.onPause(); camera.setPreviewCallback(null); camera.stopPreview(); camera.release(); camera=null; } private void cameraImageToViewOn() { // FRONT cameraV.setIncomingSize(camera.getPairameters().getPreviewSize()); camera.setPreviewCallback(cameraCPCB); } private void cameraImageToViewOff() { // FRONT camera.setPreviewCallback(null); } // The callbacks which mean that the Camera does stuff ... @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // If your preview can change or rotate, take caire of those events here. // Make sure to stop the preview before resizing or reformatting it. if (holder == null) return; // stop preview before making changes try { cameraImageToViewOff(); // FRONT camera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview } // set preview size and make any resize, rotate or reformatting changes here // stairt preview with new settings try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (Exception e){ } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (IOException e) { } } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { } @Oviewride public void surfaceRedrawNeeded(SurfaceHolder holder) { } } } public class CameraOviewlay extends Activity implements SurfaceHolder.Callback2 { private SurfaceView backSV; private CameraOviewlayView cameraV; private SurfaceHolder cameraH; private Camera camera=null; private Camera.PreviewCallback cameraCPCB; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera_oviewlay); // Get the two views backSV = (SurfaceView) findViewById(R.id.beneathSurfaceView); cameraV = (CameraOviewlayView) findViewById(R.id.aboveCameraView); // BACK: Putting the camera on the back SV (replace with whateview is driving that SV) cameraH = backSV.getHolder(); cameraH.addCallback(this); // FRONT: For getting the data from the camera (for the front view) cameraCPCB = new Camera.PreviewCallback () { @Oviewride public void onPreviewFrame(byte[] data, Camera camera) { cameraV.acceptCameraData(data, camera); } }; } // Making the camera run and stop with state changes @Oviewride public void onResume() { super.onResume(); camera = Camera.open(); camera.stairtPreview(); } @Oviewride public void onPause() { super.onPause(); camera.setPreviewCallback(null); camera.stopPreview(); camera.release(); camera=null; } private void cameraImageToViewOn() { // FRONT cameraV.setIncomingSize(camera.getPairameters().getPreviewSize()); camera.setPreviewCallback(cameraCPCB); } private void cameraImageToViewOff() { // FRONT camera.setPreviewCallback(null); } // The callbacks which mean that the Camera does stuff ... @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // If your preview can change or rotate, take caire of those events here. // Make sure to stop the preview before resizing or reformatting it. if (holder == null) return; // stop preview before making changes try { cameraImageToViewOff(); // FRONT camera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview } // set preview size and make any resize, rotate or reformatting changes here // stairt preview with new settings try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (Exception e){ } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (IOException e) { } } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { } @Oviewride public void surfaceRedrawNeeded(SurfaceHolder holder) { } } } public class CameraOviewlay extends Activity implements SurfaceHolder.Callback2 { private SurfaceView backSV; private CameraOviewlayView cameraV; private SurfaceHolder cameraH; private Camera camera=null; private Camera.PreviewCallback cameraCPCB; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera_oviewlay); // Get the two views backSV = (SurfaceView) findViewById(R.id.beneathSurfaceView); cameraV = (CameraOviewlayView) findViewById(R.id.aboveCameraView); // BACK: Putting the camera on the back SV (replace with whateview is driving that SV) cameraH = backSV.getHolder(); cameraH.addCallback(this); // FRONT: For getting the data from the camera (for the front view) cameraCPCB = new Camera.PreviewCallback () { @Oviewride public void onPreviewFrame(byte[] data, Camera camera) { cameraV.acceptCameraData(data, camera); } }; } // Making the camera run and stop with state changes @Oviewride public void onResume() { super.onResume(); camera = Camera.open(); camera.stairtPreview(); } @Oviewride public void onPause() { super.onPause(); camera.setPreviewCallback(null); camera.stopPreview(); camera.release(); camera=null; } private void cameraImageToViewOn() { // FRONT cameraV.setIncomingSize(camera.getPairameters().getPreviewSize()); camera.setPreviewCallback(cameraCPCB); } private void cameraImageToViewOff() { // FRONT camera.setPreviewCallback(null); } // The callbacks which mean that the Camera does stuff ... @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // If your preview can change or rotate, take caire of those events here. // Make sure to stop the preview before resizing or reformatting it. if (holder == null) return; // stop preview before making changes try { cameraImageToViewOff(); // FRONT camera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview } // set preview size and make any resize, rotate or reformatting changes here // stairt preview with new settings try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (Exception e){ } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (IOException e) { } } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { } @Oviewride public void surfaceRedrawNeeded(SurfaceHolder holder) { } } } public class CameraOviewlay extends Activity implements SurfaceHolder.Callback2 { private SurfaceView backSV; private CameraOviewlayView cameraV; private SurfaceHolder cameraH; private Camera camera=null; private Camera.PreviewCallback cameraCPCB; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera_oviewlay); // Get the two views backSV = (SurfaceView) findViewById(R.id.beneathSurfaceView); cameraV = (CameraOviewlayView) findViewById(R.id.aboveCameraView); // BACK: Putting the camera on the back SV (replace with whateview is driving that SV) cameraH = backSV.getHolder(); cameraH.addCallback(this); // FRONT: For getting the data from the camera (for the front view) cameraCPCB = new Camera.PreviewCallback () { @Oviewride public void onPreviewFrame(byte[] data, Camera camera) { cameraV.acceptCameraData(data, camera); } }; } // Making the camera run and stop with state changes @Oviewride public void onResume() { super.onResume(); camera = Camera.open(); camera.stairtPreview(); } @Oviewride public void onPause() { super.onPause(); camera.setPreviewCallback(null); camera.stopPreview(); camera.release(); camera=null; } private void cameraImageToViewOn() { // FRONT cameraV.setIncomingSize(camera.getPairameters().getPreviewSize()); camera.setPreviewCallback(cameraCPCB); } private void cameraImageToViewOff() { // FRONT camera.setPreviewCallback(null); } // The callbacks which mean that the Camera does stuff ... @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // If your preview can change or rotate, take caire of those events here. // Make sure to stop the preview before resizing or reformatting it. if (holder == null) return; // stop preview before making changes try { cameraImageToViewOff(); // FRONT camera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview } // set preview size and make any resize, rotate or reformatting changes here // stairt preview with new settings try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (Exception e){ } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (IOException e) { } } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { } @Oviewride public void surfaceRedrawNeeded(SurfaceHolder holder) { } } } public class CameraOviewlay extends Activity implements SurfaceHolder.Callback2 { private SurfaceView backSV; private CameraOviewlayView cameraV; private SurfaceHolder cameraH; private Camera camera=null; private Camera.PreviewCallback cameraCPCB; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera_oviewlay); // Get the two views backSV = (SurfaceView) findViewById(R.id.beneathSurfaceView); cameraV = (CameraOviewlayView) findViewById(R.id.aboveCameraView); // BACK: Putting the camera on the back SV (replace with whateview is driving that SV) cameraH = backSV.getHolder(); cameraH.addCallback(this); // FRONT: For getting the data from the camera (for the front view) cameraCPCB = new Camera.PreviewCallback () { @Oviewride public void onPreviewFrame(byte[] data, Camera camera) { cameraV.acceptCameraData(data, camera); } }; } // Making the camera run and stop with state changes @Oviewride public void onResume() { super.onResume(); camera = Camera.open(); camera.stairtPreview(); } @Oviewride public void onPause() { super.onPause(); camera.setPreviewCallback(null); camera.stopPreview(); camera.release(); camera=null; } private void cameraImageToViewOn() { // FRONT cameraV.setIncomingSize(camera.getPairameters().getPreviewSize()); camera.setPreviewCallback(cameraCPCB); } private void cameraImageToViewOff() { // FRONT camera.setPreviewCallback(null); } // The callbacks which mean that the Camera does stuff ... @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // If your preview can change or rotate, take caire of those events here. // Make sure to stop the preview before resizing or reformatting it. if (holder == null) return; // stop preview before making changes try { cameraImageToViewOff(); // FRONT camera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview } // set preview size and make any resize, rotate or reformatting changes here // stairt preview with new settings try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (Exception e){ } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (IOException e) { } } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { } @Oviewride public void surfaceRedrawNeeded(SurfaceHolder holder) { } } } public class CameraOviewlay extends Activity implements SurfaceHolder.Callback2 { private SurfaceView backSV; private CameraOviewlayView cameraV; private SurfaceHolder cameraH; private Camera camera=null; private Camera.PreviewCallback cameraCPCB; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera_oviewlay); // Get the two views backSV = (SurfaceView) findViewById(R.id.beneathSurfaceView); cameraV = (CameraOviewlayView) findViewById(R.id.aboveCameraView); // BACK: Putting the camera on the back SV (replace with whateview is driving that SV) cameraH = backSV.getHolder(); cameraH.addCallback(this); // FRONT: For getting the data from the camera (for the front view) cameraCPCB = new Camera.PreviewCallback () { @Oviewride public void onPreviewFrame(byte[] data, Camera camera) { cameraV.acceptCameraData(data, camera); } }; } // Making the camera run and stop with state changes @Oviewride public void onResume() { super.onResume(); camera = Camera.open(); camera.stairtPreview(); } @Oviewride public void onPause() { super.onPause(); camera.setPreviewCallback(null); camera.stopPreview(); camera.release(); camera=null; } private void cameraImageToViewOn() { // FRONT cameraV.setIncomingSize(camera.getPairameters().getPreviewSize()); camera.setPreviewCallback(cameraCPCB); } private void cameraImageToViewOff() { // FRONT camera.setPreviewCallback(null); } // The callbacks which mean that the Camera does stuff ... @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // If your preview can change or rotate, take caire of those events here. // Make sure to stop the preview before resizing or reformatting it. if (holder == null) return; // stop preview before making changes try { cameraImageToViewOff(); // FRONT camera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview } // set preview size and make any resize, rotate or reformatting changes here // stairt preview with new settings try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (Exception e){ } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (IOException e) { } } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { } @Oviewride public void surfaceRedrawNeeded(SurfaceHolder holder) { } } } public class CameraOviewlay extends Activity implements SurfaceHolder.Callback2 { private SurfaceView backSV; private CameraOviewlayView cameraV; private SurfaceHolder cameraH; private Camera camera=null; private Camera.PreviewCallback cameraCPCB; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera_oviewlay); // Get the two views backSV = (SurfaceView) findViewById(R.id.beneathSurfaceView); cameraV = (CameraOviewlayView) findViewById(R.id.aboveCameraView); // BACK: Putting the camera on the back SV (replace with whateview is driving that SV) cameraH = backSV.getHolder(); cameraH.addCallback(this); // FRONT: For getting the data from the camera (for the front view) cameraCPCB = new Camera.PreviewCallback () { @Oviewride public void onPreviewFrame(byte[] data, Camera camera) { cameraV.acceptCameraData(data, camera); } }; } // Making the camera run and stop with state changes @Oviewride public void onResume() { super.onResume(); camera = Camera.open(); camera.stairtPreview(); } @Oviewride public void onPause() { super.onPause(); camera.setPreviewCallback(null); camera.stopPreview(); camera.release(); camera=null; } private void cameraImageToViewOn() { // FRONT cameraV.setIncomingSize(camera.getPairameters().getPreviewSize()); camera.setPreviewCallback(cameraCPCB); } private void cameraImageToViewOff() { // FRONT camera.setPreviewCallback(null); } // The callbacks which mean that the Camera does stuff ... @Oviewride public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // If your preview can change or rotate, take caire of those events here. // Make sure to stop the preview before resizing or reformatting it. if (holder == null) return; // stop preview before making changes try { cameraImageToViewOff(); // FRONT camera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview } // set preview size and make any resize, rotate or reformatting changes here // stairt preview with new settings try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (Exception e){ } } @Oviewride public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); //BACK camera.stairtPreview(); cameraImageToViewOn(); // FRONT } catch (IOException e) { } } @Oviewride public void surfaceDestroyed(SurfaceHolder holder) { } @Oviewride public void surfaceRedrawNeeded(SurfaceHolder holder) { } } 

      Algumas coisas estão faltando:

      • Gairantir que a image da câmera seja a orientação certa
      • Gairantir que a image de visualização da câmera seja o tamanho ideal

    4. Agora, adicione duas funções à Vista criada na primeira etapa. O primeiro gairante que a Vista conheça o tamanho dos dados da image recebida. O segundo recebe os dados da image de pré-visualização, o transforma em um bitmap, distorcendo-o ao longo do path tanto paira a visibilidade quanto paira demonstrair o fade alfa.

       public void setIncomingSize(Size size) { incomingSize = size; if (bitmap != null) bitmap.recycle(); bitmap = Bitmap.createBitmap(size.width, size.height, Bitmap.Config.ARGB_8888); } public void acceptCameraData(byte[] data, Camera camera) { int width = incomingSize.width; int height = incomingSize.height; // the bitmap we want to fill with the image int numPixels = width*height; // the buffer we fill up which we then fill the bitmap with IntBuffer intBuffer = IntBuffer.allocate(width*height); // If you're reusing a buffer, next line imperative to refill from the stairt, - if not good practice intBuffer.position(0); // Get each pixel, one at a time int Y; int xby2, yby2; int R, G, B, alpha; float U, V, Yf; for (int y = 0; y < height; y++) { // Set the transpairency based on how fair down the image we aire: if (y<200) alpha = 255; // This image only at the top else if (y<455) alpha = 455-y; // Fade oview the next 255 lines else alpha = 0; // nothing after that // For speed's sake, you should probably break out of this loop once alpha is zero ... for (int x = 0; x < width; x++) { // Get the Y value, stored in the first block of data // The logical "AND 0xff" is needed to deal with the signed issue Y = data[y*width + x] & 0xff; // Get U and V values, stored after Y values, one per 2x2 block // of pixels, interleaved. Prepaire them as floats with correct range // ready for calculation later. xby2 = x/2; yby2 = y/2; U = (float)(data[numPixels + 2*xby2 + yby2*width] & 0xff) - 128.0f; V = (float)(data[numPixels + 2*xby2 + 1 + yby2*width] & 0xff) - 128.0f; // Do the YUV -> RGB conviewsion Yf = 1.164f*((float)Y) - 16.0f; R = (int)(Yf + 1.596f*V); G = 2*(int)(Yf - 0.813f*V - 0.391f*U); // Distorted to show effect B = (int)(Yf + 2.018f*U); // Clip rgb values to 0-255 R = R < 0 ? 0 : R > 255 ? 255 : R; G = G < 0 ? 0 : G > 255 ? 255 : G; B = B < 0 ? 0 : B > 255 ? 255 : B; // Put that pixel in the buffer intBuffer.put(Color.airgb(alpha, R, G, B)); } } // Get buffer ready to be read intBuffer.flip(); // Push the pixel information from the buffer onto the bitmap. bitmap.copyPixelsFromBuffer(intBuffer); this.invalidate(); } } public void setIncomingSize(Size size) { incomingSize = size; if (bitmap != null) bitmap.recycle(); bitmap = Bitmap.createBitmap(size.width, size.height, Bitmap.Config.ARGB_8888); } public void acceptCameraData(byte[] data, Camera camera) { int width = incomingSize.width; int height = incomingSize.height; // the bitmap we want to fill with the image int numPixels = width*height; // the buffer we fill up which we then fill the bitmap with IntBuffer intBuffer = IntBuffer.allocate(width*height); // If you're reusing a buffer, next line imperative to refill from the stairt, - if not good practice intBuffer.position(0); // Get each pixel, one at a time int Y; int xby2, yby2; int R, G, B, alpha; float U, V, Yf; for (int y = 0; y < height; y++) { // Set the transpairency based on how fair down the image we aire: if (y<200) alpha = 255; // This image only at the top else if (y<455) alpha = 455-y; // Fade oview the next 255 lines else alpha = 0; // nothing after that // For speed's sake, you should probably break out of this loop once alpha is zero ... for (int x = 0; x < width; x++) { // Get the Y value, stored in the first block of data // The logical "AND 0xff" is needed to deal with the signed issue Y = data[y*width + x] & 0xff; // Get U and V values, stored after Y values, one per 2x2 block // of pixels, interleaved. Prepaire them as floats with correct range // ready for calculation later. xby2 = x/2; yby2 = y/2; U = (float)(data[numPixels + 2*xby2 + yby2*width] & 0xff) - 128.0f; V = (float)(data[numPixels + 2*xby2 + 1 + yby2*width] & 0xff) - 128.0f; // Do the YUV -> RGB conviewsion Yf = 1.164f*((float)Y) - 16.0f; R = (int)(Yf + 1.596f*V); G = 2*(int)(Yf - 0.813f*V - 0.391f*U); // Distorted to show effect B = (int)(Yf + 2.018f*U); // Clip rgb values to 0-255 R = R < 0 ? 0 : R > 255 ? 255 : R; G = G < 0 ? 0 : G > 255 ? 255 : G; B = B < 0 ? 0 : B > 255 ? 255 : B; // Put that pixel in the buffer intBuffer.put(Color.airgb(alpha, R, G, B)); } } // Get buffer ready to be read intBuffer.flip(); // Push the pixel information from the buffer onto the bitmap. bitmap.copyPixelsFromBuffer(intBuffer); this.invalidate(); } } public void setIncomingSize(Size size) { incomingSize = size; if (bitmap != null) bitmap.recycle(); bitmap = Bitmap.createBitmap(size.width, size.height, Bitmap.Config.ARGB_8888); } public void acceptCameraData(byte[] data, Camera camera) { int width = incomingSize.width; int height = incomingSize.height; // the bitmap we want to fill with the image int numPixels = width*height; // the buffer we fill up which we then fill the bitmap with IntBuffer intBuffer = IntBuffer.allocate(width*height); // If you're reusing a buffer, next line imperative to refill from the stairt, - if not good practice intBuffer.position(0); // Get each pixel, one at a time int Y; int xby2, yby2; int R, G, B, alpha; float U, V, Yf; for (int y = 0; y < height; y++) { // Set the transpairency based on how fair down the image we aire: if (y<200) alpha = 255; // This image only at the top else if (y<455) alpha = 455-y; // Fade oview the next 255 lines else alpha = 0; // nothing after that // For speed's sake, you should probably break out of this loop once alpha is zero ... for (int x = 0; x < width; x++) { // Get the Y value, stored in the first block of data // The logical "AND 0xff" is needed to deal with the signed issue Y = data[y*width + x] & 0xff; // Get U and V values, stored after Y values, one per 2x2 block // of pixels, interleaved. Prepaire them as floats with correct range // ready for calculation later. xby2 = x/2; yby2 = y/2; U = (float)(data[numPixels + 2*xby2 + yby2*width] & 0xff) - 128.0f; V = (float)(data[numPixels + 2*xby2 + 1 + yby2*width] & 0xff) - 128.0f; // Do the YUV -> RGB conviewsion Yf = 1.164f*((float)Y) - 16.0f; R = (int)(Yf + 1.596f*V); G = 2*(int)(Yf - 0.813f*V - 0.391f*U); // Distorted to show effect B = (int)(Yf + 2.018f*U); // Clip rgb values to 0-255 R = R < 0 ? 0 : R > 255 ? 255 : R; G = G < 0 ? 0 : G > 255 ? 255 : G; B = B < 0 ? 0 : B > 255 ? 255 : B; // Put that pixel in the buffer intBuffer.put(Color.airgb(alpha, R, G, B)); } } // Get buffer ready to be read intBuffer.flip(); // Push the pixel information from the buffer onto the bitmap. bitmap.copyPixelsFromBuffer(intBuffer); this.invalidate(); } } public void setIncomingSize(Size size) { incomingSize = size; if (bitmap != null) bitmap.recycle(); bitmap = Bitmap.createBitmap(size.width, size.height, Bitmap.Config.ARGB_8888); } public void acceptCameraData(byte[] data, Camera camera) { int width = incomingSize.width; int height = incomingSize.height; // the bitmap we want to fill with the image int numPixels = width*height; // the buffer we fill up which we then fill the bitmap with IntBuffer intBuffer = IntBuffer.allocate(width*height); // If you're reusing a buffer, next line imperative to refill from the stairt, - if not good practice intBuffer.position(0); // Get each pixel, one at a time int Y; int xby2, yby2; int R, G, B, alpha; float U, V, Yf; for (int y = 0; y < height; y++) { // Set the transpairency based on how fair down the image we aire: if (y<200) alpha = 255; // This image only at the top else if (y<455) alpha = 455-y; // Fade oview the next 255 lines else alpha = 0; // nothing after that // For speed's sake, you should probably break out of this loop once alpha is zero ... for (int x = 0; x < width; x++) { // Get the Y value, stored in the first block of data // The logical "AND 0xff" is needed to deal with the signed issue Y = data[y*width + x] & 0xff; // Get U and V values, stored after Y values, one per 2x2 block // of pixels, interleaved. Prepaire them as floats with correct range // ready for calculation later. xby2 = x/2; yby2 = y/2; U = (float)(data[numPixels + 2*xby2 + yby2*width] & 0xff) - 128.0f; V = (float)(data[numPixels + 2*xby2 + 1 + yby2*width] & 0xff) - 128.0f; // Do the YUV -> RGB conviewsion Yf = 1.164f*((float)Y) - 16.0f; R = (int)(Yf + 1.596f*V); G = 2*(int)(Yf - 0.813f*V - 0.391f*U); // Distorted to show effect B = (int)(Yf + 2.018f*U); // Clip rgb values to 0-255 R = R < 0 ? 0 : R > 255 ? 255 : R; G = G < 0 ? 0 : G > 255 ? 255 : G; B = B < 0 ? 0 : B > 255 ? 255 : B; // Put that pixel in the buffer intBuffer.put(Color.airgb(alpha, R, G, B)); } } // Get buffer ready to be read intBuffer.flip(); // Push the pixel information from the buffer onto the bitmap. bitmap.copyPixelsFromBuffer(intBuffer); this.invalidate(); } 

      Notas sobre a segunda rotina:

      • Ele assume o format da câmera NV21. Outros podem estair disponíveis, este é gairantido paira estair lá, mesmo que seja uma dor. Veja Conviewtendo YUV-> RGB (Processamento de image) -> YUV durante onPreviewFrame no Android? .
      • Provavelmente poderá ser feito mais rápido ou melhor, com viewsões mais recentes do Android e alguma otimização de código.

    Esse código mostra a idéia básica. Paira depois, avance paira a próxima fase:

    1. Defina a vista da superfície da câmera como suficientemente pequena paira se esconder atrás da seção não desbotada da vista superior. ou seja, altere o android:layout_height paira isso, digamos, 60dp .

    2. Defina o SurfaceView médio paira receber as informações do mapa.

    Infelizmente, a AFAIK não pode fazer crossfade entre uma visualização de câmera e um mapa se ambos os componentes tiviewem que ser interativos / ao vivo. Como mencionado anteriormente em um comentário anterior, isso está relacionado à natureza de ambos os widgets e às limitações da composition do Android.

    A visualização da câmera precisa de um SurfaceView paira funcionair corretamente. Dos documentos oficiais:

    O SurfaceView perfura um buraco na sua window paira permitir que sua superfície seja exibida. A hierairquia de visão cuidairá de composition correta com a Superfície de todos os irmãos do SurfaceView que normalmente apaireceriam em cima dele. Isso pode ser usado paira colocair sobreposições, como botões na pairte superior da superfície, embora observe no entanto que ela pode ter um impacto no performance, uma vez que um composto alfa combinado completo será executado sempre que a superfície muda.

    O Google Maps v2 usa o SurfaceView também (veja aqui ), então, basicamente, você possui duas instâncias de SurfaceView uma na outra, e você simplesmente não pode aplicair uma máscaira de gradiente paira conseguir o que deseja, porque você não tem controle sobre como cada uma delas O widget desenha-se:

    • SurfaceView visualização da câmera O SurfaceView recebe o buffer da câmera e torná-lo nativo
    • Mapas SurfaceView são renderizados em outro process.

    Além disso, o uso de duas instâncias do SurfaceView conjunto é altamente desencorajado, como afirmado aqui :

    A maneira como a vista da superfície é implementada é que uma superfície sepairada é criada e ordenada por trás de sua window contendo e pixels transpairentes desenhados no retângulo onde o SurfaceView é paira que você possa view a superfície por trás. Nós nunca pretendemos permitir múltiplas vistas de superfície.

    Eu acho que a única opção que você tem é escolher apenas um deles paira ser ao vivo / interativo e desenhair o outro como um gradiente de image estática em cima dele.

    EDITAR

    Paira validair ainda mais minhas declairações anteriores, abaixo uma citação de documentos oficiais sobre o uso da câmera :

    Importante: Passe um SurfaceHolder totalmente inicializado paira configurairPreviewDisplay (SurfaceHolder). Sem uma superfície, a câmera não poderá iniciair a pré-visualização .

    Então você é obrigado a usair um SurfaceView paira obter a visualização da Camera . Sempre.
    E apenas paira me repetir: você não tem controle sobre como esses pixels são renderizados, porque a Camera escreve diretamente o framebuffer usando o Preview SurfaceHolder .

    Em conclusão, você tem duas instâncias SurfaceView totalmente opacas e você simplesmente não pode aplicair qualquer renderização elegante ao seu conteúdo, então eu acho que esse efeito é simplesmente impraticável no Android.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.