Skip to content

Instantly share code, notes, and snippets.

@MartinRGB
Last active September 19, 2017 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinRGB/85e88e9951840e8b953e231d786c003f to your computer and use it in GitHub Desktop.
Save MartinRGB/85e88e9951840e8b953e231d786c003f to your computer and use it in GitHub Desktop.
RoundedCornerLayout
package com.martinrgb.multitask;
/**
* Created by MartinRGB on 2017/9/14.
*/
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.FrameLayout;
public class RoundedCornerLayout extends FrameLayout {
//Init Radius in DP
public float CORNER_RADIUS = 6.0f;
//Draw Round Radius Boolean
public boolean shouldRedraw = false;
//Now Value for torlerance
public float nowValue = 0;
//Animation Time
public float animTime = 400;
private float cornerRadius;
public long startTime;
int framesPerSecond = 60;
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 DisplayMetrics mMetrics;
private void init(Context context, AttributeSet attrs, int defStyle) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
mMetrics = metrics;
cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, metrics);
setLayerType(View.LAYER_TYPE_HARDWARE, null);
this.startTime = System.currentTimeMillis();
this.postInvalidate();
}
@Override
protected void onDraw(Canvas canvas){
}
@Override
protected void dispatchDraw(Canvas canvas) {
int count = canvas.save();
if(shouldRedraw){
final Path reDrawPath = new Path();
cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, mMetrics);
Log.e("TAG",Float.toString(cornerRadius));
nowValue += cornerRadius * animTime/1000*60;
//18 200
reDrawPath.addRoundRect(new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), Math.min(cornerRadius,nowValue), Math.min(cornerRadius,nowValue), Path.Direction.CW);
canvas.clipPath(reDrawPath, Region.Op.REPLACE);
canvas.clipPath(reDrawPath);
super.dispatchDraw(canvas);
canvas.restoreToCount(count);
if(nowValue < cornerRadius)
this.postInvalidateDelayed( 1000 / framesPerSecond);
Log.e("nowValue",Float.toString(nowValue));
}
else{
final Path path = new Path();
if(nowValue >0){
cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, mMetrics);
nowValue -= cornerRadius * animTime/1000*60;
}
else{
nowValue = 0;
}
path.addRoundRect(new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), Math.max(0,nowValue), Math.max(0,nowValue), Path.Direction.CW);
canvas.clipPath(path, Region.Op.REPLACE);
canvas.clipPath(path);
super.dispatchDraw(canvas);
canvas.restoreToCount(count);
if(nowValue > 0)
this.postInvalidateDelayed( 1000 / framesPerSecond);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment