mirror of
https://github.com/HeyPuter/firefox.git
synced 2026-11-30 14:17:36 -10:00
93 lines
3.1 KiB
Java
93 lines
3.1 KiB
Java
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
package org.mozilla.gecko.widget;
|
|
|
|
import org.mozilla.gecko.R;
|
|
|
|
import android.content.Context;
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.Canvas;
|
|
import android.graphics.Color;
|
|
import android.graphics.Paint;
|
|
import android.graphics.PorterDuff;
|
|
import android.graphics.PorterDuffXfermode;
|
|
import android.graphics.RectF;
|
|
import android.util.AttributeSet;
|
|
import android.util.DisplayMetrics;
|
|
import android.util.TypedValue;
|
|
import android.widget.LinearLayout;
|
|
|
|
public class RoundedCornerLayout extends LinearLayout {
|
|
private static final String LOGTAG = "Gecko" + RoundedCornerLayout.class.getSimpleName();
|
|
private Bitmap maskBitmap;
|
|
private Paint paint, maskPaint;
|
|
private float cornerRadius;
|
|
|
|
public RoundedCornerLayout(Context context) {
|
|
super(context);
|
|
init(context);
|
|
}
|
|
|
|
public RoundedCornerLayout(Context context, AttributeSet attrs) {
|
|
super(context, attrs);
|
|
init(context);
|
|
}
|
|
|
|
public RoundedCornerLayout(Context context, AttributeSet attrs, int defStyle) {
|
|
super(context, attrs, defStyle);
|
|
init(context);
|
|
}
|
|
|
|
private void init(Context context) {
|
|
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
|
|
|
|
cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,
|
|
getResources().getDimensionPixelSize(R.dimen.doorhanger_rounded_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
|
|
protected void onLayout(final boolean changed, final int l, final int t, final int r, final int b) {
|
|
super.onLayout(changed, l, t, r, b);
|
|
if (changed) {
|
|
maskBitmap = createMask(r, b);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
public void draw(Canvas canvas) {
|
|
Bitmap offscreenBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
|
|
Canvas offscreenCanvas = new Canvas(offscreenBitmap);
|
|
|
|
super.draw(offscreenCanvas);
|
|
|
|
offscreenCanvas.drawBitmap(maskBitmap, 0f, 0f, maskPaint);
|
|
canvas.drawBitmap(offscreenBitmap, 0f, 0f, paint);
|
|
}
|
|
|
|
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;
|
|
}
|
|
} |