Issue
I need to display wind direction readings as arrows, original bmp is an arrow that is rotated to be 0° (pointing upwards), i have a for loop in which i can retrieve degrees from database and i can set drawable to my mpandroidchart but i need to rotate that drawable.
val jsonTemperatureData = JSONArray(result?.get(0))
for (i in 0 until jsonTemperatureData.length()) {
val item = jsonTemperatureData.getJSONObject(i)
val reading_temperature = item.getString("reading_windspeed")
val degrees= item.getString("degrees")
yVals.add(Entry(hour.toFloat(), reading_temperature.toFloat(), ContextCompat.getDrawable(getApplicationContext(),R.drawable.direc)))
}
How would i set float value from val degrees= item.getString("degrees")
to R.drawable.direc
inside this loop?
Solution
I have found the solution, MPAndroidChart accepts only drawables as icons to their charts and i wanted to rotate that drawable (it would be easy if there were ImageViews).
So the solution was to convert drawable to bitmap, rotate that bitmap and then convert it back to drawable and set it as icon:
val jsonTemperatureData = JSONArray(result?.get(0))
for (i in 0 until jsonTemperatureData.length()) {
val item = jsonTemperatureData.getJSONObject(i)
val reading_temperature = item.getString("reading_windspeed")
val reading_direction = item.getString("reading_winddirection")
val hour = item.getString("hour")
if(item.getDouble("maxspeed") > max)
max = item.getDouble("maxspeed")
if(item.getDouble("minspeed")< min)
min = item.getDouble("minspeed")
var icon = BitmapFactory.decodeResource(applicationContext.getResources(),
R.drawable.direction0)
val rotatedBitmap = icon.rotate(reading_direction.toFloat())
var d: Drawable = BitmapDrawable(getResources(), rotatedBitmap)
yVals.add(Entry(hour.toFloat(), reading_temperature.toFloat(), d))
}
Answered By - stacks
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.