Created
August 11, 2017 19:14
-
-
Save robillo/45f75b5ffbd8ded7748c5a1ab034651a to your computer and use it in GitHub Desktop.
my custom view
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.content.res.TypedArray; | |
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{ | |
Paint mPaint; | |
Rect mRect; | |
int mSquareColor; | |
int mPadding = 0; | |
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){ | |
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
mRect = new Rect(); | |
if(set == null){ | |
return; | |
} | |
TypedArray ta = getContext().obtainStyledAttributes(set, R.styleable.MyCustomView); | |
mSquareColor = ta.getColor(R.styleable.MyCustomView_square_color, Color.GREEN); | |
mPaint.setColor(mSquareColor); | |
ta.recycle(); | |
} | |
public void swapColor(){ | |
} | |
public void customPaddingUp(int padding){ | |
} | |
public void customPaddingDown(int padding){ | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
mRect.left = 0 + mPadding; | |
mRect.right = getWidth() - mPadding; | |
mRect.top = 0 + mPadding; | |
mRect.bottom = getHeight() - mPadding; | |
canvas.drawRect(mRect, mPaint); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment