Skip to content

Instantly share code, notes, and snippets.

@praslnx8
Created June 7, 2017 13:43
Show Gist options
  • Save praslnx8/873d4a8863d5b43d2c4294816d50654a to your computer and use it in GitHub Desktop.
Save praslnx8/873d4a8863d5b43d2c4294816d50654a to your computer and use it in GitHub Desktop.
inclined view
/*
* @category Daimler
* @copyright Copyright (C) 2017 Contus. All rights reserved.
* @license http://www.apache.org/licenses/LICENSE-2.0
*/
package daimler.com.loadcarrier.uiux;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import daimler.com.loadcarrier.R;
/**
* An extension of view class that creates a tapered view effect.
*
* @author ContusTeam <[email protected]>
* @version 1.0
*/
public class InclinedVIew extends View {
private Paint paint;
private Path path;
private int w;
private int h;
private int inclinedAreaColor = Color.WHITE;
/**
* Public constructor to create an instance of this view.
*
* @param ctx The context this view is created with.
* @param attrs The attribute set for this view.
*/
public InclinedVIew(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
init(ctx, attrs);
setWillNotDraw(false);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStrokeWidth(2);
paint.setColor(inclinedAreaColor);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setAntiAlias(true);
path = new Path();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
w = getMeasuredWidth();
h = getMeasuredHeight();
}
/**
* Searches for InclinedVIew_color and applies that color to the view.
*
* @param context The context instance with which this view is initialized.
* @param attrs The attrs from xml.
*/
private void init(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.InclinedVIew);
inclinedAreaColor = typedArray.getColor(R.styleable.InclinedVIew_color, Color.WHITE);
typedArray.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
path.reset();
path.setFillType(Path.FillType.EVEN_ODD);
path.moveTo(0, h);
path.lineTo(w, 0);
path.lineTo(w, h);
path.close();
canvas.drawPath(path, paint);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment