I'm trying to make a game in Android Studio using Kotlin, currently I have a grid drawn using two for loops (1 for cols and 1 for rows) that draws dots. I want the dots to fill the View, without the blank space around the grid. Any ideas?
val columns = 5
val rows = 5
var xPos: Float = width / (columns + 1)
var yPos: Float = height / (rows + 1)
for (col in 1..columns) {
for (row in 1..rows) {
canvas.drawPoint(col*xPos, row*yPos, paint)
}
}
What I get:
d
What I want:
You need a little space around the edges. So you can have a variable for padding, which should be half the diameter of your dots plus however many pixels of white you want around them. You would probably calculate the size using a constant DIP unit and the screen density, same as you probably did for your paint to get the dot diameter.
Then you can use the padding in your calculation. For example, if you have five columns, to get the space between dots, you want to divide the width by four, after subtracting the padding from both sides.
val padding = /* ... */
val columns = 5
val rows = 5
val hSpacing = (width - (2 * padding)) / (columns - 1)
val vSpacing = (height - (2 * padding)) / (rows - 1)
for (i in 0..columns)
for (j in 0..rows)
canvas.drawPoint(padding + i * hSpacing, padding + j * vSpacing, paint)
Related
I have a RecyclerView with a StaggeredGridLayoutManager.
The problem is that before a full span element there is a gap that I need to fill, increasing/decreasing the height of the last two elements.
A picture of the problem:
Just 2 columns are required.
The elements of the grid can have different heights.
I'm trying to calculate the height of the two columns using each element height, and at the end see which column is longer, and add height to the shortest, but I don't know where is a safe place to set the new height. When I know for sure the height of the view is the one is going to be draw.
I'm stuck for days with this problem, any suggestions are welcome!
onBindViewHolder
double positionHeight = getPositionRatio(position);
holder.img_event).setHeightRatio(positionHeight);
private double getPositionRatio(final int position) {
double ratio = sPositionHeightRatios.get(position, 0.0);
// if not yet done generate and stash the columns height
// in our real world scenario this will be determined by
// some match based on the known height and width of the image
// and maybe a helpful way to get the column height!
if (ratio == 0) {
ratio = getRandomHeightRatio();
sPositionHeightRatios.append(position, ratio);
Log.d("'", "getPositionRatio:" + position + " ratio:" + ratio);
}
return ratio;
}
private final Random mRandom = new Random();
private double getRandomHeightRatio() {
return (mRandom.nextDouble() / 2.0) + 1.0; // height will be 1.0 - 1.5 the width
}
What is happening from line #129 to line #133 in this class of the Color blob detection sample app?
SOME CONTEXT:
The camera view in the app looks like this: (Notice that in the camera view, there is a black border around the camera frame)
(If you can't see the image, check it here.)
From Line 114 to 128, the following is happening.
int cols = mRgba.cols(); cols() gives the number of columns in a matrix. The matrix here is the Mat representing a frame in the live stream of frames being displayed (and not the entire camera view), i.e. it represents the part of the camera view where live stream is being displayed, EXCLUDING the black border of the camera view.
int rows = mRgba.rows(); rows() gives the number of rows in the camera frame, EXCLUDING the black border of the camera view.
int xOffset = (mOpenCvCameraView.getWidth() - cols) / 2; int yOffset = (mOpenCvCameraView.getHeight() - rows) / 2; mOpenCvCameraView.getWidth() gives the number of rows in the entire camera view, i.e. the camera frame PLUS the black border of the camera view around the frame. (mOpenCvCameraView.getWidth() - cols) gives the sum of the width of left and right black border of the camera view. (mOpenCvCameraView.getWidth() - cols)/2 or xOffset gives the width of the black border on one side, i.e. either right or left side, black border of the camera view. Likewise for yOffset
int x = (int)event.getX() - xOffset; int y = (int)event.getY() - yOffset; getX() returns the X coordinate of this event for the first pointer index. So getX() gives the distance of the touched region from the left extreme side of the camera view, which includes the black border on the left. So event.getX()-xOffset or int x is the distance of the touched region from the left extreme side of the camera "frame" (which does NOT include the black border of the camera view). Likewise for int y.
Then are the lines which I have no clue about.
I assume you're asking about these lines:
touchedRect.x = (x>4) ? x-4 : 0;
touchedRect.y = (y>4) ? y-4 : 0;
touchedRect.width = (x+4 < cols) ? x + 4 - touchedRect.x : cols - touchedRect.x;
touchedRect.height = (y+4 < rows) ? y + 4 - touchedRect.y : rows - touchedRect.y;
From what I can tell, it's basically just making sure that Rectangle height and width don't wander out of frame.
Breakdown of why -
The ? operator (called the ternary operator) in Java is basically a shorthand of if/else - the line touchedRect.x = (x>4) ? x-4 : 0 means
if (x>4) {
touchedRect.x = x-4;
} else {
touchedRect.x = 0;
}
So, for line 129, if x > 4, set touchedRect.x to x-4, else 0.
line 130, if y > 4, set touchedRect.y to y-4, else 0.
line 131, touchedRect.width becomes x+4 - touchedRect.x if x+4 < cols, else touchedRect.width becomes cols - touchedRect.x
line 132, touchedRect.height becomes y+4-touchedRect.y if y+4 < rows, else it becomes rows - touchedRect.y
I want to change textcolor of a textview which is in the header of the listview from a particular color (This could be any color) to WHITE onscroll of the listview.
I read about HSV and I need to decrease values of H and S towards 0 and increase the value of V towards 1 to get a color closer to WHITE
Has someone done a similar thing where in interpolation would happen with the value of scrollY?
Hope it will help:
private final float[] mHsvTemp = new float[3];
int hsvInterplate(float[] hsvWhen0, float[] hsvWhen1, float scale) {
if (scale <= 0) return Color.HSVToColor(hsvWhen0);
if (scale >= 1) return Color.HSVToColor(hsvWhen1);
float hDist = hsvWhen1[0] - hsvWhen0[0];
if (hDist > 180) hDist -= 360;
else if (hDist <= -180) hDist += 360;
mHsvTemp[0] = hsvWhen0[0] + hDist * scale;
mHsvTemp[1] = hsvWhen0[1] + (hsvWhen0[1] - hsvWhen0[1]) * scale;
mHsvTemp[2] = hsvWhen0[2] + (hsvWhen0[2] - hsvWhen0[2]) * scale;
return Color.HSVToColor(mHsvTemp);
}
This function takes two hsv arguments and interpolates between them, depend on scale value. scale is in range from 0 to 1.
zero or less - color fully as first argument
one or greater - color fully as second argument
0.5f - average middle between them.
So, you can pass something like (float)getScrollY() / computeVerticalScrollRange () as third argument
I have about 10 buttons, and these 10 should be displayed based on the screen width. If the screen width is lesser, then a "more" button should be displayed and on click of more , the remaining buttons should be displayed as a pop up on more button.
I have tried this
Add Buttons dynamically depending on screen width
But width consideration is troubling me.. Could some one help me out in this regard
Following code may help you
Display display =((WindowManager)activityContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
DisplayWidth = display.getWidth();
DisplayHeight = display.getHeight();
if(DisplayHeight > DisplayWidth)
{
ButtonWidth = (6 * DisplayWidth) / 7 ;
ButtonHeight = (4 * DisplayHeight) / 5 ;
}
else
{
ButtonWidth = (6 * DisplayWidth) / 9 ;
ButtonHeight = (4 * DisplayHeight) / 5 ;
}
Divide and multiply display width and height as per your requirement and set as button width and height.
I have a button class based on a LinearLayout that contains two TextViews, and I want the button always to be square (which I'm doing using onMeasure()), and the upper of the TextViews to resize automatically to fit the button. I want the lower TextView to have fixed-size text, so it looks roughly like
|---------|
| |
| 1 8 0 |
| |
| foo |
|---------|
with "180" being as large as possible in the available space.
I'm calling a function in onLayout() called adjustTextSize():
private void adjustTextSize() {
float height = mHeight - mTitleView.getMeasuredHeight() - 4 * mPadding - getPaddingTop() - getPaddingBottom(); //title text view
int numberheight = (int) (height * 0.75);
int extravertpadding = 0;//(int) (height * 0.125);
mTitleView.setPadding(mPadding, 0, mPadding, mPadding);
mNumberView.setTextSize(TypedValue.COMPLEX_UNIT_PX, numberheight);
mNumberView.setPadding(mPadding, mPadding + extravertpadding, mPadding, mPadding + extravertpadding);
if(mNumberView.getMeasuredWidth() > mWidth) {
float reduction = mWidth / mNumberView.getMeasuredWidth();
extravertpadding = (int) ((numberheight - height*reduction) / 2);
numberheight = (int) (height*reduction);
mNumberView.setTextSize(TypedValue.COMPLEX_UNIT_PX, numberheight);
mNumberView.setPadding(mPadding, mPadding + extravertpadding, mPadding, mPadding + extravertpadding);
}
mNumberView.setTextSize(TypedValue.COMPLEX_UNIT_PX, numberheight);
mNumberView.setPadding(mPadding, mPadding + extravertpadding, mPadding, mPadding + extravertpadding);
}
mWidth and mHeight are set in onLayout().
However, I have two initial problems: firstly, setting the text size to a certain pixel value makes the View somewhat bigger than the pixel value. I want to work out how large to set the text height to fit the available space; three quarters seems to work OK but I would rather have an exact value.
The second problem is that the value of getMeasuredWidth() seems not to be changing after I've changed the text size. Why not?
Answer to part 2:
It's necessary to call
mNumberView.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
);
to tell the system to measure the view as though there were no constraints on its size. After this has been called you can find out how large the view would be if space were no object and scale it down accordingly.