Akshay Paliwal
Akshay Paliwal

Reputation: 3916

How to create camera frame in drawable in android

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?

Upvotes: 2

Views: 1530

Answers (2)

Akshay Paliwal
Akshay Paliwal

Reputation: 3916

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

Upvotes: 0

Jarin Rocks
Jarin Rocks

Reputation: 1015

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>

Upvotes: 1

Related Questions