/*
* Copyright 2016 Flipkart Internet Pvt. Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.flipkart.android.proteus.demo.customviews;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import com.flipkart.android.proteus.view.ProteusView;
import com.flipkart.android.proteus.view.manager.ProteusViewManager;
/**
* CircleView
*
* @author aditya.sharat
*/
public class CircleView extends View implements ProteusView {
public void setColor(String HEX_COLOR) {
this.HEX_COLOR = HEX_COLOR;
}
ProteusViewManager viewManager;
@Override
public ProteusViewManager getViewManager() {
return viewManager;
}
@Override
public void setViewManager(ProteusViewManager viewManager) {
this.viewManager = viewManager;
}
private String HEX_COLOR = "#45ba8a";
private Paint drawPaint;
private float radius;
public CircleView(Context context) {
super(context);
init();
}
public CircleView(final Context context, final AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
drawPaint = new Paint();
drawPaint.setColor(Color.parseColor(HEX_COLOR));
drawPaint.setAntiAlias(true);
setOnMeasureCallback();
}
@Override
protected void onDraw(final Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(radius, radius, radius, drawPaint);
}
private void setOnMeasureCallback() {
ViewTreeObserver vto = getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
removeOnGlobalLayoutListener(this);
radius = getMeasuredWidth() / 2;
}
});
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void removeOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener listener) {
if (Build.VERSION.SDK_INT < 16) {
getViewTreeObserver().removeGlobalOnLayoutListener(listener);
} else {
getViewTreeObserver().removeOnGlobalLayoutListener(listener);
}
}
}