Last active
February 11, 2020 17:59
Star
You must be signed in to star a gist
Draw gradient LP according to hour of the day
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import android.graphics.Canvas | |
import android.graphics.Paint | |
import android.os.Handler | |
import android.service.wallpaper.WallpaperService | |
import android.util.DisplayMetrics | |
import android.view.SurfaceHolder | |
import android.view.WindowManager | |
import java.util.* | |
class GradientDayLP : WallpaperService() { | |
private var mWallpaperPaint = Paint() | |
private var mDeviceWidth = 0F | |
private var mDeviceHeight = 0F | |
private var mHour = -1 | |
override fun onCreateEngine(): Engine { | |
applicationContext?.let { | |
//retrieve display specifications | |
val window = applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager | |
val d = DisplayMetrics() | |
window.defaultDisplay.getRealMetrics(d) | |
mDeviceWidth = d.widthPixels.toFloat() | |
mDeviceHeight = d.heightPixels.toFloat() | |
} | |
//set paints props | |
mWallpaperPaint.isAntiAlias = true | |
mWallpaperPaint.style = Paint.Style.FILL | |
return WallpaperEngine() | |
} | |
private inner class WallpaperEngine : WallpaperService.Engine() { | |
private val handler = Handler() | |
private var sVisible = true | |
private val drawRunner = Runnable { draw() } | |
override fun onVisibilityChanged(visible: Boolean) { | |
sVisible = visible | |
if (visible) { | |
handler.post(drawRunner) | |
} else { | |
handler.removeCallbacks(drawRunner) | |
mHour = -1 | |
} | |
} | |
override fun onSurfaceDestroyed(holder: SurfaceHolder) { | |
super.onSurfaceDestroyed(holder) | |
sVisible = false | |
handler.removeCallbacks(drawRunner) | |
mHour = -1 | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
sVisible = false | |
handler.removeCallbacks(drawRunner) | |
mHour = -1 | |
} | |
//draw gradient according to hour | |
private fun draw() { | |
val holder = surfaceHolder | |
var canvas: Canvas? = null | |
try { | |
//get battery level | |
val hour = getHour() | |
if (mHour != hour) { | |
mHour = hour | |
canvas = holder.lockCanvas() | |
if (canvas != null) { | |
//here you draw the lp | |
GradientObject.draw( | |
canvas, | |
mWallpaperPaint, | |
mDeviceWidth, | |
mDeviceHeight, | |
mHour | |
) | |
} | |
} | |
} finally { | |
if (canvas != null) | |
holder.unlockCanvasAndPost(canvas) | |
} | |
handler.removeCallbacks(drawRunner) | |
if (sVisible) handler.postDelayed( | |
drawRunner, | |
5000 // here you set time in ms | |
) | |
} | |
} | |
private fun getHour() = Calendar.getInstance().get(Calendar.HOUR_OF_DAY) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Bitmap | |
import android.graphics.Canvas | |
import android.graphics.Color | |
import android.graphics.Paint | |
import android.graphics.drawable.Drawable | |
import android.graphics.drawable.GradientDrawable | |
import androidx.annotation.NonNull | |
object GradientObject { | |
fun draw( | |
@NonNull c: Canvas?, | |
@NonNull paint: Paint, | |
cw: Float, | |
ch: Float, | |
hour: Int | |
) { | |
val gd = GradientDrawable( | |
getMode(hour), getColorsSet(hour) | |
) | |
c?.drawBitmap(convertGradientDrawableToBitmap(gd, cw.toInt(), ch.toInt())!!, 0f, 0f, paint) | |
} | |
private fun convertGradientDrawableToBitmap( | |
drawable: Drawable, | |
widthPixels: Int, | |
heightPixels: Int | |
): Bitmap? { | |
val mutableBitmap = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888) | |
val canvas = Canvas(mutableBitmap) | |
drawable.setBounds(0, 0, widthPixels, heightPixels) | |
drawable.draw(canvas) | |
return mutableBitmap | |
} | |
private fun getColorsSet(hour: Int) = when (hour) { | |
in 3..6 -> intArrayOf(Color.GREEN, Color.WHITE, Color.RED) | |
in 6..9 -> intArrayOf(Color.BLACK, Color.WHITE, Color.BLACK) | |
in 9..12 -> intArrayOf(Color.BLUE, Color.YELLOW, Color.DKGRAY) | |
//define other colors | |
/* in 12..15 -> | |
in 15..18 -> | |
in 18..21 -> | |
in 21..24 -> */ | |
else -> intArrayOf(Color.CYAN, Color.DKGRAY, Color.MAGENTA) | |
} | |
private fun getMode(hour: Int) = | |
when (hour) { | |
in 3..6 -> GradientDrawable.Orientation.BL_TR //bottom-left to top-right | |
in 6..9 -> GradientDrawable.Orientation.BOTTOM_TOP //bottom to top | |
in 9..12 -> GradientDrawable.Orientation.BR_TL //bottom-right to top-left | |
in 12..15 -> GradientDrawable.Orientation.LEFT_RIGHT //left to right | |
in 15..18 -> GradientDrawable.Orientation.RIGHT_LEFT //bottom-right to top-left | |
in 18..21 -> GradientDrawable.Orientation.TL_BR //top-left to bottom-right | |
in 21..24 -> GradientDrawable.Orientation.TOP_BOTTOM //top to bottom | |
else -> GradientDrawable.Orientation.TR_BL //0..3 //top-right to bottom-left | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment