Skip to content

Instantly share code, notes, and snippets.

@robillo
Created August 6, 2017 18:34
Show Gist options
  • Save robillo/a270fc6f5ce26176574cf6db149d9c9a to your computer and use it in GitHub Desktop.
Save robillo/a270fc6f5ce26176574cf6db149d9c9a to your computer and use it in GitHub Desktop.
Basic rect shape drawn on canvas.
package com.robillo.customviewstutorial;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by robinkamboj on 06/08/17.
*/
public class MyCustomView extends View{
public MyCustomView(Context context) {
super(context);
init(null);
}
public MyCustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public MyCustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MyCustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(attrs);
}
private void init(@Nullable AttributeSet set){
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.MAGENTA);
Rect rect = new Rect();
rect.left = 0;
rect.right = getWidth();
rect.top = 0;
rect.bottom = getHeight();
canvas.drawRect(rect, paint);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment