0

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

Snefru Clone
  • 77
  • 1
  • 9

1 Answers1

2

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

        this@BorderActivity.runOnUiThread {
            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]
        }
        this@BorderActivity.runOnUiThread {
            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)
        }
mbu
  • 66
  • 3
  • I am developing with java so what should i use instead of fixedRateTimer – Snefru Clone Sep 28 '20 at 12:37
  • You can use Timer.scheduleAtFixedRate https://developer.android.com/reference/java/util/Timer#scheduleAtFixedRate(java.util.TimerTask,%20long,%20long) – mbu Sep 28 '20 at 12:42
  • Both of them are working properly but could we make transactions continuously. In this code how can i reduce distances when switching between colors. They jump immediately and i dont want it. – Snefru Clone Sep 28 '20 at 14:40
  • That means if the rotation angle changes by 45 , how can we reduce it to 1 – Snefru Clone Sep 28 '20 at 14:52
  • Orientation works on an enum, so values are fixed and there are only 8 options. For that effect you may need to change the color codes of your colors. See the update to my answer and for the color loop use the one specified as **Color shift effect** – mbu Sep 28 '20 at 17:54
  • Hey, I have also found a similar question in [here](https://stackoverflow.com/questions/32224797/how-to-animate-gradient). Also color shift effect did not work. Thanks for replying and sharing your solutions. – Snefru Clone Sep 29 '20 at 11:23