simplifying base models
new attachments
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package com.meloda.fast.widget;
|
||||
|
||||
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.FrameLayout;
|
||||
|
||||
public class RoundedCornerLayout extends FrameLayout {
|
||||
private final static float CORNER_RADIUS = 40.0f;
|
||||
|
||||
private Bitmap maskBitmap;
|
||||
private Paint paint, maskPaint;
|
||||
private float cornerRadius;
|
||||
|
||||
public RoundedCornerLayout(Context context) {
|
||||
super(context);
|
||||
init(context, null, 0);
|
||||
}
|
||||
|
||||
public RoundedCornerLayout(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init(context, attrs, 0);
|
||||
}
|
||||
|
||||
public RoundedCornerLayout(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
init(context, attrs, defStyle);
|
||||
}
|
||||
|
||||
private void init(Context context, AttributeSet attrs, int defStyle) {
|
||||
DisplayMetrics 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) {
|
||||
Bitmap offscreenBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
|
||||
Canvas offscreenCanvas = new Canvas(offscreenBitmap);
|
||||
|
||||
super.draw(offscreenCanvas);
|
||||
|
||||
if (maskBitmap == null) {
|
||||
maskBitmap = createMask(getWidth(), getHeight());
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.meloda.fast.widget
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Path
|
||||
import android.graphics.RectF
|
||||
import android.graphics.Region
|
||||
import android.util.AttributeSet
|
||||
import android.widget.FrameLayout
|
||||
import com.meloda.fast.R
|
||||
|
||||
class RoundedFrameLayout : FrameLayout {
|
||||
/**
|
||||
* The corners than can be changed
|
||||
*/
|
||||
private var topLeftCornerRadius = 0f
|
||||
private var topRightCornerRadius = 0f
|
||||
private var bottomLeftCornerRadius = 0f
|
||||
private var bottomRightCornerRadius = 0f
|
||||
|
||||
constructor(context: Context) : super(context) {
|
||||
init(context, null, 0)
|
||||
}
|
||||
|
||||
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
|
||||
init(context, attrs, 0)
|
||||
}
|
||||
|
||||
constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet?,
|
||||
defStyleAttr: Int
|
||||
) : super(context, attrs, defStyleAttr) {
|
||||
init(context, attrs, defStyleAttr)
|
||||
}
|
||||
|
||||
private fun init(context: Context, attrs: AttributeSet?, defStyle: Int) {
|
||||
val typedArray = context.obtainStyledAttributes(
|
||||
attrs,
|
||||
R.styleable.RoundedFrameLayout, 0, 0
|
||||
)
|
||||
|
||||
topLeftCornerRadius =
|
||||
typedArray.getDimension(R.styleable.RoundedFrameLayout_topLeftCornerRadius, 0f)
|
||||
topRightCornerRadius =
|
||||
typedArray.getDimension(R.styleable.RoundedFrameLayout_topRightCornerRadius, 0f)
|
||||
bottomLeftCornerRadius =
|
||||
typedArray.getDimension(R.styleable.RoundedFrameLayout_bottomLeftCornerRadius, 0f)
|
||||
bottomRightCornerRadius =
|
||||
typedArray.getDimension(R.styleable.RoundedFrameLayout_bottomRightCornerRadius, 0f)
|
||||
|
||||
typedArray.recycle()
|
||||
setLayerType(LAYER_TYPE_HARDWARE, null)
|
||||
}
|
||||
|
||||
override fun dispatchDraw(canvas: Canvas) {
|
||||
val count: Int = canvas.save()
|
||||
val path = Path()
|
||||
val cornerDimensions = floatArrayOf(
|
||||
topLeftCornerRadius, topLeftCornerRadius,
|
||||
topRightCornerRadius, topRightCornerRadius,
|
||||
bottomRightCornerRadius, bottomRightCornerRadius,
|
||||
bottomLeftCornerRadius, bottomLeftCornerRadius
|
||||
)
|
||||
path.addRoundRect(
|
||||
RectF(0f, 0f, canvas.width.toFloat(), canvas.height.toFloat()),
|
||||
cornerDimensions,
|
||||
Path.Direction.CW
|
||||
)
|
||||
canvas.clipPath(path, Region.Op.INTERSECT)
|
||||
canvas.clipPath(path)
|
||||
super.dispatchDraw(canvas)
|
||||
canvas.restoreToCount(count)
|
||||
}
|
||||
|
||||
fun setTopLeftCornerRadius(topLeftCornerRadius: Float) {
|
||||
this.topLeftCornerRadius = topLeftCornerRadius
|
||||
invalidate()
|
||||
}
|
||||
|
||||
fun setTopRightCornerRadius(topRightCornerRadius: Float) {
|
||||
this.topRightCornerRadius = topRightCornerRadius
|
||||
invalidate()
|
||||
}
|
||||
|
||||
fun setBottomLeftCornerRadius(bottomLeftCornerRadius: Float) {
|
||||
this.bottomLeftCornerRadius = bottomLeftCornerRadius
|
||||
invalidate()
|
||||
}
|
||||
|
||||
fun setBottomRightCornerRadius(bottomRightCornerRadius: Float) {
|
||||
this.bottomRightCornerRadius = bottomRightCornerRadius
|
||||
invalidate()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user