2017-02-16 12 views
2

私は自分の正面のカメラで撮影したポートレートビットマップを持っているとします。 画像を撮影した後、ビットマップを楕円形のオーバーレイの形にクロップする方法はありますか?楕円形はスクリーンの中央に配置されます。楕円形状は、高さと幅を固定しているAndroidビットマップクロップ楕円形

<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> 

スクリーンショット:

@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を変更する:あなたは、ビットマップから円をトリミングする方法についてのあなたのソリューションをベースにすることができます enter image description here

+0

どのように解決しましたか? –

答えて

0

​​とそれに適切な値を与えます。