Android 旋转R.可在kotlin中拉拔

Android 旋转R.可在kotlin中拉拔,android,kotlin,android-drawable,Android,Kotlin,Android Drawable,我需要将风向读数显示为箭头,原始bmp是一个旋转为0°(指向上方)的箭头,我有一个for循环,可以从数据库中检索角度,我可以将可绘制设置为mpandroidchart,但我需要旋转该可绘制 val jsonTemperatureData = JSONArray(result?.get(0)) for (i in 0 until jsonTemperatureData.length()) { val item = jsonTemperatureData.getJSONObject(i

我需要将风向读数显示为箭头,原始bmp是一个旋转为0°(指向上方)的箭头,我有一个for循环,可以从数据库中检索角度,我可以将可绘制设置为mpandroidchart,但我需要旋转该可绘制

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

在这个循环中,如何将浮点值从
val degrees=item.getString(“degrees”)
设置为
R.drawable.direc

如果您的代码与以下类似:

<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"/>

我建议您旋转
ImageView
,而不是旋转drawable

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

mImageView.setRotation(你的角度在这里)
使用API>=11


我找到了解决方案,MPAndroidChart只接受可绘制图标作为其图表的图标,我想旋转该可绘制图标(如果有ImageView,这将很容易)

因此,解决方案是将可绘制转换为位图,旋转该位图,然后将其转换回可绘制,并将其设置为图标:

 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))
            }
val jsonTemperatureData=JSONArray(结果?.get(0))
对于(0中的i,直到jsonTemperatureData.length()){
val item=jsonTemperatureData.getJSONObject(i)
val reading_temperature=item.getString(“reading_windspeed”)
val reading_direction=item.getString(“reading_windirection”)
val hour=item.getString(“小时”)
如果(item.getDouble(“maxspeed”)>max)
max=item.getDouble(“maxspeed”)
if(item.getDouble(“minspeed”)
我正在使用:ContextCompat.getDrawable(getApplicationContext(),R.drawable.direc)将drawable直接设置为mpandroidchart。我没有使用ImageView。我可以拥有一些您用于UI的特定视图吗?您找到解决方案了吗?我已将其作为解决方案发布,我正在将drawable直接设置为mpandroidchart line char,因此我需要旋转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))
            }