Search code examples
javaandroidanimationbackgroundandroid-drawable

Moving colors Android Studio


i am developing an android application. In this application i want to move colors as in the picture below.Colors

1)I've tried to rotate background drawable:This

2)I've tried animation-list but this wasn't the solution.

Layout

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="@drawable/border"
    android:contentDescription="@string/qr"
    android:cropToPadding="true"
    android:padding="10dp"
    android:scaleType="centerCrop"
    />

@drawable/border

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:angle="270"
    android:endColor="@color/colorYellowGreen"
    android:type="linear"
    android:centerColor="@color/colorBlue"
    android:startColor="@color/colorPink" />

<corners android:radius="5dp" />

</shape>

NOTE: I am not looking for rotating view. Because there is content inside imageview. So do not suggest to use ViewPropertyAnimator class


Solution

  • One way to do it would be the gradient with a timer. Here are two examples, one changes the colors and the other changes the angle hence creating a rotation effect.

    You can tweak them for your purposes.

    But be wary of the performance depending on how fast you want it to change.

    Rotation effect

    //Call this once to change gradient angle
    private fun startAngleChangeJob() {
        var shape = imageView.background
        var borderGrad = shape as GradientDrawable
    
        var timer = fixedRateTimer("colorTimer", false, 0L, 100) {
            var ori = borderGrad.orientation.ordinal
            var newOri = (ori + 1) % 7
    
            [email protected] {
                borderGrad.orientation = GradientDrawable.Orientation.values()[newOri]
            }
        }
    }
    

    Color change effect

    //Call this once in onCreate to change gradient colors
    private fun startColorChangeJob() {
        var timer = fixedRateTimer("colorTimer", false, 0L, 100) {
            var shape = imageView.background
            var borderGrad = shape as GradientDrawable
            var colors = borderGrad.colors
            var colorsNew = IntArray(colors!!.size)
            for (i in colors.indices) {
                var newInd = (i + 1) % colors.size
                colorsNew[newInd] = colors[i]
            }
            [email protected] {
                borderGrad.colors = colorsNew
            }
        }
    }
    

    Color shift effect

    for (i in colors.indices) {
                var oldColor = Color.valueOf(colors[i])
                var red = (oldColor.red() + 0.2f) % 1.0f
                var green = (oldColor.green() + 0.2f) % 1.0f
                var blue = (oldColor.blue() + 0.2f) % 1.0f
                colorsNew[i] = Color.argb(1.0f, red, green, blue)
            }