Its possible to make float random from range 1.3000 to 1.4000? That give me numbers like 13405, 13855 etc.
I know double is more flexible with range floating point, but i cant use it.
So i create something like:
float highmax = 0.6500f;
float highlow = 0.7000f;
float generatedFloatHigh = highmax + new Random().nextFloat() * (highlow - highmax);
But this not work what i want.
Any suggestion? Or maybe i should look for other libraly?
You can try to get a random number(for example x) between 0 and 1000, then you can get a number from range 1.3000 to 1.4000 like this :
int x = new Random(1000).nextInt();
float result = 1.3000f + x*0.0001f;
Random generator = new Random(System.currentTimeMillis());
int n = generator.nextInt(1000);
float generatedFloatHigh = 1.3000 + n/1000;
Related
I'm using Googles Mobile Vision API to recognize text (numbers) in a static Bitmap. Now I would like to zoom in to the place where the number was found.
So this is how I scan the Bitmap and obtain my x and y coordinates
Point[] p = textBlock.getCornerPoints();
public void Test(Bitmap bitmap) {
Context context = getApplicationContext();
TextRecognizer ocrFrame = new TextRecognizer.Builder(context).build();
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
ByteBuffer byteBuffer = frame.getGrayscaleImageData();
if (ocrFrame.isOperational()) {
Log.e(TAG, "Textrecognizer is operational");
}
SparseArray<TextBlock> textBlocks = ocrFrame.detect(frame);
for (int i = 0; i < textBlocks.size(); i++) {
TextBlock textBlock = textBlocks.get(textBlocks.keyAt(i));
String value = textBlock.getValue();
Point[] p = textBlock.getCornerPoints();
Log.e(TAG, "something is happening");
}
}
Furthermore, I´m using the TouchImageView to display the bitmap. Now I'm calling the setZoom method with my obtained coordinates like this:
touchImageView.setZoom(1F, 210F, 748F, ImageView.ScaleType.CENTER);
But it zooms to the wrong place and I don't really know why. Can anybody give me some tips?
(https://github.com/MikeOrtiz/TouchImageView/blob/master/src/com/ortiz/touch/TouchImageView.java)
EDIT: Ok, I figured it out that scale type does something I don't get. The problem here is setZoom, I think. I have to convert the coordinates of the bitmap to the coordinates of the Touchimageview.
EDIT2: Solution: Mistake was to pass the x and y coordinate directly but setZoom take values between 0 and 1
int BitmapHeight = photo.getHeight();
int BitmapWidth = photo.getWidth();
int FoundX = p[0].x;
int FoundY = p[0].y;
float DividerX = BitmapWidth / (float)FoundX;
float DividerY = BitmapHeight / (float)FoundY;
float ZoomX = 1 / (float)DividerX;
float ZoomY = 1 / (float)DividerY;
touchImageView.setZoom(touchImageView.getMaxZoom(), ZoomX, ZoomY, ImageView.ScaleType.CENTER);
You can use this google library https://developers.google.com/vision/android/text-overview .
You can find example here https://codelabs.developers.google.com/codelabs/mobile-vision-ocr
by adding below in android gradle file
compile 'com.google.android.gms:play-services-vision:15.0.0'
I am creating a CandleStick chart
from API: https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD&limit=10
Using Library: https://github.com/PhilJay/MPAndroidChart
When I creating the chart with the count of values as X-axis, There is no problem (ploat_point.add(new CandleEntry(i,high,low,open,close); in for loop ).
But when I use ploat_point.add(new CandleEntry(time,high,low,open,close); in for loop, And parse X-axis values using my custom function.
i.e :
xAxis = candle_chart.getXAxis();
xAxis.setValueFormatter(new IAxisValueFormatter() {
#Override
public String getFormattedValue(float value, AxisBase axis) {
Log.e(TAG, "getFormattedValue: " + (int) value);
return Utilities.timeStampToMonth((int) value + "");
}
});
This time it is showing the graph only when the paint style is set to Paint.Style.FILL_AND_STROKE, But It does not increasing the bar size with candleDataSet.setBarSize() . It depending only the candleDataSet.setShadowWidth().
my code :
private void drawCandleStickChart(JSONArray dataArr) throws JSONException {
mChart.setVisibility(View.GONE);
ArrayList<CandleEntry> plot_point = new ArrayList<>();
for (int i = 0; i < dataArr.length(); i++) {
JSONObject point = dataArr.getJSONObject(i);
int time = Math.round(Float.parseFloat(point.getString("time")));
float open = Float.parseFloat(String.valueOf(point.getString("open")));
float close = Float.parseFloat(String.valueOf(point.getString("close")));
float high = Float.parseFloat(String.valueOf(point.getString("high")));
float low = Float.parseFloat(String.valueOf(point.getString("low")));
plot_point.add(new CandleEntry(time, high, low, open, close));
}
CandleDataSet cds = new CandleDataSet(plot_point, "Entries");
cds.setShadowColor(Color.WHITE);
cds.setDecreasingColor(Color.RED);
cds.setDecreasingPaintStyle(Paint.Style.FILL_AND_STROKE);
cds.setIncreasingColor(Color.GREEN);
cds.setIncreasingPaintStyle(Paint.Style.FILL_AND_STROKE);
cds.setNeutralColor(Color.BLUE);
cds.setShowCandleBar(true);
cds.setBarSpace(0.5f);
cds.setShadowWidth(0.1f);
cds.setHighlightEnabled(false);
cds.setDrawValues(false);
candle_chart.setMaxVisibleValueCount(20);
CandleData cd = new CandleData(cds);
candle_chart.setData(cd);
candle_chart.invalidate();
candle_chart.setVisibility(View.VISIBLE);
}
I need X-axis as time and bar size is more than shadow please help me.
I acheived it by the changing constant value in library class 'CandleStickChartRenderer'. This has a method 'drawDataSet' with the code block
mBodyBuffers[0] = xPos - 0.5f + barSpace;
mBodyBuffers[1] = close * phaseY;
mBodyBuffers[2] = (xPos + 0.5f - barSpace);
mBodyBuffers[3] = open * phaseY;
This 0.5 constant you have to change to the some bigger value like 1000, As candle stick library has some issue with bigger x value.
How can I get entries on screen(not all) in MPAndroidChart?
What I need to use? ViewPortHandler or what? Do I need to left element on screen, and get low of him.(this is a candle chart)
I have silly issues play with width of candletick, cuz i have method for get min/max X cord on screen(left right candle).
But its only cord, not index of entry.
i found solution, but maybe its bad.
float lowestVisibleX = mainChart.getLowestVisibleX();
float highestVisibleX = mainChart.getHighestVisibleX();
int left = (int) Math.ceil(lowestVisibleX);
int right = (int) Math.ceil(highestVisibleX);
List<Integer> indexCheck = new ArrayList<>();
int loopCount = right - left;
while (loopCount != 0) {
indexCheck.add(new Integer(left));
left++;
loopCount--;
}
List<CandleEntry> entriesOnScreen = new ArrayList<>();
for (Integer in : indexCheck) {
entriesOnScreen.add(candleDataSet.getValues().get(in));
}
Looking at the Android docs, the ColorMatrix setSaturation() method says:
Set the matrix to affect the saturation of colors. A value of 0 maps the color to gray-scale. 1 is identity.
I'm trying to boost the saturation (in terms of HSL/HSV) to get more intense colors, so I passed in 1.6 and it seems to work. I've run into device-specific issues with Android layout shadowRadius values less than 1, so is there a danger of those types of issues with this parameter and exceeding the specified range?
The range for the saturation is indeed between 0 and 1. But lets take a look at the code for the setSaturation() method. (Android is open source):
public void setSaturation(float sat) {
reset();
float[] m = mArray;
final float invSat = 1 - sat; //<---------invSat will be negative if sat bigger than 1
final float R = 0.213f * invSat;
final float G = 0.715f * invSat;
final float B = 0.072f * invSat;
m[0] = R + sat; m[1] = G; m[2] = B;
m[5] = R; m[6] = G + sat; m[7] = B;
m[10] = R; m[11] = G; m[12] = B + sat;
}
The part of the code that we care about is pointed out. As you can see,The method will take your input and subtract it from 1. If you input a value greater than 1, this will result in a negative value for the invSat, and as you can see it may cause bigger problems as well.
How can i get the battery temperature with decimal? Actually i can calculate it with
int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
But in this way the result will be for example 36 °C.. I want something that show me 36.4 °C How can i do?
Google says here :
Extra for ACTION_BATTERY_CHANGED: integer containing the current battery temperature.
The returned value is an int representing, for example, 27.5 Degrees Celcius as "275" , so it is accurate to a tenth of a centigrade. Simply cast this to a float and divide by 10.
Using your example:
int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
float tempTwo = ((float) temp) / 10;
OR
float temp = ((float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0) / 10;
You don't need to worry about the 10 as an int since only one operand needs to be a float for the result to be one too.
public static String batteryTemperature(Context context)
{
Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
float temp = ((float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0)) / 10;
return String.valueOf(temp) + "*C";
}
That's the only way I know you can get the battery temperature, and is always an int.
According to documentation:
public static final String EXTRA_TEMPERATURE
Extra for ACTION_BATTERY_CHANGED: integer containing the current battery
temperature.
But you can divide by 10.0f to get one decimal.
float ftemp = temp/10.0f;