Skip to content

Instantly share code, notes, and snippets.

@SouravKumarPandit
Created August 21, 2018 10:17
Show Gist options
  • Save SouravKumarPandit/f9d0a8c6255c7ea6b833ab8b75909075 to your computer and use it in GitHub Desktop.
Save SouravKumarPandit/f9d0a8c6255c7ea6b833ab8b75909075 to your computer and use it in GitHub Desktop.
public class SimpleSyncView extends View {
String syncing = " Syncing";
String toSync = " To Sync";
String synced = " Synced";
String valueSync = "00";
public static int TO_SYNC = 0;
public static int SYNCED = 1;
public static int SYNCING = 2;
private int rotationDegrees = 0;
private int colorSyncingLable;
private int colorValue;
private int colorSyncingBg;
private int colorSyncedBg;
private int colorToSyncBg;
private int colorSyncedLable;
private int colorToSyncLable;
private int syncStateIn = 0;
private int lableSize = 14;
private int valueSize = 18;
Paint painLable;
Paint paintValue;
float measuredlableWidth;
float measuredTextValuewidth;
private float measuredlableHeight;
private float measuredValueHeight;
private Drawable mDrawable;
public SimpleSyncView(Context context)
{
super(context);
colorSyncingLable = Color.BLACK;
colorSyncedLable = Color.BLACK;
colorToSyncLable = Color.BLACK;
colorValue = Color.WHITE;
colorSyncingBg = getResources().getColor(android.R.color.holo_orange_dark);
colorSyncedBg = getResources().getColor(android.R.color.holo_green_dark);
colorToSyncBg = getResources().getColor(android.R.color.holo_red_dark);
init(context);
}
public SimpleSyncView(Context context, @Nullable AttributeSet attrs)
{
super(context);
init(context);
}
private void init(Context context)
{
painLable = new Paint(Paint.ANTI_ALIAS_FLAG);
painLable.setTextSize(lableSize * getResources().getDisplayMetrics().density);
// painLable.setTextSize(size );
painLable.setColor(colorSyncingLable);
paintValue = new Paint(Paint.ANTI_ALIAS_FLAG);
paintValue.setTextSize(valueSize * getResources().getDisplayMetrics().density);
// paintValue.setTextSize(size );
paintValue.setColor(colorValue);
measuredlableWidth = painLable.measureText(syncing);
measuredTextValuewidth = paintValue.measureText(valueSync);
measuredlableHeight = lableSize * getResources().getDisplayMetrics().density;
measuredValueHeight = valueSize * getResources().getDisplayMetrics().density;
mDrawable = new SyncDrawable();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
if (syncStateIn == 2) {
mDrawable.setBounds((int) (measuredTextValuewidth / 3), (int) (measuredTextValuewidth / 3), (int) (measuredTextValuewidth * 2) - (int) (measuredTextValuewidth / 3), canvas.getHeight() - (int) (measuredTextValuewidth / 3));
// canvas.save();
painLable.setColor(colorSyncingBg);
canvas.drawRect(0, 0, measuredTextValuewidth * 2, canvas.getHeight(), painLable);
painLable.setColor(colorSyncingLable);
canvas.drawText(syncing, measuredTextValuewidth * 2, canvas.getHeight() / 2 + measuredlableHeight / 3, painLable);
// canvas.drawText(valueSync, measuredTextValuewidth * 3 / 2 - measuredTextValuewidth / 2, canvas.getHeight() / 2 + measuredValueHeight / 2, paintValue);
canvas.rotate(rotation(8), measuredTextValuewidth, canvas.getHeight() / 2);
mDrawable.draw(canvas);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
postInvalidateOnAnimation();
}
} else if (syncStateIn == 1) {
canvas.save();
painLable.setColor(colorSyncedBg);
canvas.drawRect(0, 0, measuredTextValuewidth * 2, canvas.getHeight(), painLable);
painLable.setColor(colorSyncedLable);
canvas.drawText(synced, measuredTextValuewidth * 2, canvas.getHeight() / 2 + measuredlableHeight / 3, painLable);
canvas.drawText(valueSync, measuredTextValuewidth - measuredTextValuewidth / 2, canvas.getHeight() / 2 + measuredValueHeight / 2, paintValue);
canvas.restore();
} else {
canvas.save();
painLable.setColor(colorToSyncBg);
canvas.drawRect(0, 0, measuredTextValuewidth * 2, canvas.getHeight(), painLable);
painLable.setColor(colorToSyncLable);
canvas.drawText(toSync, measuredTextValuewidth * 2, canvas.getHeight() / 2 + measuredlableHeight / 3, painLable);
canvas.drawText(valueSync, measuredTextValuewidth - measuredTextValuewidth / 2, canvas.getHeight() / 2 + measuredValueHeight / 2, paintValue);
canvas.restore();
}
}
public void setGradient(int sColor, int eColor)
{
painLable.setShader(new LinearGradient(0, 0, 0, getHeight() / 2,
new int[]{sColor, eColor},
new float[]{0.0f, 1.0f}, Shader.TileMode.CLAMP));
}
private int rotation(int delta)
{
rotationDegrees = (rotationDegrees + delta) % 360;
return -rotationDegrees;
}
public int getSyncStateIn()
{
return syncStateIn;
}
public void setSyncStateIn(int syncStateIn)
{
this.syncStateIn = syncStateIn;
invalidate();
}
public String getValueSync()
{
return valueSync;
}
public void setValueSync(String valueSync)
{
this.valueSync = valueSync;
invalidate();
}
public int getRotationDegrees()
{
return rotationDegrees;
}
public void setRotationDegrees(int rotationDegrees)
{
this.rotationDegrees = rotationDegrees;
invalidate();
}
public int getColorSyncingLable()
{
return colorSyncingLable;
}
public void setColorSyncingLable(int colorSyncingLable)
{
this.colorSyncingLable = colorSyncingLable;
invalidate();
}
public int getColorValue()
{
return colorValue;
}
public void setColorValue(int colorValue)
{
this.colorValue = colorValue;
invalidate();
}
public int getColorSyncingBg()
{
return colorSyncingBg;
}
public void setColorSyncingBg(int colorSyncingBg)
{
this.colorSyncingBg = colorSyncingBg;
invalidate();
}
public int getColorSyncedBg()
{
return colorSyncedBg;
}
public void setColorSyncedBg(int colorSyncedBg)
{
this.colorSyncedBg = colorSyncedBg;
invalidate();
}
public int getColorToSyncBg()
{
return colorToSyncBg;
}
public void setColorToSyncBg(int colorToSyncBg)
{
this.colorToSyncBg = colorToSyncBg;
invalidate();
}
public int getColorSyncedLable()
{
return colorSyncedLable;
}
public void setColorSyncedLable(int colorSyncedLable)
{
this.colorSyncedLable = colorSyncedLable;
invalidate();
}
public int getColorToSyncLable()
{
return colorToSyncLable;
}
public void setColorToSyncLable(int colorToSyncLable)
{
this.colorToSyncLable = colorToSyncLable;
invalidate();
}
public int getLableSize()
{
return lableSize;
}
public void setLableSize(int lableSize)
{
this.lableSize = lableSize;
invalidate();
}
public int getValueSize()
{
return valueSize;
}
public void setValueSize(int valueSize)
{
this.valueSize = valueSize;
invalidate();
}
}
@SouravKumarPandit
Copy link
Author

SouravKumarPandit commented Aug 21, 2018

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;

public class SyncDrawable extends Drawable {
private static final float[] VIEW_BOX = { 0.000000f, 0.000000f, 561.000000f, 561.000000f };
private RectF rect = new RectF();
private Matrix matrix = new Matrix();
private Shader shader;
private Path p = new Path();
private Paint paint = new Paint();
@OverRide
public void draw(Canvas canvas) {
paint.setAntiAlias(true);
float viewBoxWidth = VIEW_BOX[2];
float viewBoxHeight = VIEW_BOX[3];
Rect bounds = getBounds();
if (viewBoxHeight <= 0 || viewBoxWidth <= 0 || bounds.width() <= 0 || bounds.height() <= 0) {
return;
}
canvas.save();
float viewBoxRatio = viewBoxWidth / (float) viewBoxHeight;
float boundsRatio = bounds.width() / (float) bounds.height();
float factorScale;
if (boundsRatio > viewBoxRatio) {
// canvas larger than viewbox
factorScale = bounds.height() / (float) viewBoxHeight;
} else {
// canvas higher (or equals) than viewbox
factorScale = bounds.width() / (float) viewBoxWidth;
}
int newViewBoxHeight = Math.round(factorScale * viewBoxHeight);
int newViewBoxWidth = Math.round(factorScale * viewBoxWidth);
int marginX = bounds.width() - newViewBoxWidth;
int marginY = bounds.height() - newViewBoxHeight;
canvas.translate(bounds.left, bounds.top);
canvas.translate(Math.round(marginX / 2f), Math.round(marginY / 2f));
canvas.clipRect(0, 0, newViewBoxWidth, newViewBoxHeight);
canvas.translate(-Math.round(factorScale * VIEW_BOX[0]), -Math.round(factorScale * VIEW_BOX[1]));
paint.setAlpha(255);
p.reset();
p.moveTo(factorScale * 280.500000f, factorScale * 76.500000f);
p.lineTo(factorScale * 280.500000f, factorScale * 0.000000f);
p.rLineTo(factorScale * -102.000000f, factorScale * 102.000000f);
p.rLineTo(factorScale * 102.000000f, factorScale * 102.000000f);
p.rLineTo(0, factorScale * -76.500000f);
p.rCubicTo(factorScale * 84.150002f, factorScale * 0.000000f, factorScale * 153.000000f, factorScale * 68.849998f, factorScale * 153.000000f, factorScale * 153.000000f);
p.rCubicTo(factorScale * 0.000000f, factorScale * 25.500000f, factorScale * -7.650000f, factorScale * 51.000000f, factorScale * -17.850000f, factorScale * 71.400002f);
p.rLineTo(factorScale * 38.250000f, factorScale * 38.250000f);
p.cubicTo(factorScale * 471.750000f, factorScale * 357.000000f, factorScale * 484.500000f, factorScale * 321.299988f, factorScale * 484.500000f, factorScale * 280.500000f);
p.cubicTo(factorScale * 484.500000f, factorScale * 168.300003f, factorScale * 392.700012f, factorScale * 76.500000f, factorScale * 280.500000f, factorScale * 76.500000f);
p.close();
p.moveTo(factorScale * 280.500000f, factorScale * 76.500000f);
p.moveTo(factorScale * 280.500000f, factorScale * 433.500000f);
p.rCubicTo(factorScale * -84.150002f, factorScale * 0.000000f, factorScale * -153.000000f, factorScale * -68.849998f, factorScale * -153.000000f, factorScale * -153.000000f);
p.rCubicTo(factorScale * 0.000000f, factorScale * -25.500000f, factorScale * 7.650000f, factorScale * -51.000000f, factorScale * 17.850000f, factorScale * -71.400002f);
p.rLineTo(factorScale * -38.250000f, factorScale * -38.250000f);
p.cubicTo(factorScale * 89.250000f, factorScale * 204.000000f, factorScale * 76.500000f, factorScale * 239.699997f, factorScale * 76.500000f, factorScale * 280.500000f);

p.rCubicTo(factorScale * 0.000000f, factorScale * 112.199997f, factorScale * 91.800003f, factorScale * 204.000000f, factorScale * 204.000000f, factorScale * 204.000000f);
p.lineTo(factorScale * 280.500000f, factorScale * 561.000000f);
p.rLineTo(factorScale * 102.000000f, factorScale * -102.000000f);
p.rLineTo(factorScale * -102.000000f, factorScale * -102.000000f);
p.lineTo(factorScale * 280.500000f, factorScale * 433.500000f);
p.close();
p.moveTo(factorScale * 280.500000f, factorScale * 433.500000f);
p.setFillType(Path.FillType.EVEN_ODD);
paint.setShader(null);
paint.setColor(-1);
paint.setAlpha(255);
paint.setStyle(Paint.Style.FILL);
canvas.drawPath(p, paint);
paint.setAlpha(255);
canvas.restore();
}
@OverRide public void setAlpha(int alpha) { }
@OverRide public void setColorFilter(ColorFilter cf) { }
@OverRide public int getOpacity() { return 0; }
@OverRide public int getMinimumHeight() { return 10; }
@OverRide public int getMinimumWidth() { return 10; }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment