0

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?

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
stacks
  • 221
  • 3
  • 13

3 Answers3

0

If you are using imageView, there is a property of it: rotation

if you have code similar to below:

<ImageView
        android:id="@+id/imageView"
        android:layout_width="60dp"
        android:layout_height="60dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

below code works for me:

imageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),R.drawable.direc))
imageView.rotation = imageView.rotation + degrees.toFloat()
Sanket Vekariya
  • 2,848
  • 3
  • 12
  • 34
0

Rather than rotating the drawable, I would suggest you to rotate the ImageView.

Matrix matrix = new Matrix();
imageView.setScaleType(ImageView.ScaleType.MATRIX);   //required
val bounds: Any = imageView.getDrawable().getBounds()
matrix.postRotate( YOUR_ANGLE_GOES_HERE, bounds.width()/2, bounds.height()/2);
imageView.setImageMatrix(matrix);

or

mImageView.setRotation(YOUR_ANGLE_GOES_HERE) with API>=11

Source

theapache64
  • 10,926
  • 9
  • 65
  • 108
0

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))
            }
stacks
  • 221
  • 3
  • 13