Created
August 6, 2017 18:34
-
-
Save robillo/a270fc6f5ce26176574cf6db149d9c9a to your computer and use it in GitHub Desktop.
Basic rect shape drawn on canvas.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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