【问题标题】:Android Bitmap crop oval shapeAndroid 位图裁剪椭圆形状
【发布时间】:2017-07-05 21:35:00
【问题描述】:

假设我有一张用前置摄像头拍摄的人像位图。 拍照后如何将位图裁剪为椭圆形叠加层的形状?椭圆形以屏幕为中心。椭圆形有固定的高度和宽度

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:orientation="vertical"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<!-- camera viewfinder here which fits the whole screen-->
<RelativeLayout
    android:id="@+id/cameraLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:paddingTop="10dp">

        <!--Custom buttons-->

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/record_panel"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="@android:color/transparent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <!--Capture button-->

    </RelativeLayout>

</RelativeLayout>

<!--the oval-shaped overlay-->
<ImageView 
    android:layout_width="210dp"
    android:layout_height="318dp"
    android:id="@+id/overlay"
    android:layout_gravity="center"
    android:src="@mipmap/oval" />

</FrameLayout>

截图:

【问题讨论】:

  • Kelok 你是怎么解决的?

标签: android bitmap crop bitmapfactory


【解决方案1】:

您可以根据如何从位图裁剪圆来解决您的问题:

    @Override
    public Bitmap crop(Bitmap source) {
        int size = Math.min(source.getWidth(), source.getHeight());

        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;

        Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
        if (squaredBitmap != source) {
            source.recycle();
        }

        Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        BitmapShader shader = new BitmapShader(squaredBitmap,
                BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
        paint.setShader(shader);
        paint.setAntiAlias(true);

        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);

        squaredBitmap.recycle();
        return bitmap;
    }

现在只需将 canvas.drawCircle 更改为 canvas.drawOval 并为其赋予适当的值。

【讨论】:

    猜你喜欢
    • 2014-05-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    相关资源
    最近更新 更多