Created
June 20, 2019 13:23
-
-
Save mohamad-shafaee/87421d87e914779208109d6d78478ff6 to your computer and use it in GitHub Desktop.
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.msh_n.myp.customview1; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.res.Configuration; | |
import android.content.res.TypedArray; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.Path; | |
import android.graphics.Rect; | |
import android.graphics.RectF; | |
import android.media.Image; | |
import android.os.Build; | |
import android.support.annotation.Nullable; | |
import android.support.annotation.RequiresApi; | |
import android.support.v7.widget.AppCompatButton; | |
import android.support.v7.widget.AppCompatImageView; | |
import android.util.AttributeSet; | |
import android.util.DisplayMetrics; | |
import android.util.TypedValue; | |
import android.view.Display; | |
import android.view.View; | |
import android.widget.Button; | |
public class PieView extends View { | |
int radius; | |
float angle; | |
Paint paintBackground; | |
Paint paintV; | |
Paint paintShadow; | |
Rect rect; | |
int backColor; | |
int viewColor; | |
int height =500; | |
int width=250; | |
int ori; | |
int actionBarHeight; | |
RectF rectCircle; | |
RectF rectOval; | |
public PieView(Context context){ | |
//this(context, null, 0); | |
super(context); | |
} | |
public PieView(Context context, AttributeSet attrs){ | |
//this(context, attrs, 0); | |
super(context, attrs); | |
init(context, attrs); | |
} | |
public PieView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(context, attrs); | |
} | |
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | |
public PieView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes){ | |
super(context, attrs, defStyleAttr, defStyleRes); | |
init(context, attrs); | |
} | |
public String getTitleText(){ | |
return titleText; | |
} | |
public void setTitleText(String str){ | |
titleText = str; | |
} | |
public int getRadius(){ | |
return radius; | |
} | |
public float getAngle() { | |
return angle; | |
} | |
public void setAngle(float ang) { | |
this.angle = ang; | |
} | |
protected void init(Context context, @Nullable AttributeSet attrs){ | |
angle = 5; | |
rect = new Rect(); | |
// finding height of the action bar | |
TypedValue tv = new TypedValue(); | |
context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true); | |
actionBarHeight = getResources().getDimensionPixelSize(tv.resourceId); | |
paintBackground = new Paint(Paint.ANTI_ALIAS_FLAG); | |
paintV = new Paint(); | |
paintShadow = new Paint(); | |
//height = getLayoutParams().height; | |
//width = getLayoutParams().width; | |
//width = getMeasuredWidth(); | |
//height = getMeasuredHeight(); | |
ori = ((Activity) context).getResources().getConfiguration().orientation; | |
if(attrs == null){ | |
backColor = getResources().getColor(android.R.color.holo_purple); | |
viewColor = getResources().getColor(android.R.color.holo_green_light); | |
paintBackground.setColor(backColor); | |
paintV.setColor(viewColor); | |
paintShadow.setColor(getResources().getColor(android.R.color.holo_purple)); | |
return; | |
} | |
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, | |
R.styleable.PieView, 0, 0); | |
try { | |
titleText = a.getString(R.styleable.PieView_titleText); | |
backColor = a.getColor(R.styleable.PieView_backgroundColor, | |
getResources().getColor(android.R.color.holo_blue_light)); | |
viewColor = a.getColor(R.styleable.PieView_viewColor, | |
getResources().getColor(android.R.color.holo_orange_dark)); | |
} finally { | |
a.recycle(); | |
} | |
paintBackground.setColor(backColor); | |
paintV.setColor(viewColor); | |
paintShadow.setColor(getResources().getColor(android.R.color.holo_purple)); | |
} | |
@Override | |
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | |
super.onSizeChanged(w, h, oldw, oldh); | |
width = w; | |
//height = h + actionBarHeight; | |
height = h; | |
//rectCircle = new RectF(width/2, 0, (3/4)*width, height/3); | |
rectCircle = new RectF(width/4, 0, 3*width/4, height/3); | |
rectOval = new | |
RectF(width/4, 0,width/2+width/3, height/3); | |
boolean isWidthLarger = width > height/3; | |
if(isWidthLarger){ | |
radius = height/6; | |
}else{ | |
radius = width/2; | |
} | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ | |
int widthSize = MeasureSpec.getSize(widthMeasureSpec); | |
int heightSize = MeasureSpec.getSize(heightMeasureSpec); | |
int widthMode = MeasureSpec.getMode(widthMeasureSpec); | |
int heightMode = MeasureSpec.getMode(heightMeasureSpec); | |
int desiredWidth = 1000; | |
int desiredHeight = 1500; | |
int width; | |
int height; | |
//width | |
if(widthMode == MeasureSpec.EXACTLY){ | |
//Must be this size | |
width = widthSize; | |
}else if(widthMode == MeasureSpec.AT_MOST){ | |
//Can't be bigger than... | |
width = Math.min(widthSize, desiredWidth); | |
}else{ | |
//Be whatever you want | |
width = desiredWidth; | |
} | |
//height | |
if(heightMode == MeasureSpec.EXACTLY){ | |
//Must be this size | |
height = heightSize; | |
}else if(heightMode == MeasureSpec.AT_MOST){ | |
//Can't be bigger than... | |
height = Math.min(heightSize, desiredHeight); | |
}else{ | |
//Be whatever you want | |
height = desiredHeight; | |
} | |
//MUST CALL THIS | |
setMeasuredDimension(width, height); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
rect.left = 0; | |
rect.top = 0; | |
if(ori == Configuration.ORIENTATION_PORTRAIT) { | |
rect.bottom = height / 3; | |
rect.right = width; | |
RectF rect2 = new RectF(); | |
rect2.left = 0; | |
rect2.right = width; | |
rect2.bottom = height; | |
rect2.top = 0; | |
/*boolean isWidthLarger = width > height/3; | |
int r; | |
if(isWidthLarger){ | |
r = height/6; | |
}else{ | |
r = width/2; | |
}*/ | |
//canvas.drawRect(rect2, paintBackground); | |
canvas.drawRect(rect, paintBackground); | |
//canvas.drawOval(rectOval, paintShadow); | |
canvas.drawRect(rectCircle, paintShadow); | |
canvas.drawArc(rectCircle, 0, angle, true, paintV); | |
canvas.drawCircle(width/2, height/6, radius/1.2f, paintShadow); | |
}else if(ori == Configuration.ORIENTATION_LANDSCAPE){ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment