How to create camera frame in drawable in android












0















In my requirement, I have to show a frame on camera view to align face to user like below image:



enter image description here



I am able to create a drawable through xml but not able to achieve transparency for inner circle so camera can be seen clearly. I am able to create below drawable:



enter image description here



By using this simple xml:



<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#44000000" />
<padding
android:bottom="50dp"
android:left="20dp"
android:right="20dp"
android:top="50dp" />
</shape>
</item>

<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#ffffff" />
</shape>
</item>
</layer-list>


Is there any way to create such type of frame or layer in drawable?










share|improve this question



























    0















    In my requirement, I have to show a frame on camera view to align face to user like below image:



    enter image description here



    I am able to create a drawable through xml but not able to achieve transparency for inner circle so camera can be seen clearly. I am able to create below drawable:



    enter image description here



    By using this simple xml:



    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#44000000" />
    <padding
    android:bottom="50dp"
    android:left="20dp"
    android:right="20dp"
    android:top="50dp" />
    </shape>
    </item>

    <item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#ffffff" />
    </shape>
    </item>
    </layer-list>


    Is there any way to create such type of frame or layer in drawable?










    share|improve this question

























      0












      0








      0








      In my requirement, I have to show a frame on camera view to align face to user like below image:



      enter image description here



      I am able to create a drawable through xml but not able to achieve transparency for inner circle so camera can be seen clearly. I am able to create below drawable:



      enter image description here



      By using this simple xml:



      <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      <item>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle">
      <solid android:color="#44000000" />
      <padding
      android:bottom="50dp"
      android:left="20dp"
      android:right="20dp"
      android:top="50dp" />
      </shape>
      </item>

      <item>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="oval">
      <solid android:color="#ffffff" />
      </shape>
      </item>
      </layer-list>


      Is there any way to create such type of frame or layer in drawable?










      share|improve this question














      In my requirement, I have to show a frame on camera view to align face to user like below image:



      enter image description here



      I am able to create a drawable through xml but not able to achieve transparency for inner circle so camera can be seen clearly. I am able to create below drawable:



      enter image description here



      By using this simple xml:



      <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      <item>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle">
      <solid android:color="#44000000" />
      <padding
      android:bottom="50dp"
      android:left="20dp"
      android:right="20dp"
      android:top="50dp" />
      </shape>
      </item>

      <item>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="oval">
      <solid android:color="#ffffff" />
      </shape>
      </item>
      </layer-list>


      Is there any way to create such type of frame or layer in drawable?







      android-layout android-drawable






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 13:24









      Akshay PaliwalAkshay Paliwal

      2,20912437




      2,20912437
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Use like this made the center area transparent but cant make it oval. This may help you.



          <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
          <item>
          <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle">
          <solid android:color="#00000000" />
          <stroke android:width="100dp"
          android:color="#e6e6e6"></stroke>
          <padding
          android:bottom="50dp"
          android:left="20dp"
          android:right="20dp"
          android:top="50dp" />
          </shape>
          </item>

          </layer-list>





          share|improve this answer
























          • Thanks for your efforts, but this gives me the same situation I mentioned

            – Akshay Paliwal
            Jan 3 at 14:02











          • Is it not transparent in center

            – Jarin Rocks
            Jan 3 at 14:13











          • Yes this is transparent but it gives padding to whole view like this image: imgur.com/1i6SAh8

            – Akshay Paliwal
            Jan 4 at 5:25



















          0














          I got a solution, but not via xml drawable. We can create a CustomView by extending View class which can perform same as required:



          public class RadiusOverlayView extends View {

          Bitmap bm;
          Canvas cv;
          Paint eraser;

          public RadiusOverlayView(Context context) {
          super(context);
          Init();
          }

          public RadiusOverlayView(Context context, AttributeSet attrs) {
          super(context, attrs);
          Init();
          }

          public RadiusOverlayView(Context context, AttributeSet attrs,
          int defStyleAttr) {
          super(context, attrs, defStyleAttr);
          Init();
          }

          private void Init() {

          eraser = new Paint();
          eraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
          eraser.setAntiAlias(true);
          }

          @Override
          protected void onSizeChanged(int w, int h, int oldw, int oldh) {

          if (w != oldw || h != oldh) {
          bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
          cv = new Canvas(bm);
          }
          super.onSizeChanged(w, h, oldw, oldh);
          }

          @Override
          protected void onDraw(Canvas canvas) {

          int w = getWidth();
          int h = getHeight();
          int radius = w > h ? h / 2 : w / 2;

          bm.eraseColor(Color.TRANSPARENT);
          cv.drawColor(getResources().getColor(R.color.colorPrimary));
          cv.drawCircle(w / 2, h / 2, radius, eraser);
          canvas.drawBitmap(bm, 0, 0, null);
          super.onDraw(canvas);
          }
          }


          Add this view as a layer on your camera view.



          Thanks






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007180%2fhow-to-create-camera-frame-in-drawable-in-android%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Use like this made the center area transparent but cant make it oval. This may help you.



            <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            <item>
            <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="#00000000" />
            <stroke android:width="100dp"
            android:color="#e6e6e6"></stroke>
            <padding
            android:bottom="50dp"
            android:left="20dp"
            android:right="20dp"
            android:top="50dp" />
            </shape>
            </item>

            </layer-list>





            share|improve this answer
























            • Thanks for your efforts, but this gives me the same situation I mentioned

              – Akshay Paliwal
              Jan 3 at 14:02











            • Is it not transparent in center

              – Jarin Rocks
              Jan 3 at 14:13











            • Yes this is transparent but it gives padding to whole view like this image: imgur.com/1i6SAh8

              – Akshay Paliwal
              Jan 4 at 5:25
















            1














            Use like this made the center area transparent but cant make it oval. This may help you.



            <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            <item>
            <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="#00000000" />
            <stroke android:width="100dp"
            android:color="#e6e6e6"></stroke>
            <padding
            android:bottom="50dp"
            android:left="20dp"
            android:right="20dp"
            android:top="50dp" />
            </shape>
            </item>

            </layer-list>





            share|improve this answer
























            • Thanks for your efforts, but this gives me the same situation I mentioned

              – Akshay Paliwal
              Jan 3 at 14:02











            • Is it not transparent in center

              – Jarin Rocks
              Jan 3 at 14:13











            • Yes this is transparent but it gives padding to whole view like this image: imgur.com/1i6SAh8

              – Akshay Paliwal
              Jan 4 at 5:25














            1












            1








            1







            Use like this made the center area transparent but cant make it oval. This may help you.



            <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            <item>
            <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="#00000000" />
            <stroke android:width="100dp"
            android:color="#e6e6e6"></stroke>
            <padding
            android:bottom="50dp"
            android:left="20dp"
            android:right="20dp"
            android:top="50dp" />
            </shape>
            </item>

            </layer-list>





            share|improve this answer













            Use like this made the center area transparent but cant make it oval. This may help you.



            <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            <item>
            <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="#00000000" />
            <stroke android:width="100dp"
            android:color="#e6e6e6"></stroke>
            <padding
            android:bottom="50dp"
            android:left="20dp"
            android:right="20dp"
            android:top="50dp" />
            </shape>
            </item>

            </layer-list>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 2 at 14:32









            Jarin RocksJarin Rocks

            16919




            16919













            • Thanks for your efforts, but this gives me the same situation I mentioned

              – Akshay Paliwal
              Jan 3 at 14:02











            • Is it not transparent in center

              – Jarin Rocks
              Jan 3 at 14:13











            • Yes this is transparent but it gives padding to whole view like this image: imgur.com/1i6SAh8

              – Akshay Paliwal
              Jan 4 at 5:25



















            • Thanks for your efforts, but this gives me the same situation I mentioned

              – Akshay Paliwal
              Jan 3 at 14:02











            • Is it not transparent in center

              – Jarin Rocks
              Jan 3 at 14:13











            • Yes this is transparent but it gives padding to whole view like this image: imgur.com/1i6SAh8

              – Akshay Paliwal
              Jan 4 at 5:25

















            Thanks for your efforts, but this gives me the same situation I mentioned

            – Akshay Paliwal
            Jan 3 at 14:02





            Thanks for your efforts, but this gives me the same situation I mentioned

            – Akshay Paliwal
            Jan 3 at 14:02













            Is it not transparent in center

            – Jarin Rocks
            Jan 3 at 14:13





            Is it not transparent in center

            – Jarin Rocks
            Jan 3 at 14:13













            Yes this is transparent but it gives padding to whole view like this image: imgur.com/1i6SAh8

            – Akshay Paliwal
            Jan 4 at 5:25





            Yes this is transparent but it gives padding to whole view like this image: imgur.com/1i6SAh8

            – Akshay Paliwal
            Jan 4 at 5:25













            0














            I got a solution, but not via xml drawable. We can create a CustomView by extending View class which can perform same as required:



            public class RadiusOverlayView extends View {

            Bitmap bm;
            Canvas cv;
            Paint eraser;

            public RadiusOverlayView(Context context) {
            super(context);
            Init();
            }

            public RadiusOverlayView(Context context, AttributeSet attrs) {
            super(context, attrs);
            Init();
            }

            public RadiusOverlayView(Context context, AttributeSet attrs,
            int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            Init();
            }

            private void Init() {

            eraser = new Paint();
            eraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            eraser.setAntiAlias(true);
            }

            @Override
            protected void onSizeChanged(int w, int h, int oldw, int oldh) {

            if (w != oldw || h != oldh) {
            bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            cv = new Canvas(bm);
            }
            super.onSizeChanged(w, h, oldw, oldh);
            }

            @Override
            protected void onDraw(Canvas canvas) {

            int w = getWidth();
            int h = getHeight();
            int radius = w > h ? h / 2 : w / 2;

            bm.eraseColor(Color.TRANSPARENT);
            cv.drawColor(getResources().getColor(R.color.colorPrimary));
            cv.drawCircle(w / 2, h / 2, radius, eraser);
            canvas.drawBitmap(bm, 0, 0, null);
            super.onDraw(canvas);
            }
            }


            Add this view as a layer on your camera view.



            Thanks






            share|improve this answer




























              0














              I got a solution, but not via xml drawable. We can create a CustomView by extending View class which can perform same as required:



              public class RadiusOverlayView extends View {

              Bitmap bm;
              Canvas cv;
              Paint eraser;

              public RadiusOverlayView(Context context) {
              super(context);
              Init();
              }

              public RadiusOverlayView(Context context, AttributeSet attrs) {
              super(context, attrs);
              Init();
              }

              public RadiusOverlayView(Context context, AttributeSet attrs,
              int defStyleAttr) {
              super(context, attrs, defStyleAttr);
              Init();
              }

              private void Init() {

              eraser = new Paint();
              eraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
              eraser.setAntiAlias(true);
              }

              @Override
              protected void onSizeChanged(int w, int h, int oldw, int oldh) {

              if (w != oldw || h != oldh) {
              bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
              cv = new Canvas(bm);
              }
              super.onSizeChanged(w, h, oldw, oldh);
              }

              @Override
              protected void onDraw(Canvas canvas) {

              int w = getWidth();
              int h = getHeight();
              int radius = w > h ? h / 2 : w / 2;

              bm.eraseColor(Color.TRANSPARENT);
              cv.drawColor(getResources().getColor(R.color.colorPrimary));
              cv.drawCircle(w / 2, h / 2, radius, eraser);
              canvas.drawBitmap(bm, 0, 0, null);
              super.onDraw(canvas);
              }
              }


              Add this view as a layer on your camera view.



              Thanks






              share|improve this answer


























                0












                0








                0







                I got a solution, but not via xml drawable. We can create a CustomView by extending View class which can perform same as required:



                public class RadiusOverlayView extends View {

                Bitmap bm;
                Canvas cv;
                Paint eraser;

                public RadiusOverlayView(Context context) {
                super(context);
                Init();
                }

                public RadiusOverlayView(Context context, AttributeSet attrs) {
                super(context, attrs);
                Init();
                }

                public RadiusOverlayView(Context context, AttributeSet attrs,
                int defStyleAttr) {
                super(context, attrs, defStyleAttr);
                Init();
                }

                private void Init() {

                eraser = new Paint();
                eraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
                eraser.setAntiAlias(true);
                }

                @Override
                protected void onSizeChanged(int w, int h, int oldw, int oldh) {

                if (w != oldw || h != oldh) {
                bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
                cv = new Canvas(bm);
                }
                super.onSizeChanged(w, h, oldw, oldh);
                }

                @Override
                protected void onDraw(Canvas canvas) {

                int w = getWidth();
                int h = getHeight();
                int radius = w > h ? h / 2 : w / 2;

                bm.eraseColor(Color.TRANSPARENT);
                cv.drawColor(getResources().getColor(R.color.colorPrimary));
                cv.drawCircle(w / 2, h / 2, radius, eraser);
                canvas.drawBitmap(bm, 0, 0, null);
                super.onDraw(canvas);
                }
                }


                Add this view as a layer on your camera view.



                Thanks






                share|improve this answer













                I got a solution, but not via xml drawable. We can create a CustomView by extending View class which can perform same as required:



                public class RadiusOverlayView extends View {

                Bitmap bm;
                Canvas cv;
                Paint eraser;

                public RadiusOverlayView(Context context) {
                super(context);
                Init();
                }

                public RadiusOverlayView(Context context, AttributeSet attrs) {
                super(context, attrs);
                Init();
                }

                public RadiusOverlayView(Context context, AttributeSet attrs,
                int defStyleAttr) {
                super(context, attrs, defStyleAttr);
                Init();
                }

                private void Init() {

                eraser = new Paint();
                eraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
                eraser.setAntiAlias(true);
                }

                @Override
                protected void onSizeChanged(int w, int h, int oldw, int oldh) {

                if (w != oldw || h != oldh) {
                bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
                cv = new Canvas(bm);
                }
                super.onSizeChanged(w, h, oldw, oldh);
                }

                @Override
                protected void onDraw(Canvas canvas) {

                int w = getWidth();
                int h = getHeight();
                int radius = w > h ? h / 2 : w / 2;

                bm.eraseColor(Color.TRANSPARENT);
                cv.drawColor(getResources().getColor(R.color.colorPrimary));
                cv.drawCircle(w / 2, h / 2, radius, eraser);
                canvas.drawBitmap(bm, 0, 0, null);
                super.onDraw(canvas);
                }
                }


                Add this view as a layer on your camera view.



                Thanks







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 3 at 12:53









                Akshay PaliwalAkshay Paliwal

                2,20912437




                2,20912437






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007180%2fhow-to-create-camera-frame-in-drawable-in-android%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas