2013-11-21 2 views
0

저는 현재 Android에 기존 iOS 앱을 이식하는 과정에 있으며 레이아웃에서보기의 내용을자를 필요가있는 곳으로 달려 들었습니다.Android 클립/자르기보기 콘텐츠

iOS에서는보기의 레이어에 액세스 한 다음 그에 따라 layer.contentsRect을 설정했습니다. 안드로이드에서

, 나는 내가 GLSurfaceView 클래스의 해당 기능을 발견했다고 생각 - setClipBounds, 그러나 이것은 단지 API 레벨 18을 지원하는 장치에서 작동하며 내 갤럭시 S 3

합니까에 NoSuchMethodError 예외가 발생합니다 누구나 API 레벨 9 (2.3)의보기 내용을 클리핑 또는 자르기위한 대체 솔루션 (또는 지원 라이브러리)을 가지고 있습니까? 감사.

답변

0

xamarin (C#)에서 작성한 일부 자르기 코드는 Java에서 포팅되었으므로 직접 변환해야합니다. 클래스 이름은 대략 같으며 메서드 및 속성은 get/Java로 설정합니다.

코드는 부동 얼굴 :

public static class BitmapHelpers 
{ 
    public static Bitmap LoadAndResizeBitmap (this string fileName, int width, int height) 
    { 
     // First we get the the dimensions of the file on disk 
     BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true }; 
     BitmapFactory.DecodeFile (fileName, options); 

     // Next we calculate the ratio that we need to resize the image by 
     // in order to fit the requested dimensions. 
     int outHeight = options.OutHeight; 
     int outWidth = options.OutWidth; 
     int inSampleSize = 1; 

     if (outHeight > height || outWidth > width) { 
      inSampleSize = outWidth > outHeight 
           ? outHeight/height 
           : outWidth/width; 
     } 

     // Now we will load the image and have BitmapFactory resize it for us. 
     options.InSampleSize = inSampleSize; 
     options.InJustDecodeBounds = false; 
     Bitmap resizedBitmap = BitmapFactory.DecodeFile (fileName, options); 

     return resizedBitmap; 
    } 

    public static Bitmap GetCroppedBitmap (Bitmap bitmap) 
    { 
     Bitmap output = Bitmap.CreateBitmap (bitmap.Width, 
            bitmap.Height, global::Android.Graphics.Bitmap.Config.Argb8888); 
     Canvas canvas = new Canvas (output); 

     int color = -1; 
     Paint paint = new Paint(); 
     Rect rect = new Rect (0, 0, bitmap.Width, bitmap.Height); 

     paint.AntiAlias = true; 
     canvas.DrawARGB (0, 0, 0, 0); 
     paint.Color = Color.White; 

     canvas.DrawCircle (bitmap.Width/2, bitmap.Height/2, 
      bitmap.Width/2, paint); 
     paint.SetXfermode (new PorterDuffXfermode (global::Android.Graphics.PorterDuff.Mode.SrcIn)); 
     canvas.DrawBitmap (bitmap, rect, rect, paint); 

     return output; 
    } 
} 
페이스 북의 유사한 원에 비트 맵을, 작물