简体   繁体   English

在 kotlin 中旋转 R.drawable

[英]Rotate R.drawable in kotlin

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.我需要将风向读数显示为箭头,原始 bmp 是一个旋转为 0°(指向上方)的箭头,我有一个 for 循环,我可以在其中从数据库中检索度数,我可以将 drawable 设置为我的 mpandroidchart 但我需要旋转那个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?我将如何在此循环中将浮点值从val degrees= item.getString("degrees")R.drawable.direc

If you are using imageView, there is a property of it: rotation如果您使用的是 imageView,它有一个属性:旋转

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()

Rather than rotating the drawable, I would suggest you to rotate the ImageView .我建议您旋转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 mImageView.setRotation(YOUR_ANGLE_GOES_HERE)与 API>=11

Source 来源

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).我找到了解决方案,MPAndroidChart 只接受可绘制对象作为其图表的图标,我想旋转该可绘制对象(如果有 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:所以解决方案是将 drawable 转换为位图,旋转该位图,然后将其转换回 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 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))
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM