Java tutorial
/* * Copyright (c) 2016. by Hoang Hiep (hoanghiep8899@gmail.com) * This file SizeDrawable.java is part of File Manager * Create at 3/6/16 2:19 PM * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.filemanager.free.ui.views; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.RectF; import android.support.v4.content.ContextCompat; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.view.View; import com.filemanager.free.R; /** * Created by Arpit on 30-07-2015. */ public class SizeDrawable extends View { Paint mPaint, mPaint1, mPaint2; RectF rectF; float startangle = -90, angle = 0; float startangle1 = -90, angle1 = 0; float startangle2 = -90, angle2 = 0; public SizeDrawable(Context context) { super(context); } int twenty; public SizeDrawable(Context context, AttributeSet attributeSet) { super(context, attributeSet); int strokeWidth = dpToPx(40); rectF = new RectF(getLeft(), getTop(), getRight(), getBottom()); //rectF = new RectF(dpToPx(0), dpToPx(0), dpToPx(200), dpToPx(200)); mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(ContextCompat.getColor(getContext(), R.color.accent_indigo)); // mPaint.setStrokeCap(Paint.Cap.BUTT); mPaint.setStrokeWidth(strokeWidth); mPaint1 = new Paint(); mPaint1.setAntiAlias(true); mPaint1.setStyle(Paint.Style.FILL); mPaint1.setColor(ContextCompat.getColor(getContext(), R.color.accent_red)); // mPaint1.setStrokeCap(Paint.Cap.BUTT); mPaint1.setStrokeWidth(strokeWidth); mPaint2 = new Paint(); mPaint2.setAntiAlias(true); mPaint2.setStyle(Paint.Style.FILL); mPaint2.setColor(ContextCompat.getColor(getContext(), R.color.accent_green)); // mPaint2.setStrokeCap(Paint.Cap.BUTT); mPaint2.setStrokeWidth(strokeWidth); twenty = dpToPx(10); } DisplayMetrics displayMetrics; public int dpToPx(int dp) { if (displayMetrics == null) displayMetrics = getResources().getDisplayMetrics(); int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT)); return px; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // canvas.drawLine((getWidth() - twenty)-2,0,(getWidth() - twenty),0,mPaint1); if (angle2 != 0) canvas.drawLine(0, getHeight() - (getHeight() * angle1), 0, getHeight() - (getHeight() * angle2), mPaint2); canvas.drawLine(0, getHeight(), 0, getHeight() - (getHeight() * angle), mPaint); if (angle1 != 0) canvas.drawLine(0, getHeight() - (getHeight() * angle), 0, getHeight() - (getHeight() * angle1), mPaint1); /* Paint paint = new Paint(); paint.setColor(Color.WHITE); paint.setStyle(Paint.Style.STROKE); paint.setTextSize(20); canvas.drawText(Math.round(angle * 100)+"%",(getWidth() - twenty)*angle/2, 25,paint); if(angle1>0.20)canvas.drawText(Math.round((angle1-angle)*100)+"%",(getWidth() - twenty)*angle+(getWidth() - twenty)*(angle1-angle)/2, 25,paint); if(angle2>0.20)canvas.drawText(Math.round((angle2-angle1)*100)+"%",(getWidth() - twenty)*angle1+(getWidth() - twenty)*(angle2-angle1)/2, 25,paint); canvas.drawArc(rectF, startangle, angle, true, mPaint); canvas.drawArc(rectF, startangle1, angle1, true, mPaint1); canvas.drawArc(rectF, startangle2, angle2, true, mPaint2); */ } public void setAngle(float angle, float startangle) { this.angle = angle; this.startangle = startangle; } public void setAngle1(float angle, float startangle1) { this.angle1 = angle; this.startangle1 = startangle1; } public void setAngle2(float angle2, float startangle2) { this.angle2 = angle2; this.startangle2 = startangle2; } }