Skip to content

Instantly share code, notes, and snippets.

val colorCount = RAINBOW_COLORS.size
val colors = IntArray(colorCount * 2)
val stops = FloatArray(colorCount * 2)
for (i in 0 until colorCount) {
colors[i * 2] = ContextCompat.getColor(context, RAINBOW_COLORS[i])
colors[i * 2 + 1] = colors[i * 2]
stops[i * 2] = (i / colorCount.toFloat() + RAINBOW_EPSILON).coerceAtLeast(0f)
stops[i * 2 + 1] = ((i + 1) / colorCount.toFloat() - RAINBOW_EPSILON).coerceAtMost(1f)
}
return object : Drawable() {
private val paint = Paint()
init {
val scaledWidth = RAINBOW_WIDTH * context.resources.displayMetrics.density
val y1 = (Math.sin(2 * RAINBOW_ANGLE) / 2 * scaledWidth).toFloat()
val x1 = (scaledWidth - Math.tan(RAINBOW_ANGLE) * y1).toFloat()
paint.shader = LinearGradient(
0f, 0f,
x1, y1,
val colorCount = RAINBOW_COLORS.size
val colors = IntArray(colorCount * 2)
val stops = FloatArray(colorCount * 2)
for (i in 0 until colorCount) {
colors[i * 2] = ContextCompat.getColor(context, RAINBOW_COLORS[i])
colors[i * 2 + 1] = colors[i * 2]
stops[i * 2] = i / colorCount.toFloat()
stops[i * 2 + 1] = (i + 1) / colorCount.toFloat()
}
@ColorRes
private val RAINBOW_COLORS = intArrayOf(
R.color.pink,
R.color.orange,
R.color.yellow,
R.color.green,
R.color.emerald_green,
R.color.azur_blue,
R.color.blue,
R.color.cerulean_blue,
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/rainbow_stripes_pattern"
android:tileMode="repeat" />
@cyrilmottier
cyrilmottier / CityBikesContract.java
Last active January 12, 2024 18:04
Using the new Gradle-based Android build system: a second example
package com.cyrilmottier.android.citybikes.provider;
import android.net.Uri;
import com.cyrilmottier.android.avelov.BuildConfig;
/**
* @author Cyril Mottier
*/
public class CityBikesContract {
@cyrilmottier
cyrilmottier / ResourcesAdditions.java
Last active January 12, 2024 17:55
Lightweight key-value pairs resources for Android applications.
package com.cyrilmottier.android.resourcesadditions;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
/**
* @author Cyril Mottier
@cyrilmottier
cyrilmottier / AndroidManifest.xml
Last active January 30, 2023 00:04
Android example of how to add a custom logo to the ActionBar and ensure the best possible matching starting Window.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cyrilmottier.android.anapp">
<!-- ... -->
<application
android:icon="@drawable/ic_launcher"
android:label="@string/config_app_name"
android:theme="@style/Theme.AnApp" >
@cyrilmottier
cyrilmottier / _app_avelibRelease_res_values_config.xml
Last active November 20, 2020 11:27
Using the new Gradle-based Android build system: an example
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="config_app_name">AVélib</string>
<string name="config_authority">com.cyrilmottier.android.avelib.citybikes</string>
<string name="config_com.google.android.maps.v2.api_key">XXX</string>
</resources>
@cyrilmottier
cyrilmottier / ActionBarDrawerToggleHoneycomb.java
Created May 21, 2013 09:07
Extract from <android_sdk>/extras/android/support/v4/src/honeycomb/android/support/v4/app/ActionBarDrawerToggleHoneycomb.java
/**
* This class encapsulates some awful hacks.
*
* Before JB-MR2 (API 18) it was not possible to change the home-as-up indicator glyph
* in an action bar without some really gross hacks. Since the MR2 SDK is not published as of
* this writing, the new API is accessed via reflection here if available.
*/
class ActionBarDrawerToggleHoneycomb {
private static final String TAG = "ActionBarDrawerToggleHoneycomb";