How to disable not showing today's date in previous calendar? - java

I am using custom calendar where everything works fine.
For eg: If today's date is 8th February.
In February, it shows 8th as different color which is today's date and others are in black color.
But when swipped to previous month January, date 8 alone is showing. How to remove the today's date in previous month?
Here is screenshot
When i click to previous month,you can see 8 alone which is current today's date is showing in previous month.
Here is code:
public class CalendarNumbersView extends View {
public static final int MAX_WEEKS_IN_MONTH = 7;
private float MAX_SELECTION_FINGER_SHIFT_DIST = 5.0f;
private TextPaint paint;
private int cellPadding;
private int textColor;
private int inactiveTextColor;
private int selectionTextColor;
private int cellBackgroundColor;
private int cellSelectionBackgroundColor;
private int dayNamesTextColor;
private int dayNamesBackgroundColor;
private boolean showDayNames = true;
private Locale locale = Locale.getDefault();
private Calendar selectedDate;
private Calendar shownMonth;
private DateSelectionListener listener = null;
//temporary and cache values
private int _cachedCellSideWidth = 0;
private int _cachedCellSideHeight = 0;
private Calendar _calendar = Calendar.getInstance();
private Rect _rect = new Rect();
private float _textHeight = 0;
private float _x;
private float _y;
private Typeface _boldTypeface;
private Typeface _defaultTypeface;
public interface DateSelectionListener {
void onDateSelected(Calendar selectedDate);
}
public static class CalendarDayCellCoord {
public int col;
public int row;
public CalendarDayCellCoord(int col, int row) {
this.col = col;
this.row = row;
}
}
public CalendarNumbersView(Context context) {
super(context);
init(null);
}
public CalendarNumbersView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public CalendarNumbersView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
private void init(AttributeSet attrs) {
paint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
paint.setTextSize(getResources().getDimensionPixelSize(R.dimen.calendar_default_text_size));
textColor = getResources().getColor(R.color.calendar_default_text_color);
//changed by adding color in inactive text.(previous month days)
inactiveTextColor = Color.parseColor("#FFFFFFFF");
selectionTextColor = getResources().getColor(R.color.calendar_default_selection_text_color);
cellPadding = getResources().getDimensionPixelSize(R.dimen.calendar_default_cell_padding);
cellBackgroundColor = getResources().getColor(R.color.calendar_default_cell_background_color);
//cellSelectionBackgroundColor = getResources().getColor(R.color.calendar_default_cell_selection_background_color);
dayNamesTextColor = getResources().getColor(R.color.calendar_default_day_names_cell_text_color);
dayNamesBackgroundColor = getResources().getColor(R.color.calendar_default_day_names_cell_background_color);
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.CalendarNumbersView);
if (ta != null) {
paint.setTextSize(ta.getDimensionPixelSize(R.styleable.CalendarNumbersView_fontSize, (int) paint.getTextSize()));
textColor = ta.getColor(R.styleable.CalendarNumbersView_textColor, textColor);
inactiveTextColor = ta.getColor(R.styleable.CalendarNumbersView_inactiveTextColor, inactiveTextColor);
selectionTextColor = ta.getColor(R.styleable.CalendarNumbersView_selectionTextColor, selectionTextColor);
cellPadding = ta.getDimensionPixelSize(R.styleable.CalendarNumbersView_cellPadding, cellPadding);
cellBackgroundColor = ta.getColor(R.styleable.CalendarNumbersView_cellBackgroundColor, cellBackgroundColor);
cellSelectionBackgroundColor = ta.getColor(R.styleable.CalendarNumbersView_cellSelectionBackgroundColor, cellSelectionBackgroundColor);
dayNamesTextColor = ta.getColor(R.styleable.CalendarNumbersView_cellDayNamesCellTextColor, dayNamesTextColor);
dayNamesBackgroundColor = ta.getColor(R.styleable.CalendarNumbersView_cellDayNamesCellBackgroundColor, dayNamesBackgroundColor);
}
selectedDate = Calendar.getInstance();
shownMonth = (Calendar) selectedDate.clone();
}
public int calculateQuadCellSideWidth() {
Rect bounds = new Rect();
String str = "WW";//widest possible cell string
paint.getTextBounds(str, 0, str.length(), bounds);
int maxWidth = bounds.width();
int maxHeight = bounds.height();
_textHeight = bounds.height();
return Math.max(maxWidth, maxHeight) + cellPadding * 2;
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int quadCellSideWidth = calculateQuadCellSideWidth();
int calculatedWidth = quadCellSideWidth * shownMonth.getActualMaximum(Calendar.DAY_OF_WEEK) + getPaddingLeft() + getPaddingRight();
int calculatedHeight = quadCellSideWidth * MAX_WEEKS_IN_MONTH + getPaddingTop() + getPaddingBottom();
if (showDayNames) {
calculatedHeight += quadCellSideWidth;
}
int minimumWidth = Math.max(getSuggestedMinimumWidth(), calculatedWidth);
int minimumHeight = Math.max(getSuggestedMinimumHeight(), calculatedHeight);
int width = chooseSize(minimumWidth, widthMeasureSpec);
int height = chooseSize(minimumHeight, heightMeasureSpec);
setMeasuredDimension(width, height);
}
public int chooseSize(int size, int measureSpec) {
int result = size;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
switch (specMode) {
case MeasureSpec.UNSPECIFIED:
case MeasureSpec.AT_MOST:
result = size;
break;
case MeasureSpec.EXACTLY:
result = specSize;
break;
}
return result;
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
_cachedCellSideWidth = (w - getPaddingRight() - getPaddingLeft()) / shownMonth.getActualMaximum(Calendar.DAY_OF_WEEK);
_cachedCellSideHeight = (h - getPaddingTop() - getPaddingBottom()) / MAX_WEEKS_IN_MONTH;
super.onSizeChanged(w, h, oldw, oldh);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (showDayNames) {
setCalendarToFirstVisibleDay(_calendar);
DateFormatSymbols symbols = new DateFormatSymbols(locale);
for (int col = 0; col < _calendar.getActualMaximum(Calendar.DAY_OF_WEEK); col++) {
String str = _calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, locale);
String name = str.substring(0, str.length() - 1);
drawCell(canvas, -1, col, dayNamesTextColor, dayNamesBackgroundColor, name, true);
_calendar.add(Calendar.DAY_OF_MONTH, 1);
}
}
setCalendarToFirstVisibleDay(_calendar);
for (int row = 0; row < MAX_WEEKS_IN_MONTH; row++) {
for (int col = 0; col < _calendar.getActualMaximum(Calendar.DAY_OF_WEEK); col++) {
int textColor;
int backgroundColor;
if (_calendar.get(Calendar.DAY_OF_YEAR) == selectedDate.get(Calendar.DAY_OF_YEAR) &&
_calendar.get(Calendar.YEAR) == selectedDate.get(Calendar.YEAR)) {
//here is the logic applied. If both are same, apply color. But why it's affecting in previous calendar.
textColor = selectionTextColor;
backgroundColor = cellSelectionBackgroundColor;
} else {
if (_calendar.get(Calendar.MONTH) == shownMonth.get(Calendar.MONTH)) {
textColor = this.textColor;
} else {
textColor = inactiveTextColor;
}
backgroundColor = cellBackgroundColor;
}
int day = _calendar.get(Calendar.DAY_OF_MONTH);
String str = Integer.toString(day);
drawCell(canvas, row, col, textColor, backgroundColor, str, false);
_calendar.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
private void drawCell(Canvas canvas, int row, int col, int textColor, int backgroundColor, String str, boolean bold) {
getRectForCell(col, row, _rect);
paint.setColor(backgroundColor);
_rect.inset(cellPadding, cellPadding);
canvas.drawRect(_rect, paint);
_rect.inset(-cellPadding, -cellPadding);
paint.setColor(textColor);
if (bold) {
if (_boldTypeface == null) {
_boldTypeface = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL);//su,mo,tu,wed
}
paint.setTypeface(_boldTypeface);
} else {
if (_defaultTypeface == null) {
_defaultTypeface = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL);
}
paint.setTypeface(_defaultTypeface);
}
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText(str,
_rect.left + _cachedCellSideWidth / 2f,
_rect.top + _cachedCellSideHeight / 2f + _textHeight / 2f - paint.getFontMetrics().descent / 2,
paint);
}
private void setCalendarToFirstVisibleDay(Calendar calendar) {
calendar.setTime(shownMonth.getTime());
calendar.set(Calendar.DAY_OF_MONTH, 1);
int firstDayInWeek = calendar.getFirstDayOfWeek();
int firstDayOfWeekOfCurrentMonth = calendar.get(Calendar.DAY_OF_WEEK);
int shift;
if (firstDayInWeek > firstDayOfWeekOfCurrentMonth) {
shift = -(firstDayOfWeekOfCurrentMonth + calendar.getActualMaximum(Calendar.DAY_OF_WEEK) - firstDayInWeek);
} else {
shift = -(firstDayOfWeekOfCurrentMonth - firstDayInWeek);
}
calendar.add(Calendar.DAY_OF_MONTH, shift);
}
private void getRectForCell(int col, int row, Rect outRect) {
if (showDayNames) {
row++;
}
outRect.set(getPaddingLeft() + col * _cachedCellSideWidth,
getPaddingTop() + row * _cachedCellSideHeight,
getPaddingLeft() + col * _cachedCellSideWidth + _cachedCellSideWidth,
getPaddingTop() + row * _cachedCellSideHeight + _cachedCellSideHeight);
}
private CalendarDayCellCoord getCellForCoords(float x, float y) {
if (x < getPaddingLeft() ||
x >= getWidth() - getPaddingRight() ||
y < getPaddingTop() ||
y >= getHeight() - getPaddingBottom()) {
return null;
}
CalendarDayCellCoord coord = new CalendarDayCellCoord(
(int) (x - getPaddingLeft()) / _cachedCellSideWidth,
(int) (y - getPaddingTop()) / _cachedCellSideHeight
);
if (showDayNames) {
coord.row--;
if (coord.row < 0) {
return null;
}
}
return coord;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
_x = event.getX();
_y = event.getY();
return true;
case MotionEvent.ACTION_UP:
float x = event.getX();
float y = event.getY();
if (Math.sqrt(Math.pow(x - _x, 2) + Math.pow(y - _y, 2)) <= MAX_SELECTION_FINGER_SHIFT_DIST) {
selectDayAt(x, y);
}
return true;
default:
return super.onTouchEvent(event);
}
}
private void selectDayAt(float x, float y) {
CalendarDayCellCoord cellCoords = getCellForCoords(x, y);
if (cellCoords == null) {
return;
}
setCalendarToFirstVisibleDay(_calendar);
_calendar.add(Calendar.DAY_OF_YEAR, cellCoords.col);
_calendar.add(Calendar.WEEK_OF_MONTH, cellCoords.row);
selectedDate.setTime(_calendar.getTime());
if (listener != null) {
listener.onDateSelected(selectedDate);
}
invalidate();
}
public int getCellBackgroundColor() {
return cellBackgroundColor;
}
public void setCellBackgroundColor(int cellBackgroundColor) {
this.cellBackgroundColor = cellBackgroundColor;
invalidate();
}
public int getCellPadding() {
return cellPadding;
}
public void setCellPadding(int cellPadding) {
this.cellPadding = cellPadding;
invalidate();
}
public int getCellSelectionBackgroundColor() {
return cellSelectionBackgroundColor;
}
public void setCellSelectionBackgroundColor(int cellSelectionBackgroundColor) {
this.cellSelectionBackgroundColor = cellSelectionBackgroundColor;
invalidate();
}
public int getInactiveTextColor() {
return inactiveTextColor;
}
public void setInactiveTextColor(int inactiveTextColor) {
this.inactiveTextColor = inactiveTextColor;
invalidate();
}
public DateSelectionListener getListener() {
return listener;
}
public void setListener(DateSelectionListener listener) {
this.listener = listener;
}
public Calendar getSelectedDate() {
return selectedDate;
}
public void setSelectedDate(Calendar selectedDate) {
this.selectedDate = selectedDate;
invalidate();
}
public int getSelectionTextColor() {
return selectionTextColor;
}
public void setSelectionTextColor(int selectionTextColor) {
this.selectionTextColor = selectionTextColor;
invalidate();
}
public int getTextColor() {
return textColor;
}
public void setTextColor(int textColor) {
this.textColor = textColor;
invalidate();
}
public Calendar getShownMonth() {
return shownMonth;
}
public void setShownMonth(Calendar shownMonth) {
this.shownMonth = shownMonth;
invalidate();
}
public boolean isShowDayNames() {
return showDayNames;
}
public void setShowDayNames(boolean showDayNames) {
this.showDayNames = showDayNames;
invalidate();
}
public Locale getLocale() {
return locale;
}
public void setLocale(Locale locale) {
this.locale = locale;
invalidate();
}
public int getDayNamesBackgroundColor() {
return dayNamesBackgroundColor;
}
public void setDayNamesBackgroundColor(int dayNamesBackgroundColor) {
this.dayNamesBackgroundColor = dayNamesBackgroundColor;
invalidate();
}
public int getDayNamesTextColor() {
return dayNamesTextColor;
}
public void setDayNamesTextColor(int dayNamesTextColor) {
this.dayNamesTextColor = dayNamesTextColor;
invalidate();
}
}
If you see the above code,at one particular line, I have applied color only in current month of today's date. But why it's displaying in previous month?
[extracted the above specific code lines..]
if (_calendar.get(Calendar.DAY_OF_YEAR) == selectedDate.get(Calendar.DAY_OF_YEAR) &&
_calendar.get(Calendar.YEAR) == selectedDate.get(Calendar.YEAR)) {
//here is the logic applied. If both are same, apply color. But why it's affecting in previous calendar.
textColor = selectionTextColor;//today's date
backgroundColor = cellSelectionBackgroundColor;
}

Update the condition for check the month also
if (_calendar.get(Calendar.DAY_OF_YEAR) == selectedDate.get(Calendar.DAY_OF_YEAR) &&
_calendar.get(Calendar.YEAR) == selectedDate.get(Calendar.YEAR)
&& _calendar.get(Calendar.MONTH) == shownMonth.get(Calendar.MONTH)) {
textColor = selectionTextColor;
backgroundColor = cellSelectionBackgroundColor;
} else {
if (_calendar.get(Calendar.MONTH) == shownMonth.get(Calendar.MONTH)) {
textColor = this.textColor;
} else {
textColor = inactiveTextColor;
}
backgroundColor = cellBackgroundColor;
}

Related

How to insert an image in a paint method

I try to make a game in android studio (for an univeristy project) and I have some problems.
The game in question is a maze game type. I create the maze and the player, it work but I would to insert an image for the player, but I don't know how.
This is the code of the entire GameView.
public class GameView extends View {
private enum Direction {
UP, DOWN, LEFT, RIGHT
}
private Cell[][] cells;
private Cell player, exit;
private static final int COLS = 7 , ROWS = 10;
private static final float WALL_THICKNESS = 7;
private float cellSize, hMargin, vMargin;
private Paint wallPaint, exitPaint,playerPaint;
private Random random;
public GameView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
wallPaint = new Paint();
wallPaint.setColor(Color.BLACK);
wallPaint.setStrokeWidth(WALL_THICKNESS);
playerPaint = new Paint();
playerPaint.setColor(Color.RED);
exitPaint = new Paint();
exitPaint.setColor(Color.BLUE);
random = new Random();
createMaze();
}
private Cell getNeighbour(Cell cell){
ArrayList<Cell> neighbours = new ArrayList<>();
if(cell.col> 0){
if(!cells[cell.col-1][cell.row].visited){
neighbours.add(cells[cell.col-1][cell.row]);
}
}
if(cell.col < COLS-1){
if(!cells[cell.col+1][cell.row].visited){
neighbours.add(cells[cell.col+1][cell.row]);
}
}
if(cell.row > 0){
if(!cells[cell.col][cell.row-1].visited){
neighbours.add(cells[cell.col][cell.row-1]);
}
}
if(cell.row < ROWS-1){
if(!cells[cell.col][cell.row+1].visited){
neighbours.add(cells[cell.col][cell.row+1]);
}
}
if(neighbours.size() > 0){
int index = random.nextInt(neighbours.size());
return neighbours.get(index);
}
return null;
}
private void removeWall(Cell current, Cell next){
if(current.col == next.col && current.row == next.row+1){
current.topWall = false;
next.bottomWall = false;
}
if(current.col == next.col && current.row == next.row-1){
current.bottomWall = false;
next.topWall = false;
}
if(current.col == next.col+1 && current.row == next.row){
current.leftWall = false;
next.rightWall = false;
}
if(current.col == next.col-1 && current.row == next.row){
current.rightWall = false;
next.leftWall = false;
}
}
private void createMaze(){
Stack<Cell> stack = new Stack<>();
Cell current, next;
cells= new Cell[COLS][ROWS];
for(int x=0;x<COLS;x++){
for(int y=0;y<ROWS;y++){
cells[x][y] = new Cell(x, y);
}
}
player = cells[0][0];
exit = cells[COLS-1][ROWS-1];
current = cells[0][0];
current.visited = true;
do {
next = getNeighbour(current);
if (next != null) {
removeWall(current, next);
stack.push(current);
current = next;
current.visited = true;
} else {
current = stack.pop();
}
}while(!stack.empty());
}
#Override
protected void onDraw(Canvas canvas) {
//canvas.drawColor(Color.GREEN);
int width = getWidth();
int height = getHeight();
if (width / COLS > height / ROWS){
cellSize = height / (ROWS + 1);
}
else {
cellSize = width / (COLS + 1);
}
hMargin = (width - COLS*cellSize)/2;
vMargin = (height - ROWS*cellSize)/2;
for(int x=0;x<COLS;x++){
for(int y=0;y<ROWS;y++){
if(cells[x][y].topWall){
canvas.drawLine(
x*cellSize,
y*cellSize,
(x+1)*cellSize,
y*cellSize,
wallPaint);
}
if(cells[x][y].leftWall){
canvas.drawLine(
x*cellSize,
y*cellSize,
x*cellSize,
(y+1)*cellSize,
wallPaint);
}
if(cells[x][y].bottomWall){
canvas.drawLine(
x*cellSize,
(y+1)*cellSize,
(x+1)*cellSize,
(y+1)*cellSize,
wallPaint);
}
if(cells[x][y].rightWall){
canvas.drawLine(
(x+1)*cellSize,
y*cellSize,
(x+1)*cellSize,
(y+1)*cellSize,
wallPaint);
}
}
}
float margin = cellSize/20;
canvas.drawRect(
player.col*cellSize+margin,
player.row*cellSize+margin,
(player.col+1)*cellSize-margin,
(player.row+1)*cellSize-margin,
playerPaint
);
canvas.drawRect(
exit.col*cellSize+margin,
exit.row*cellSize+margin,
(exit.col+1)*cellSize-margin,
(exit.row+1)*cellSize-margin,
exitPaint
);
}
private void movePlayer(Direction direction){
switch (direction){
case UP :
if(!player.topWall)
player = cells[player.col][player.row-1];
break;
case DOWN :
if(!player.bottomWall)
player = cells[player.col][player.row+1];
break;
case LEFT :
if(!player.leftWall)
player = cells[player.col-1][player.row];
break;
case RIGHT :
if(!player.rightWall)
player = cells[player.col+1][player.row];
break;
}
invalidate();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN)
return true;
if(event.getAction() == MotionEvent.ACTION_MOVE){
float x = event.getX();
float y = event.getY();
float playerCenterX = hMargin + (player.col+0.5f)*cellSize;
float playerCenterY = vMargin + (player.col+0.5f)*cellSize;
float dx = x - playerCenterX;
float dy = y - playerCenterY;
float absDx = Math.abs(dx);
float absDy = Math.abs(dy);
if(absDx > cellSize || absDy > cellSize){
if(absDx > absDy){
if(dx>0){
movePlayer(Direction.RIGHT);
}else{
movePlayer(Direction.LEFT);
}
}
else{
if(dy>0){
movePlayer(Direction.DOWN);
}else{
movePlayer(Direction.UP);
}
}
}
return true;
}
return super.onTouchEvent(event);
}
private static class Cell{
boolean
topWall = true,
leftWall = true,
bottomWall = true,
rightWall = true,
visited = false;
int col, row;
public Cell(int col, int row) {
this.col = col;
this.row = row;
}
}
}
I try to do it with bitmap but when I start the app, it crash.

I have an arc I want to draw an arrow around which is animated just like the arc is

The arrow around the arc is what I want to draw
Here is the code for the animated arc.
The code I have added draws an animated arc to the point of touch on another arc. The arc fills up with the provided gradient color to the point of touch on the arc. The filling up of the arc is an animation. The code for the animation works. I want to draw an arrow around the arc which follows the arc to the point where it is filled. The end result has to be an arrow which follows the arc.
Please help me with this.
//Rectangle for the arc
private RectF mArcRect = new RectF();
//Paints required for drawing
private Paint mArcPaint;
private Paint mArcProgressPaint;
private Paint mTickPaint;
private Paint mTickProgressPaint;
private Paint mTickTextPaint;
private Paint mTickTextColoredPaint;
private Paint linePaint;
//Arc related dimens
private int mArcRadius = 0;
private int mArcWidth = 2;
private int mArcProgressWidth = 18;
private boolean mRoundedEdges = true;
//Thumb Drawable
private Drawable mThumb;
//Thumb position related coordinates
private int mTranslateX;
private int mTranslateY;
private int mThumbXPos;
private int mThumbYPos;
private int mAngleTextSize = 12;
private int LOWER_LIMIT = -16;
private int mTickOffset = 12;
private int mTickLength = 10;
private int mTickWidth = 2;
private int mTickProgressWidth = 2;
private int mAngle = LOWER_LIMIT;
private boolean mTouchInside = true;
private boolean mEnabled = true;
private TicksBetweenLabel mTicksBetweenLabel = TicksBetweenLabel.TWO;
private int mTickIntervals = 15;
private double mTouchAngle = 0;
private float mTouchIgnoreRadius;
private int sweepAngle = 0;
private int oldAngle = 0;
private int bottomLeft;
private int top;
private long startClickTime = 0;
//Event listener
private OnProtractorViewChangeListener mOnProtractorViewChangeListener = null;
//Interface for event listener
public interface OnProtractorViewChangeListener {
void onProgressChanged(ProtractorView protractorView, double progress, boolean fromUser);
void onStartTrackingTouch(ProtractorView protractorView);
void onStopTrackingTouch(ProtractorView protractorView);
}
public enum TicksBetweenLabel {
ZERO,
ONE,
TWO,
THREE
}
public ProtractorView(Context context) {
super(context);
init(context, null, 0);
}
public ProtractorView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, R.attr.protractorViewStyle);
}
public ProtractorView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
final Resources res = getResources();
int textColor = res.getColor(R.color.progress_gray);
int textProgressColor = res.getColor(R.color.default_blue_light);
int tickColor = res.getColor(R.color.progress_gray);
int tickProgressColor = res.getColor(R.color.default_blue_light);
int thumbHalfHeight;
int thumbHalfWidth;
mThumb = res.getDrawable(R.drawable.thumb_selector);
mArcWidth = (int)(mArcWidth * DENSITY);
mArcProgressWidth = (int)(mArcProgressWidth * DENSITY);
mAngleTextSize = (int)(mAngleTextSize * DENSITY);
mTickOffset = (int)(mTickOffset * DENSITY);
mTickLength = (int)(mTickLength * DENSITY);
mTickWidth = (int)(mTickWidth * DENSITY);
mTickProgressWidth = (int)(mTickProgressWidth * DENSITY);
if (attrs != null) {
final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ProtractorView, defStyle, 0);
Drawable thumb = array.getDrawable(R.styleable.ProtractorView_thumb);
if (thumb != null) {
mThumb = thumb;
}
thumbHalfHeight = mThumb.getIntrinsicHeight() / 2;
thumbHalfWidth = mThumb.getIntrinsicWidth() / 2;
mThumb.setBounds(-thumbHalfWidth, -thumbHalfHeight, thumbHalfWidth, thumbHalfHeight);
//Dimensions
mAngleTextSize = (int) array.getDimension(R.styleable.ProtractorView_angleTextSize, mAngleTextSize);
mArcProgressWidth = (int) array.getDimension(R.styleable.ProtractorView_progressWidth, mArcProgressWidth);
mTickOffset = (int) array.getDimension(R.styleable.ProtractorView_tickOffset, mTickOffset);
mTickLength = (int) array.getDimension(R.styleable.ProtractorView_tickLength, mTickLength);
mArcWidth = (int) array.getDimension(R.styleable.ProtractorView_arcWidth, mArcWidth);
//Integers
mAngle = array.getInteger(R.styleable.ProtractorView_angle, mAngle);
mTickIntervals = array.getInt(R.styleable.ProtractorView_tickIntervals, mTickIntervals);
//Colors
textColor = array.getColor(R.styleable.ProtractorView_textColor, textColor);
textProgressColor = array.getColor(R.styleable.ProtractorView_textProgressColor, textProgressColor);
tickColor = array.getColor(R.styleable.ProtractorView_tickColor, tickColor);
tickProgressColor = array.getColor(R.styleable.ProtractorView_tickProgressColor, tickProgressColor);
//Boolean
mRoundedEdges = array.getBoolean(R.styleable.ProtractorView_roundEdges, mRoundedEdges);
mEnabled = array.getBoolean(R.styleable.ProtractorView_enabled, mEnabled);
mTouchInside = array.getBoolean(R.styleable.ProtractorView_touchInside, mTouchInside);
int ordinal = array.getInt(R.styleable.ProtractorView_ticksBetweenLabel, mTicksBetweenLabel.ordinal());
mTicksBetweenLabel = TicksBetweenLabel.values()[ordinal];
}
/**
* Creating and configuring the paints as required.
*/
mAngle = (mAngle > MAX) ? MAX : ((mAngle < 0) ? LOWER_LIMIT : mAngle);
int[] colors = buildColorArray("60");
mArcPaint = new Paint();
mArcPaint.setShader(new LinearGradient(0, 0, 550, 0, colors, null, Shader.TileMode.MIRROR));
mArcPaint.setAntiAlias(true);
mArcPaint.setStyle(Paint.Style.STROKE);
mArcPaint.setStrokeWidth(mArcWidth);
int[] foregroundColors = buildColorArray("");
mArcProgressPaint = new Paint();
mArcProgressPaint.setShader(new LinearGradient(0, 0, 550, 0, foregroundColors, null, Shader.TileMode.MIRROR));
mArcProgressPaint.setAntiAlias(true);
mArcProgressPaint.setStyle(Paint.Style.STROKE);
mArcProgressPaint.setStrokeWidth(mArcProgressWidth);
if (mRoundedEdges) {
mArcPaint.setStrokeCap(Paint.Cap.ROUND);
mArcProgressPaint.setStrokeCap(Paint.Cap.ROUND);
}
mTickPaint = new Paint();
mTickPaint.setColor(tickColor);
mTickPaint.setAntiAlias(true);
mTickPaint.setStyle(Paint.Style.STROKE);
mTickPaint.setStrokeWidth(mTickWidth);
mTickProgressPaint = new Paint();
mTickProgressPaint.setColor(tickProgressColor);
mTickProgressPaint.setAntiAlias(true);
mTickProgressPaint.setStyle(Paint.Style.STROKE);
mTickProgressPaint.setStrokeWidth(mTickProgressWidth);
mTickTextPaint = new Paint();
mTickTextPaint.setColor(textColor);
mTickTextPaint.setAntiAlias(true);
mTickTextPaint.setStyle(Paint.Style.FILL);
mTickTextPaint.setTextSize(mAngleTextSize);
mTickTextPaint.setTextAlign(Paint.Align.CENTER);
mTickTextColoredPaint = new Paint();
mTickTextColoredPaint.setColor(textProgressColor);
mTickTextColoredPaint.setAntiAlias(true);
mTickTextColoredPaint.setStyle(Paint.Style.FILL);
mTickTextColoredPaint.setTextSize(mAngleTextSize);
mTickTextColoredPaint.setTextAlign(Paint.Align.CENTER);
linePaint = new Paint();
linePaint.setColor(Color.BLACK);
linePaint.setAntiAlias(true);
linePaint.setStyle(Paint.Style.STROKE);
}
private int[] buildColorArray(String alpha) {
//#ffc0c0
//#fd708a
//#54fcff
// String [] colors = {"#" + alpha + "60f0f6", "#" + alpha + "fd708a", "#" + alpha + "fd708a", "#" + alpha + "60f0f6", "#" + alpha + "d683a0", "#" + alpha + "60f0f6"};
String[] colors = {
"#" + alpha + "60f0f6",
"#" + alpha + "69dee9",
"#" + alpha + "99bbcd",
"#" + alpha + "fd708a",
"#" + alpha + "f8849b"
};
int[] colorValues = new int[colors.length]; //#59dae1
int j = 0;
for (int i = 0; i < colors.length; i++) {
colorValues[j++] = Color.parseColor(colors[i]); //60f0f6, 54fcff, fd708a
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "Color value: " + colors[i]);
}
}
return colorValues;
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = getDefaultSize(getSuggestedMinimumHeight(),
heightMeasureSpec);
int width = getDefaultSize(getSuggestedMinimumWidth(),
widthMeasureSpec);
int min = Math.min(width, height);
//width = min;
height = min / 2;
int arcDiameter = 0;
int tickEndToArc = (mTickOffset + mTickLength);
int radiusConstant = 30;
arcDiameter = min - 2 * tickEndToArc + radiusConstant;
arcDiameter = (int)(arcDiameter - 2 * 20 * DENSITY);
mArcRadius = arcDiameter / 2;
top = height - (mArcRadius);
bottomLeft = width / 2 - mArcRadius;
mArcRect.set(bottomLeft, top, bottomLeft + arcDiameter, top + arcDiameter);
mTranslateX = (int) mArcRect.centerX();
mTranslateY = (int) mArcRect.centerY();
int thumbAngle = mAngle;
mThumbXPos = (int)(mArcRadius * Math.cos(Math.toRadians(thumbAngle)));
mThumbYPos = (int)(mArcRadius * Math.sin(Math.toRadians(thumbAngle)));
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "co ordinate = x = " + mThumbXPos + " y = " + mThumbYPos + " mAngle = " + mAngle);
}
setTouchInside(mTouchInside);
setMeasuredDimension(width, height + tickEndToArc + 33);
}
#SuppressLint("DrawAllocation")
#Override
protected void onDraw(Canvas canvas) {
if (mAngle > 0) {
mAngle = mAngle - 360;
}
if (mAngle <= 0 && mAngle > LOWER_LIMIT) {
mAngle = LOWER_LIMIT;
}
if (mTouchAngle > START_ANGLE && mTouchAngle < THRESHOLD_VALUE) {
mAngle = LOWER_LIMIT;
}
sweepAngle = mAngle;
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "The Sweep Angle is : " + sweepAngle);
}
if (sweepAngle < -280) {
mAngle = LOWER_LIMIT;
}
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "Max angle " + mAngle);
}
if ((double)(mAngle / 13) > 19) {
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "Inside If condition to set Complete angle");
}
mAngle = COMPLETE_ANGLE;
}
canvas.save();
canvas.scale(1, -1, mArcRect.centerX(), mArcRect.centerY());
canvas.drawArc(mArcRect, 0, MAX, false, mArcPaint);
canvas.drawArc(mArcRect, (int) START_ANGLE, mAngle, false, mArcProgressPaint);
canvas.restore();
double slope, startTickX, startTickY, endTickX, endTickY, midTickX, midTickY, thetaInRadians;
double radiusOffset = mArcRadius + mTickOffset;
}
#Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (mThumb != null && mThumb.isStateful()) {
int[] state = getDrawableState();
mThumb.setState(state);
}
invalidate();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (mEnabled) {
this.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
if (ignoreTouch(event.getX(), event.getY())) {
return false;
}
if (ignoreAngleTouch(event.getX(), event.getY())) {
return false;
}
onStartTrackingTouch();
oldAngle = getAngle();
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "onTouchEvent: ACTION_DOWN old angle" + getAngle());
}
startClickTime = Calendar.getInstance().getTimeInMillis();
updateOnTouch(event);
break;
case MotionEvent.ACTION_MOVE:
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "ACTION_MOVE TRIGGERED");
}
if (ignoreAngleTouch(event.getX(), event.getY())) {
return false;
}
updateOnTouchForMove(event);
break;
case MotionEvent.ACTION_UP:
onStopTrackingTouch();
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "onTouchEvent: ACTION_UP old angle" + getAngle());
}
long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
if (clickDuration > 50) {
updateOnTouchForMove(event);
} else {
updateOnTouch(event);
}
setPressed(false);
this.getParent().requestDisallowInterceptTouchEvent(false);
break;
case MotionEvent.ACTION_CANCEL:
onStopTrackingTouch();
setPressed(false);
this.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
return true;
}
return false;
}
private boolean ignoreAngleTouch(float x, float y) {
boolean ignore = false;
double touchAngle = getTouchDegrees(x, y);
if (touchAngle > START_ANGLE && touchAngle < LOWER_THRESHOLD_VALUE || touchAngle > START_ANGLE && touchAngle < THRESHOLD_VALUE) {
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "ignoreAngleTouch");
}
ignore = true;
}
return ignore;
}
private void updateOnTouchForMove(MotionEvent event) {
setPressed(true);
mTouchAngle = getTouchDegrees(event.getX(), event.getY());
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "Touch Angle " + mTouchAngle);
}
int angle = (int) mTouchAngle - (int) START_ANGLE;
updateAngleForMove(angle);
}
private void updateAngleForMove(int angle) {
mAngle = (angle > MAX) ? angle : (angle < 0) ? angle : angle;
if (angle == 0) {
mAngle = (int) START_ANGLE;
}
int cloneAngle = mAngle;
if (mTouchAngle > START_ANGLE && mTouchAngle < LOWER_THRESHOLD_VALUE) {
mAngle = LOWER_LIMIT;
}
if ((mAngle / 13) > 19) {
mAngle = COMPLETE_ANGLE;
}
if (mOnProtractorViewChangeListener != null) {
mOnProtractorViewChangeListener.onProgressChanged(this, cloneAngle, true);
}
boolean check = mTouchAngle > START_ANGLE && mTouchAngle < THRESHOLD_VALUE;
if (!check) {
updateThumbPosition();
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "updateAngle: Re Drawing");
}
invalidate();
} else {
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "Inside else condition");
}
mAngle = LOWER_LIMIT;
updateThumbPosition();
invalidate();
}
}
private void onStartTrackingTouch() {
if (mOnProtractorViewChangeListener != null) {
mOnProtractorViewChangeListener.onStartTrackingTouch(this);
}
}
private void onStopTrackingTouch() {
if (mOnProtractorViewChangeListener != null) {
mOnProtractorViewChangeListener.onStopTrackingTouch(this);
}
}
private boolean ignoreTouch(float xPos, float yPos) {
boolean ignore = false;
float x = xPos - mTranslateX;
float y = yPos - mTranslateY;
float touchRadius = (float) Math.sqrt(((x * x) + (y * y)));
if (touchRadius < mTouchIgnoreRadius + 30 || touchRadius > (mArcRadius + mTickLength + mTickOffset)) {
ignore = true;
}
return ignore;
}
private void updateOnTouch(MotionEvent event) {
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "updateOnTouch: Old Angle" + getAngle());
}
boolean ignoreTouch = ignoreTouch(event.getX(), event.getY());
if (ignoreTouch) {
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "in ignore touch");
}
return;
}
setPressed(true);
mTouchAngle = getTouchDegrees(event.getX(), event.getY());
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "Touch Angle " + mTouchAngle);
}
int angle = (int) mTouchAngle - (int) START_ANGLE;
onProgressRefresh(angle, true);
}
private double getTouchDegrees(float xPos, float yPos) {
float x = xPos - mTranslateX;
float y = yPos - mTranslateY;
x = -x;
// convert to arc Angle
double angle = Math.toDegrees(Math.atan2(y, x) + (Math.PI));
/*if (angle > 270)
angle = 0;
else if (angle > 180)
angle = 360;*/
return angle;
}
private void onProgressRefresh(int angle, boolean fromUser) {
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "Change Angle: " + angle);
}
updateAngle(angle, fromUser);
}
private void updateAngle(int angle, boolean fromUser) {
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "updateAngle: " + angle);
}
mAngle = (angle > MAX) ? angle : (angle < 0) ? angle : angle;
if (angle == 0) {
mAngle = (int) START_ANGLE;
}
if (mAngle > LOWER_LIMIT && mAngle < 0) {
mAngle = LOWER_LIMIT;
}
if (mTouchAngle > START_ANGLE && mTouchAngle < THRESHOLD_VALUE) {
mAngle = LOWER_LIMIT;
}
int cloneAngle = mAngle;
if (mAngle > 0) {
cloneAngle = mAngle - 360;
}
if ((mAngle / 13) > 19) {
mAngle = COMPLETE_ANGLE;
}
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "updateAngle: Angle being sent to onProgressChanged " + cloneAngle);
}
if (mOnProtractorViewChangeListener != null) {
mOnProtractorViewChangeListener.onProgressChanged(this, cloneAngle, true);
}
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "Angle being sent to the function = " + mAngle);
}
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "Value of Sweep Angle " + sweepAngle);
}
boolean check = mTouchAngle > START_ANGLE && mTouchAngle < THRESHOLD_VALUE;
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "Check Condition Value: " + check);
}
if (!check) {
updateThumbPosition();
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "updateAngle: Re Drawing");
}
startAnimatingArc();
} else {
mAngle = LOWER_LIMIT;
updateThumbPosition();
startAnimatingArc();
}
}
public void startAnimatingArc() {
final ProtractorView protractorView = findViewById(R.id.protractorview);
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "Inside start Animating Arc\nOld Angle: " + getOldAngle() + "\nNew Angle: " + getAngle());
}
int newAngle = getAngle();
if (newAngle > 0) {
newAngle -= 360;
}
ValueAnimator animator = ValueAnimator.ofInt(getOldAngle(), newAngle);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
int animatedValue = (int) animation.getAnimatedValue();
if (Util.IS_DEBUG_LOGGABLE) {
Log.d(TAG, "onAnimationUpdate: Animated Value " + animatedValue);
}
mAngle = animatedValue;
protractorView.invalidate();
}
});
animator.setDuration(450);
animator.start();
// invalidate();
}
private void updateThumbPosition() {
int thumbAngle = mAngle; //(int) (mStartAngle + mProgressSweep + mRotation + 90);
mThumbXPos = (int)(mArcRadius * Math.cos(Math.toRadians(thumbAngle)));
mThumbYPos = (int)(mArcRadius * Math.sin(Math.toRadians(thumbAngle)));
}
//*****************************************************
// Setters and Getters
//*****************************************************
public boolean getTouchInside() {
return mTouchInside;
}
public void setTouchInside(boolean isEnabled) {
int thumbHalfheight = mThumb.getIntrinsicHeight() / 2;
int thumbHalfWidth = mThumb.getIntrinsicWidth() / 2;
mTouchInside = isEnabled;
if (mTouchInside) {
mTouchIgnoreRadius = (float)(mArcRadius / 1.5) + 5;
} else {
mTouchIgnoreRadius = mArcRadius + 5 - Math.min(thumbHalfWidth, thumbHalfheight);
}
}
public void setOnProtractorViewChangeListener(OnProtractorViewChangeListener l) {
mOnProtractorViewChangeListener = l;
}
public OnProtractorViewChangeListener getOnProtractorViewChangeListener() {
return mOnProtractorViewChangeListener;
}
public int getAngle() {
return mAngle;
}
public void setAngle(int angle) {
this.mAngle = angle;
onProgressRefresh(mAngle, false);
}
public int getOldAngle() {
return oldAngle;
}
public boolean isEnabled() {
return mEnabled;
}
public void setEnabled(boolean enabled) {
this.mEnabled = enabled;
invalidate();
}
public int getProgressColor() {
return mArcProgressPaint.getColor();
}
public void setProgressColor(#ColorInt int color) {
mArcProgressPaint.setColor(color);
invalidate();
}
public int getArcColor() {
return mArcPaint.getColor();
}
public void setArcColor(#ColorInt int color) {
mArcPaint.setColor(color);
invalidate();
}
public int getArcProgressWidth() {
return mArcProgressWidth;
}
public void setArcProgressWidth(int arcProgressWidth) {
this.mArcProgressWidth = arcProgressWidth;
mArcProgressPaint.setStrokeWidth(arcProgressWidth);
invalidate();
}
public int getArcWidth() {
return mArcWidth;
}
public void setArcWidth(int arcWidth) {
this.mArcWidth = arcWidth;
mArcPaint.setStrokeWidth(arcWidth);
invalidate();
}
public boolean isRoundedEdges() {
return mRoundedEdges;
}
public void setRoundedEdges(boolean roundedEdges) {
this.mRoundedEdges = roundedEdges;
if (roundedEdges) {
mArcPaint.setStrokeCap(Paint.Cap.ROUND);
mArcProgressPaint.setStrokeCap(Paint.Cap.ROUND);
} else {
mArcPaint.setStrokeCap(Paint.Cap.SQUARE);
mArcPaint.setStrokeCap(Paint.Cap.SQUARE);
}
invalidate();
}
public Drawable getThumb() {
return mThumb;
}
public void setThumb(Drawable thumb) {
this.mThumb = thumb;
invalidate();
}
public int getAngleTextSize() {
return mAngleTextSize;
}
public void setAngleTextSize(int angleTextSize) {
this.mAngleTextSize = angleTextSize;
invalidate();
}
public int getTickOffset() {
return mTickOffset;
}
public void setTickOffset(int tickOffset) {
this.mTickOffset = tickOffset;
}
public int getTickLength() {
return mTickLength;
}
public void setTickLength(int tickLength) {
this.mTickLength = tickLength;
}
public TicksBetweenLabel getTicksBetweenLabel() {
return mTicksBetweenLabel;
}
public void setTicksBetweenLabel(TicksBetweenLabel ticksBetweenLabel) {
this.mTicksBetweenLabel = mTicksBetweenLabel;
invalidate();
}
public int getTickIntervals() {
return mTickIntervals;
}
public void setTickIntervals(int tickIntervals) {
this.mTickIntervals = tickIntervals;
invalidate();
}
}

How should I use and add other color on Activity

I've made a program that clicks on the color of the image and displays the color in text, but for now I can only use a few basic colors, basic colors like black, red or yellow, etc. If I want to join other colors such as pink or light blue, how can I do, if I may, you can also set a range of colors it? set, for example # FF0000 ~ # FF6347 is red (color code used here 16bit)
My problem in Color.XXX this part can not be customized
int touchedRGB = orgbmp.getPixel(x, y);
if(touchedRGB == Color.BLACK)
colorshow.setText("Is Black");
if(touchedRGB == Color.RED)
colorshow.setText("Is RED");
if(touchedRGB == Color.GREEN)
colorshow.setText("Is GREEN");
if(touchedRGB == Color.BLUE)
colorshow.setText("Is Blue");
if(touchedRGB == Color.GRAY)
colorshow.setText("Is Gary");
if(touchedRGB == Color.YELLOW)
colorshow.setText("Is Yellow");
Your class should implement ColorPickerDialog.OnColorChangedListener
public class MainActivity implements ColorPickerDialog.OnColorChangedListener
{
private Paint mPaint;
mPaint = new Paint();
// on button click
new ColorPickerDialog(this, this, mPaint.getColor()).show();
}
ColorPicker Dialog
public class ColorPickerDialog extends Dialog {
public interface OnColorChangedListener {
void colorChanged(int color);
}
private OnColorChangedListener mListener;
private int mInitialColor;
private static class ColorPickerView extends View {
private Paint mPaint;
private Paint mCenterPaint;
private final int[] mColors;
private OnColorChangedListener mListener;
ColorPickerView(Context c, OnColorChangedListener l, int color) {
super(c);
mListener = l;
mColors = new int[] {
0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00,
0xFFFFFF00, 0xFFFF0000
};
Shader s = new SweepGradient(0, 0, mColors, null);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setShader(s);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(32);
mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mCenterPaint.setColor(color);
mCenterPaint.setStrokeWidth(5);
}
private boolean mTrackingCenter;
private boolean mHighlightCenter;
#Override
protected void onDraw(Canvas canvas) {
float r = CENTER_X - mPaint.getStrokeWidth()*0.5f;
canvas.translate(CENTER_X, CENTER_X);
canvas.drawOval(new RectF(-r, -r, r, r), mPaint);
canvas.drawCircle(0, 0, CENTER_RADIUS, mCenterPaint);
if (mTrackingCenter) {
int c = mCenterPaint.getColor();
mCenterPaint.setStyle(Paint.Style.STROKE);
if (mHighlightCenter) {
mCenterPaint.setAlpha(0xFF);
} else {
mCenterPaint.setAlpha(0x80);
}
canvas.drawCircle(0, 0,
CENTER_RADIUS + mCenterPaint.getStrokeWidth(),
mCenterPaint);
mCenterPaint.setStyle(Paint.Style.FILL);
mCenterPaint.setColor(c);
}
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(CENTER_X*2, CENTER_Y*2);
}
private static final int CENTER_X = 100;
private static final int CENTER_Y = 100;
private static final int CENTER_RADIUS = 32;
private int floatToByte(float x) {
int n = java.lang.Math.round(x);
return n;
}
private int pinToByte(int n) {
if (n < 0) {
n = 0;
} else if (n > 255) {
n = 255;
}
return n;
}
private int ave(int s, int d, float p) {
return s + java.lang.Math.round(p * (d - s));
}
private int interpColor(int colors[], float unit) {
if (unit <= 0) {
return colors[0];
}
if (unit >= 1) {
return colors[colors.length - 1];
}
float p = unit * (colors.length - 1);
int i = (int)p;
p -= i;
// now p is just the fractional part [0...1) and i is the index
int c0 = colors[i];
int c1 = colors[i+1];
int a = ave(Color.alpha(c0), Color.alpha(c1), p);
int r = ave(Color.red(c0), Color.red(c1), p);
int g = ave(Color.green(c0), Color.green(c1), p);
int b = ave(Color.blue(c0), Color.blue(c1), p);
return Color.argb(a, r, g, b);
}
private int rotateColor(int color, float rad) {
float deg = rad * 180 / 3.1415927f;
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
ColorMatrix cm = new ColorMatrix();
ColorMatrix tmp = new ColorMatrix();
cm.setRGB2YUV();
tmp.setRotate(0, deg);
cm.postConcat(tmp);
tmp.setYUV2RGB();
cm.postConcat(tmp);
final float[] a = cm.getArray();
int ir = floatToByte(a[0] * r + a[1] * g + a[2] * b);
int ig = floatToByte(a[5] * r + a[6] * g + a[7] * b);
int ib = floatToByte(a[10] * r + a[11] * g + a[12] * b);
return Color.argb(Color.alpha(color), pinToByte(ir),
pinToByte(ig), pinToByte(ib));
}
private static final float PI = 3.1415926f;
#Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX() - CENTER_X;
float y = event.getY() - CENTER_Y;
boolean inCenter = java.lang.Math.sqrt(x*x + y*y) <= CENTER_RADIUS;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mTrackingCenter = inCenter;
if (inCenter) {
mHighlightCenter = true;
invalidate();
break;
}
case MotionEvent.ACTION_MOVE:
if (mTrackingCenter) {
if (mHighlightCenter != inCenter) {
mHighlightCenter = inCenter;
invalidate();
}
} else {
float angle = (float)java.lang.Math.atan2(y, x);
// need to turn angle [-PI ... PI] into unit [0....1]
float unit = angle/(2*PI);
if (unit < 0) {
unit += 1;
}
mCenterPaint.setColor(interpColor(mColors, unit));
invalidate();
}
break;
case MotionEvent.ACTION_UP:
if (mTrackingCenter) {
if (inCenter) {
mListener.colorChanged(mCenterPaint.getColor());
}
mTrackingCenter = false; // so we draw w/o halo
invalidate();
}
break;
}
return true;
}
}
public ColorPickerDialog(Context context,
OnColorChangedListener listener,
int initialColor) {
super(context);
mListener = listener;
mInitialColor = initialColor;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OnColorChangedListener l = new OnColorChangedListener() {
public void colorChanged(int color) {
mListener.colorChanged(color);
dismiss();
}
};
setContentView(new ColorPickerView(getContext(), l, mInitialColor));
setTitle("Pick a Color");
}
You have to choose the color and click the center circle to pick the color. Set the color to your paint object and use the same to draw.
Creating a resource file matching color name and color codes will solve the problem instead of using Color module like here
getResources().getStringArray and getResources().obtainTypedArray can be used to loop over and match the names & color codes.

Path drawing. in android studio

I am trying to implement path drawing for a herding game.
Currently when I run the game on PC I use the mouse to draw a path for which the herder will move through, but I can not get it to draw a line.
package com.mygdx.herdergame.Sprites;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Array;
import com.mygdx.herdergame.HerderGame;
import com.mygdx.herdergame.Screens.PlayScreen;
import java.util.ArrayList;
public class Herder extends Sprite implements InputProcessor {
HerderGame game;
public World world;
public Body b2body;
private PlayScreen screen;
//States
private enum State {
RUNNINGRIGHT, RUNNINGLEFT, RUNNINGUP, RUNNINGDOWN, STANDINGRIGHT, STANDINGLEFT, STANDINGUP, STANDINGDOWN
}
private State currentState;
private State previousState;
private float stateTimer;
//Textures
private Animation runningRight;
private Animation runningLeft;
private Animation runningUp;
private Animation runningDown;
private TextureRegion standRight;
private TextureRegion standLeft;
private TextureRegion standUp;
private TextureRegion standDown;
private float destinationX;
private float destinationY;
//Velocity
private static final float MAX_SPEED = 500;
private float speedLimit = 1000;
private Vector2 velocity = new Vector2(0, 0);
private static float ACCURACY = 50;
//Touch Screen
private Vector3 touchpoint = new Vector3();
private Vector2 targetPosition;
private ArrayList<Vector2> herderPath = new ArrayList<Vector2>();
boolean dragging;
boolean followingPath = false;
// death variables
private boolean isInTimedDeathZone;
private float deathTimer;
//this is the constructor.
public Herder(World world, PlayScreen screen) {
super(screen.getAtlas().findRegion("herderRight"));
this.screen = screen;
game = screen.getGame();
this.world = world;
defBody();
setTextures();
currentState = State.STANDINGRIGHT;
previousState = State.STANDINGRIGHT;
stateTimer = 0;
isInTimedDeathZone = false;
deathTimer = 0;
}
public void defBody() {
BodyDef bdef = new BodyDef();
bdef.position.set(500, 500);
bdef.type = BodyDef.BodyType.DynamicBody;
b2body = world.createBody(bdef);
FixtureDef fdef = new FixtureDef();
CircleShape shape = new CircleShape();
shape.setRadius(10);
fdef.shape = shape;
b2body.createFixture(fdef).setUserData(this);
}
public void setTextures() {
int herderUpStartX = game.rm.getChapter(game.getCurrentChapter()).getHerderUp().getStartX();
int herderUpStartY = game.rm.getChapter(game.getCurrentChapter()).getHerderUp().getStartY();
int herderDownStartX = game.rm.getChapter(game.getCurrentChapter()).getHerderDown().getStartX();
int herderDownStartY = game.rm.getChapter(game.getCurrentChapter()).getHerderDown().getStartY();
int herderLeftStartX = game.rm.getChapter(game.getCurrentChapter()).getHerderLeft().getStartX();
int herderLeftStartY = game.rm.getChapter(game.getCurrentChapter()).getHerderLeft().getStartY();
int herderRightStartX = game.rm.getChapter(game.getCurrentChapter()).getHerderRight().getStartX();
int herderRightStartY = game.rm.getChapter(game.getCurrentChapter()).getHerderRight().getStartY();
int numberOfFrame = game.rm.getChapter(game.getCurrentChapter()).getHerderRight().getNumberOfFrame();
int width = game.rm.getChapter(game.getCurrentChapter()).getHerderRight().getWidth();
int height = game.rm.getChapter(game.getCurrentChapter()).getHerderRight().getHeight();
//this enables the sprite to be drawn.
Array<TextureRegion> frames = new Array<TextureRegion>();
for (int i = 0; i < numberOfFrame; i++) {
frames.add(new TextureRegion(getTexture(), herderRightStartX + width * i, herderRightStartY, width, height));
}
runningRight = new Animation(0.1f, frames);
frames.clear();
for (int i = 0; i < numberOfFrame; i++) {
frames.add(new TextureRegion(getTexture(), herderLeftStartX + width * i, herderLeftStartY, width, height));
}
runningLeft = new Animation(0.1f, frames);
frames.clear();
for (int i = 0; i < numberOfFrame; i++) {
frames.add(new TextureRegion(getTexture(), herderDownStartX + width * i, herderDownStartY, width, height));
}
runningDown = new Animation(0.1f, frames);
frames.clear();
for (int i = 0; i < numberOfFrame; i++) {
frames.add(new TextureRegion(getTexture(), herderUpStartX + width * i, herderUpStartY, width, height));
}
runningUp = new Animation(0.1f, frames);
standRight = new TextureRegion(getTexture(), herderRightStartX, herderRightStartY, width, height);
setBounds(0, 0, 32, 32);
setRegion(standRight);
standLeft = new TextureRegion(getTexture(), herderLeftStartX, herderLeftStartY, width, height);
setBounds(0, 0, 32, 32);
setRegion(standLeft);
standUp = new TextureRegion(getTexture(), herderUpStartX, herderUpStartY, width, height);
setBounds(0, 0, 32, 32);
setRegion(standUp);
standDown = new TextureRegion(getTexture(), herderDownStartX, herderDownStartY, width, height);
setBounds(0, 0, 32, 32);
setRegion(standDown);
setSize(32, 32);
}
public TextureRegion getFrame(float dt) {
currentState = getState();
TextureRegion region;
switch (currentState) {
case RUNNINGRIGHT:
region = runningRight.getKeyFrame(stateTimer, true);
break;
case RUNNINGLEFT:
region = runningLeft.getKeyFrame(stateTimer, true);
break;
case RUNNINGUP:
region = runningUp.getKeyFrame(stateTimer, true);
break;
case RUNNINGDOWN:
region = runningDown.getKeyFrame(stateTimer, true);
break;
case STANDINGLEFT:
region = standLeft;
break;
case STANDINGUP:
region = standUp;
break;
case STANDINGDOWN:
region = standDown;
break;
case STANDINGRIGHT:
default:
region = standRight;
break;
}
stateTimer = currentState == previousState ? stateTimer + dt : 0;
previousState = currentState;
return region;
}
public State getState() {
Vector2 direction = b2body.getLinearVelocity();
if (direction.isZero()) {
switch (previousState) {
case RUNNINGUP:
return State.STANDINGUP;
case RUNNINGLEFT:
return State.STANDINGLEFT;
case RUNNINGDOWN:
return State.STANDINGDOWN;
case RUNNINGRIGHT:
return State.STANDINGRIGHT;
default:
return previousState;
}
} else if (direction.x >= 0 && direction.y >= 0) {
if (direction.x > direction.y) {
return State.RUNNINGRIGHT;
} else return State.RUNNINGUP;
} else if (direction.x >= 0 && direction.y <= 0) {
if (direction.x > -direction.y) {
return State.RUNNINGRIGHT;
} else return State.RUNNINGDOWN;
} else if (direction.x <= 0 && direction.y >= 0) {
if (-direction.x > direction.y) {
return State.RUNNINGLEFT;
} else return State.RUNNINGUP;
} else if (direction.x <= 0 && direction.y <= 0) {
if (-direction.x > -direction.y) {
return State.RUNNINGLEFT;
} else return State.RUNNINGDOWN;
} else return currentState = previousState;
}
public void update(float dt) {
if (isXStapable() || isYStapable()) {
stop();
}
if (followingPath) {
if (!herderPath.iterator().hasNext()) {
followingPath = false;
herderPath.clear();
stop();
} else if (targetPosition.dst(b2body.getPosition()) < 1.5) {
targetPosition = herderPath.get(0);
herderPath.remove(0);
velocity = calculateVelocity(b2body.getPosition(), targetPosition);
} else {
velocity = calculateVelocity(b2body.getPosition(), targetPosition);
}
}
b2body.setLinearVelocity(velocity);
setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2);
setRegion(getFrame(dt));
if (isInTimedDeathZone) {
deathTimer += dt;
} else {
deathTimer = 0;
}
if (deathTimer > 2f) {
screen.gameOver();
}
}
public void setInTimedDeathZone(boolean b) {
this.isInTimedDeathZone = b;
}
#Override
public boolean keyDown(int keycode) {
switch (keycode) {
case Input.Keys.UP:
velocity.y = speedLimit;
break;
case Input.Keys.DOWN:
velocity.y = -speedLimit;
break;
case Input.Keys.LEFT:
velocity.x = -speedLimit;
break;
case Input.Keys.RIGHT:
velocity.x = speedLimit;
break;
default:
return true;
}
followingPath = false;
return true;
}
#Override
public boolean keyUp(int keycode) {
switch (keycode) {
case Input.Keys.UP:
case Input.Keys.DOWN:
velocity.y = 0;
break;
case Input.Keys.LEFT:
case Input.Keys.RIGHT:
velocity.x = 0;
break;
}
return true;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
float diffX;
float diffY;
// ignore if its not left mouse button or first touch pointer
if (button != Input.Buttons.LEFT || pointer > 0) return false;
screen.getCamera().unproject(touchpoint.set(screenX, screenY, 0));
//ignore if the first point is not close to the herder
diffX = touchpoint.x - b2body.getPosition().x;
diffY = touchpoint.y - b2body.getPosition().y;
if (diffX > 25 || diffX < -25 || diffY > 25 || diffY < -25) return false;
dragging = true;
herderPath.clear();
targetPosition = b2body.getPosition();
herderPath.add(targetPosition);
return true;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
Vector2 vec2 = new Vector2();
if (!dragging) return false;
screen.getCamera().unproject(touchpoint.set(screenX, screenY, 0));
vec2.set(touchpoint.x, touchpoint.y);
herderPath.add(vec2);
followingPath = true;
return true;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
if (button != Input.Buttons.LEFT || pointer > 0) return false;
screen.getCamera().unproject(touchpoint.set(screenX, screenY, 0));
dragging = false;
return true;
}
private Vector2 calculateVelocity(Vector2 currentPosition, Vector2 targetPosition) {
Vector2 tempTP = new Vector2().set(targetPosition);
return tempTP.sub(currentPosition).nor().scl(speedLimit);
}
public boolean isXValid(int x) {
return b2body.getPosition().x <= x + ACCURACY && b2body.getPosition().x >= x - ACCURACY;
}
public boolean isYValid(int y) {
return b2body.getPosition().y <= y + ACCURACY && b2body.getPosition().y >= y - ACCURACY;
}
public boolean isXStapable() {
return isXValid((int) destinationX);
}
public boolean isYStapable() {
return isYValid((int) destinationY);
}
public void stop() {
velocity.set(0, 0);
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolled(int amount) {
return false;
}
public void setSpeedLimit(float multiplier) {
if (speedLimit > MAX_SPEED) {
this.speedLimit = MAX_SPEED;
} else {
this.speedLimit = multiplier;
}
}
}

Long-Press Touchevents

I have three buttons that are tapped based on touchevents in a certain boundary and I want to change it so that the tapevents can be a long-press instead of a tap. I'm not sure how to do this. Here is my relevant code:
private void updateRunning(List<TouchEvent> touchEvents, float deltaTime) {
// 1. All touch input is handled here:
int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if (event.type == TouchEvent.TOUCH_DOWN) {
if (inBounds(event, 340,512,140,140)) {
B1Pressed = true;
Log.d("GameScreen", "Button 1 Pressed");
// Button 1
}
if (inBounds(event, 340,320,140,140)) {
B2Pressed = true;
Log.d("GameScreen", "Button 2 Pressed");
// Button 2
}
if (inBounds(event, 340,120,140,140)) {
B3Pressed = true;
Log.d("GameScreen", "Button 3 Pressed");
// Button 3
}
}
private boolean inBounds(TouchEvent event, int x, int y, int width,
int height) {
if (event.x > x && event.x < x + width - 1 && event.y > y
&& event.y < y + height - 1)
return true;
else
return false;
}
Input.java:
public interface Input {
public static class TouchEvent {
public static final int TOUCH_DOWN = 0;
public static final int TOUCH_UP = 1;
public static final int TOUCH_DRAGGED = 2;
public static final int TOUCH_HOLD = 3;
public int type;
public int x, y;
public int pointer;
}
public boolean isTouchDown(int pointer);
public int getTouchX(int pointer);
public int getTouchY(int pointer);
public List<TouchEvent> getTouchEvents();
}
TouchHandler.java:
public interface TouchHandler extends OnTouchListener {
public boolean isTouchDown(int pointer);
public int getTouchX(int pointer);
public int getTouchY(int pointer);
public List<TouchEvent> getTouchEvents();
}
SingleTouchHandler:
public class SingleTouchHandler implements TouchHandler {
boolean isTouched;
int touchX;
int touchY;
Pool<TouchEvent> touchEventPool;
List<TouchEvent> touchEvents = new ArrayList<TouchEvent>();
List<TouchEvent> touchEventsBuffer = new ArrayList<TouchEvent>();
float scaleX;
float scaleY;
public SingleTouchHandler(View view, float scaleX, float scaleY) {
PoolObjectFactory<TouchEvent> factory = new PoolObjectFactory<TouchEvent>() {
#Override
public TouchEvent createObject() {
return new TouchEvent();
}
};
touchEventPool = new Pool<TouchEvent>(factory, 100);
view.setOnTouchListener(this);
this.scaleX = scaleX;
this.scaleY = scaleY;
}
#Override
public boolean onTouch(View v, MotionEvent event) {
synchronized(this) {
TouchEvent touchEvent = touchEventPool.newObject();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchEvent.type = TouchEvent.TOUCH_DOWN;
isTouched = true;
break;
case MotionEvent.ACTION_MOVE:
touchEvent.type = TouchEvent.TOUCH_DRAGGED;
isTouched = true;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
touchEvent.type = TouchEvent.TOUCH_UP;
isTouched = false;
break;
}
touchEvent.x = touchX = (int)(event.getX() * scaleX);
touchEvent.y = touchY = (int)(event.getY() * scaleY);
touchEventsBuffer.add(touchEvent);
return true;
}
}
#Override
public boolean isTouchDown(int pointer) {
synchronized(this) {
if(pointer == 0)
return isTouched;
else
return false;
}
}
#Override
public int getTouchX(int pointer) {
synchronized(this) {
return touchX;
}
}
#Override
public int getTouchY(int pointer) {
synchronized(this) {
return touchY;
}
}
#Override
public List<TouchEvent> getTouchEvents() {
synchronized(this) {
int len = touchEvents.size();
for( int i = 0; i < len; i++ )
touchEventPool.free(touchEvents.get(i));
touchEvents.clear();
touchEvents.addAll(touchEventsBuffer);
touchEventsBuffer.clear();
return touchEvents;
}
}
}
How can I go about implementing long press?
Let your Class Implement View.OnLongClickListener as
SingleTouchHandler Implements View.OnLongClickListener
#Override
public boolean onLongClick(View v) {
return false;
}
and you get method to perform long press opertaions hope this helps

Categories

Resources