2016-03-21 7 views
1

レイアウトxmlで標高パラメータを設定すると、カスタムビューにシャドーを追加できます。私は影を追加することができますが、丸い角の形ではなく四角形として適用されています。私はそれを丸めるためのコードをどこに追加するのか分かりません。それはどこにあり、どのようにすべきか?カスタムビュー:影の丸みを付けたアウトラインを作成する

これは、それが今どのように見えるかです:

enter image description here

これは、カスタムビューです:

public class RoundedFrameLayout extends FrameLayout { 
    private final static float CORNER_RADIUS = 15.0f; 

    Bitmap maskBitmap; 
    private Paint paint, maskPaint; 
    private float cornerRadius; 

    DisplayMetrics metrics; 

    public RoundedFrameLayout(Context context) { 
     super(context); 
     init(context, null, 0); 
    } 

    public RoundedFrameLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context, attrs, 0); 
    } 

    public RoundedFrameLayout(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(context, attrs, defStyle); 
    } 

    private void init(Context context, AttributeSet attrs, int defStyle) { 

     metrics = context.getResources().getDisplayMetrics(); 
     cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, metrics); 

     paint = new Paint(Paint.ANTI_ALIAS_FLAG); 

     maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); 
     maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 

     setWillNotDraw(false); 
    } 

    @Override 
    public void draw(Canvas canvas) { 

     cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) canvas.getWidth() * 0.05f, metrics); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 

      setOutlineProvider(new CustomOutline(canvas.getWidth(), canvas.getHeight())); 
     } 

     Bitmap offscreenBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888); 
     Canvas offscreenCanvas = new Canvas(offscreenBitmap); 

     super.draw(offscreenCanvas); 

     maskBitmap = createMask(canvas.getWidth(), canvas.getHeight()); 

     offscreenCanvas.drawBitmap(maskBitmap, 0f, 0f, maskPaint); 

     canvas.drawBitmap(offscreenBitmap, 0f, 0f, paint); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    private class CustomOutline extends ViewOutlineProvider { 

     int width; 
     int height; 

     CustomOutline(int width, int height) { 

      this.width = width; 
      this.height = height; 
     } 

     @Override 
     public void getOutline(View view, Outline outline) { 

      outline.setRect(0, 0, width, height); 
     } 
    } 

    private Bitmap createMask(int width, int height) { 

     Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8); 
     Canvas canvas = new Canvas(mask); 

     Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     paint.setColor(Color.WHITE); 

     canvas.drawRect(0, 0, width, height, paint); 

     paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 
     canvas.drawRoundRect(new RectF(0, 0, width, height), cornerRadius, cornerRadius, paint); 

     return mask; 
    } 
} 

答えて

1

私はちょうど私がちょうどViewOutlineProviderカスタムの異なるメソッドを呼び出すために必要なことに気づきましたクラス:

@Override 
    public void getOutline(View view, Outline outline) { 

     outline.setRoundRect(0, 0, width, height, radius); 
    } 

今すぐうまくいく。

関連する問題