Guest User

Untitled

a guest
Aug 19th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. How can o draw something on a surface view?
  2. //this is the preview class
  3. package my.pack;
  4.  
  5. import java.io.IOException;
  6.  
  7. import android.content.Context;
  8. import android.hardware.Camera;
  9. import android.hardware.Camera.PreviewCallback;
  10. import android.util.Log;
  11. import android.view.SurfaceHolder;
  12. import android.view.SurfaceView;
  13.  
  14. class Preview extends SurfaceView implements SurfaceHolder.Callback {
  15. private static final String TAG = "Preview";
  16.  
  17. SurfaceHolder mHolder;
  18. public Camera camera;
  19.  
  20. Preview(Context context) {
  21. super(context);
  22.  
  23. mHolder = getHolder();
  24. mHolder.addCallback(this);
  25. //
  26. mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  27. }
  28.  
  29.  
  30. public void surfaceCreated(SurfaceHolder holder) {
  31.  
  32. camera = Camera.open();
  33. try {
  34. camera.setPreviewDisplay(holder);
  35.  
  36. camera.setPreviewCallback(new PreviewCallback() {
  37.  
  38. public void onPreviewFrame(byte[] data, Camera camera) { // <11>
  39. Log.d(TAG, "onPreviewFrame called at: " + System.currentTimeMillis());
  40. Preview.this.invalidate();
  41. }
  42. });
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47.  
  48.  
  49. public void surfaceDestroyed(SurfaceHolder holder) {
  50. camera.stopPreview();
  51. camera = null;
  52. }
  53.  
  54.  
  55. public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
  56. camera.startPreview();
  57. }
  58.  
  59. }
  60.  
  61. //this is how i added views on thw preview
  62. mFrameLayout = (FrameLayout) findViewById(R.id.preview);
  63. // mLinearLayout = new LinearLayout(this);
  64. // mLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  65. mPreview = new Preview(this);
  66. sats = getResources().getStringArray(R.array.satellites);
  67. mMap = new HashMap<Integer,String>();
  68. mListView = new ListView(this);
  69. mListView.setLayoutParams(new LayoutParams(400, 250));
  70. mListView.setFastScrollEnabled(true);
  71. mListView.setLayoutParams(new LayoutParams(320, 270));
  72. mTextViewPhoneCoordinates = new TextView(this);
  73. mTextViewPhoneCoordinates.setGravity(1);
  74. mTextViewPhoneCoordinates.setTextColor(Color.RED);
  75. //mToggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
  76. mArrayList = new ArrayList<String>();
  77. mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mArrayList);
  78.  
  79. mFrameLayout.addView(mPreview);
  80. mFrameLayout.addView(mListView);
  81. mFrameLayout.addView(mTextViewPhoneCoordinates);
Add Comment
Please, Sign In to add comment