public interface DrawFigure { public void draw(Canvas canvas, PointF startPoint, PointF finishPoint, Paint mPaint); public void drawWithBorder(Canvas canvas, PointF startPoint, PointF finishPoint, Paint mPaint, Paint borderPaint); public void drawBorder(Canvas canvas, PointF startPoint, PointF finishPoint, Paint mPaint, Paint borderPaint); }
public class DrawThreeAngle implements DrawFigure { private float mRadius; private int[] angles = new int[]{30, 150, 270, 30}; private float[] xValues = new float[4]; private float[] yValues = new float[4]; @Override public void draw(Canvas canvas, PointF startPoint, PointF finishPoint, Paint mPaint) { mRadius = Tools.getRadius(startPoint, finishPoint); Path threeanglePath = new Path(); for (int i = 0; i < angles.length; i++) { xValues[i] = startPoint.x + (float) (mRadius * Math.cos(Math.toRadians(angles[i]))); yValues[i] = startPoint.y + (float) (mRadius * Math.sin(Math.toRadians(angles[i]))); if (i==0){ threeanglePath.moveTo(xValues[i], yValues[i]); } else { threeanglePath.lineTo(xValues[i], yValues[i]); } } threeanglePath.close(); canvas.drawPath(threeanglePath, mPaint); } @Override public void drawWithBorder(Canvas canvas, PointF startPoint, PointF finishPoint, Paint mPaint, Paint borderPaint) { draw(canvas, startPoint, finishPoint, mPaint); drawBorder(canvas, startPoint, finishPoint, mPaint, borderPaint); } @Override public void drawBorder(Canvas canvas, PointF startPoint, PointF finishPoint, Paint mPaint, Paint borderPaint) { for (int i = 0; i < angles.length - 1; i++) { canvas.drawLine(xValues[i], yValues[i], xValues[i + 1], yValues[i + 1], borderPaint); } } }
public class DrawStar implements DrawFigure { private float mRadius; private int[] angles = new int[]{-18, -54, 270, 234, 198, 162, 126, 90, 54, 18, -18}; private float[] xValues = new float[11]; private float[] yValues = new float[11]; @Override public void draw(Canvas canvas, PointF startPoint, PointF finishPoint, Paint mPaint) { mRadius = Tools.getRadius(startPoint, finishPoint); Path starPath = new Path(); for (int i = 0; i < angles.length; i++) { if (i % 2 == 0) { xValues[i] = startPoint.x + (float) (mRadius * Math.cos(Math.toRadians(angles[i]))); yValues[i] = startPoint.y + (float) (mRadius * Math.sin(Math.toRadians(angles[i]))); } else { xValues[i] = startPoint.x + (float) (mRadius / 2 * Math.cos(Math.toRadians(angles[i]))); yValues[i] = startPoint.y + (float) (mRadius / 2 * Math.sin(Math.toRadians(angles[i]))); } if (i==0){ starPath.moveTo(xValues[i], yValues[i]); } else { starPath.lineTo(xValues[i], yValues[i]); } } starPath.close(); canvas.drawPath(starPath, mPaint); } @Override public void drawWithBorder(Canvas canvas, PointF startPoint, PointF finishPoint, Paint mPaint, Paint borderPaint) { draw(canvas, startPoint, finishPoint, mPaint); drawBorder(canvas, startPoint, finishPoint, mPaint, borderPaint); } @Override public void drawBorder(Canvas canvas, PointF startPoint, PointF finishPoint, Paint mPaint, Paint borderPaint) { for (int i = 0; i < angles.length - 1; i++) { canvas.drawLine(xValues[i], yValues[i], xValues[i + 1], yValues[i + 1], borderPaint); } } }
public class LevelsDatabaseHelper extends SQLiteOpenHelper { private static final String DB_NAME = "levels.sqlite"; private static final int VERSION = 1; private static final String TABLE_LEVELS = "levels"; private static final String COLUMN_LEVELS_STARS = "stars"; private static final String COLUMN_LEVELS_TIME = "time"; private static final String COLUMN_LEVELS_NUMBER = "number"; private static final String TABLE_FIGURE = "figure"; private static final String COLUMN_FIGURE_TYPE = "type"; private static final String COLUMN_FIGURE_NUMBER = "number"; private static final String COLUMN_FIGURE_CENTER_X = "center_x"; private static final String COLUMN_FIGURE_CENTER_Y = "center_y"; private static final String COLUMN_FIGURE_FINISH_X = "finish_x"; private static final String COLUMN_FIGURE_FINISH_Y = "finish_y"; private static final String COLUMN_FIGURE_COLOR = "color"; private static final String COLUMN_FIGURE_LAYER_INDEX = "layer_index"; private Context mContext; private SQLiteDatabase mDataBase; public LevelsDatabaseHelper(Context context) { super(context, DB_NAME, null, 2); } @Override public void onCreate(SQLiteDatabase db) { mDataBase = db; db.execSQL("create table " + TABLE_LEVELS + " (_id integer primary key autoincrement, " + COLUMN_LEVELS_NUMBER + " integer, " + COLUMN_LEVELS_STARS + " integer, " + " string, " + COLUMN_LEVELS_TIME + " integer)"); db.execSQL("create table " + TABLE_FIGURE + " (_id integer primary key autoincrement, " + COLUMN_FIGURE_NUMBER + " integer, " + COLUMN_FIGURE_TYPE + " string, " + COLUMN_FIGURE_CENTER_X + " real, " + COLUMN_FIGURE_CENTER_Y + " real, " + COLUMN_FIGURE_FINISH_X + " real, " + COLUMN_FIGURE_FINISH_Y + " real, " + COLUMN_FIGURE_COLOR + " integer, " + COLUMN_FIGURE_LAYER_INDEX + " string)"); } }
public class ProgressBarSuccessView extends View { float mPercentValue = 0; String mLeftTitleText = "title"; int mWidth, mHeight; final float mLeftTitleSize = 0.3f, mProgressBarSize = 0.5f, mRightTitleSize = 0.2f; float mBorderMargin, mProgressMargin; RectF mProgressBarBorder = new RectF(), mProgressBarRectangle = new RectF(); int mLeftTitleX, mLeftTitleY, mRightTitleX, mRightTitleY; Paint mTitlePaint, mProgressPaint, mBorderProgressBarPaint; private Rect mTitleTextBounds = new Rect(); public ProgressBarSuccessView(Context context) { super(context); init(null); } public ProgressBarSuccessView(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); } public ProgressBarSuccessView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(attrs); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mWidth = w; mHeight = h; mTitlePaint.getTextBounds(mLeftTitleText, 0, mLeftTitleText.length(), mTitleTextBounds); mLeftTitleX = (int) ((w * mLeftTitleSize - mTitleTextBounds.width()) / 2 - mTitleTextBounds.left); mLeftTitleY = (h - mTitleTextBounds.height()) / 2 - mTitleTextBounds.top; mProgressBarBorder.set(w * mLeftTitleSize, 0 + mBorderMargin, w * (mLeftTitleSize + mProgressBarSize), h - mBorderMargin); } private void init(AttributeSet attrs) { TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ProgressBarTitle); mLeftTitleText = a.getString(R.styleable.ProgressBarTitle_progress_bar_title); mBorderMargin = getResources().getDimension(R.dimen.border_progress_bar_margin); mProgressMargin = getResources().getDimension(R.dimen.progress_bar_margin); mTitlePaint = new Paint(Paint.ANTI_ALIAS_FLAG); mTitlePaint.setColor(getResources().getColor(R.color.title_level_success_view)); mTitlePaint.setStyle(Paint.Style.STROKE); mTitlePaint.setTextSize(getResources().getDimension(R.dimen.progress_bar_title_text_size)); mTitlePaint.setTypeface(FontManager.BEBAS_REGULAR); mBorderProgressBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mBorderProgressBarPaint.setColor(getResources().getColor(R.color.title_level_success_view)); mBorderProgressBarPaint.setStyle(Paint.Style.STROKE); mBorderProgressBarPaint.setStrokeWidth(2); mProgressPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mProgressPaint.setColor(getResources().getColor(R.color.progress_bar_level_success_view)); mProgressPaint.setStyle(Paint.Style.FILL); setWillNotDraw(false); } public void setPercent(float percent) { mPercentValue = Math.round(percent * 100f) / 100f; invalidate(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); float left, top, right, bottom; left = mWidth * mLeftTitleSize + mProgressMargin - mBorderMargin; top = 0 + mProgressMargin; right = mPercentValue > 0 ? left + mWidth * mProgressBarSize * mPercentValue - mProgressMargin - mBorderMargin : left; bottom = mHeight - mProgressMargin; mProgressBarRectangle.set(left, top, right, bottom); canvas.drawText(mLeftTitleText, mLeftTitleX, mLeftTitleY, mTitlePaint); canvas.drawRect(mProgressBarBorder, mBorderProgressBarPaint); canvas.drawRect(mProgressBarRectangle, mProgressPaint); mTitlePaint.getTextBounds(String.valueOf((int) (mPercentValue * 100)), 0, String.valueOf((int) (mPercentValue * 100)).length(), mTitleTextBounds); mRightTitleX = (int) (mWidth - mTitleTextBounds.width() / 2 - mWidth * mRightTitleSize / 2); mRightTitleY = (mHeight - mTitleTextBounds.height()) / 2 - mTitleTextBounds.top; canvas.drawText(String.valueOf((int) (mPercentValue * 100)), mRightTitleX, mRightTitleY, mTitlePaint); } }
public static AnimatorSet createProgressAnimationSet(ArrayList<View> views, float[] percentValues) { AnimatorSet animatorSet = new AnimatorSet(); List<Animator> animatorList = new ArrayList<>(views.size()); for (int i = 0; i < views.size(); i++){ float value = percentValues[i]; View view = views.get(i); animatorList.add(createProgressAnimation(view, value)); } animatorSet.playTogether(animatorList); return animatorSet; } private static ObjectAnimator createProgressAnimation(View view, float progressValue){ ObjectAnimator shapeProgressAnimator = ObjectAnimator.ofFloat(view, "percent", 0, progressValue); shapeProgressAnimator.setDuration((long) (1000 * progressValue)); shapeProgressAnimator.setInterpolator(new DecelerateInterpolator()); return shapeProgressAnimator; }
Source: https://habr.com/ru/post/274277/
All Articles