diff --git a/build.gradle b/build.gradle index 5e8ada5..1431fda 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:3.3.1' + classpath 'com.android.tools.build:gradle:3.3.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Bitmap.kt b/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Bitmap.kt index ac8cc4b..f4a36b3 100644 --- a/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Bitmap.kt +++ b/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Bitmap.kt @@ -22,12 +22,10 @@ import androidx.annotation.DrawableRes import androidx.palette.graphics.Palette import java.io.ByteArrayOutputStream -/** - * @author jieyi - * @since 2018/01/12 - */ - -fun Resources.createBitmap(drawableResId: Int, opts: BitmapFactory.Options? = null, rect: Rect? = null): Bitmap? { +fun Resources.createBitmap( + drawableResId: Int, opts: BitmapFactory.Options? = null, + rect: Rect? = null +): Bitmap? { var bitmap: Bitmap? = null openRawResource(drawableResId).use { bitmap = BitmapFactory.decodeStream(it, rect, opts) @@ -65,7 +63,7 @@ fun Bitmap.scale(widthRatio: Float, heightRatio: Float): Bitmap { fun Bitmap.scale(ratio: Float) = scale(ratio, ratio) fun Resources.createCompressedBitmap( - drawableResId: Int, + @DrawableRes drawableResId: Int, simpleSize: Int = 1, bitmapConf: Bitmap.Config? = null ): Bitmap { diff --git a/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Keyboard.kt b/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Keyboard.kt index f27278c..840b63d 100644 --- a/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Keyboard.kt +++ b/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Keyboard.kt @@ -30,7 +30,7 @@ fun isShowingSoftKeyboard(rootView: View): Boolean { val heightDiff = bottom - r.bottom - heightDiff > softKeyboardHeight * resources.displayMetrics.density + heightDiff > softKeyboardHeight * displayMetrics().density } } diff --git a/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Resolution.kt b/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Resolution.kt deleted file mode 100644 index 9cc74b4..0000000 --- a/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Resolution.kt +++ /dev/null @@ -1,36 +0,0 @@ -package com.devrapid.kotlinknifer - -import android.content.Context -import android.content.res.Resources -import org.jetbrains.anko.px2dip -import org.jetbrains.anko.px2sp - -/** Convert dp to px */ -fun Float.dp2px(context: Context) = this * context.resources.displayMetrics.density + 0.5f - -/** Convert px to dp */ -fun Float.px2dp(context: Context) = context.px2dip(this.toInt()) - -/** Convert px to sp */ -fun Float.px2sp(context: Context) = context.px2sp(this.toInt()) - -/** Convert sp to px */ -fun Float.sp2px(context: Context) = this * context.resources.displayMetrics.scaledDensity + 0.5f - -/** Convert dp to px */ -fun Int.dp2px(context: Context) = this * context.resources.displayMetrics.density + 0.5f - -/** Convert px to dp */ -fun Int.px2dp(context: Context) = context.px2dip(this) - -/** Convert px to sp */ -fun Int.px2sp(context: Context) = context.px2sp(this) - -/** Convert sp to px */ -fun Int.sp2px(context: Context) = this * context.resources.displayMetrics.scaledDensity + 0.5f - -// Convert dimensions without Context -val Int.dp get() = this * Resources.getSystem().displayMetrics.density + 0.5f -val Int.sp get() = this * Resources.getSystem().displayMetrics.scaledDensity + 0.5f -val Float.dp get() = this * Resources.getSystem().displayMetrics.density + 0.5f -val Float.sp get() = this * Resources.getSystem().displayMetrics.scaledDensity + 0.5f diff --git a/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Resource.kt b/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Resource.kt new file mode 100644 index 0000000..2dd1a2e --- /dev/null +++ b/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Resource.kt @@ -0,0 +1,178 @@ +package com.devrapid.kotlinknifer + +import android.app.Activity +import android.content.Context +import android.content.res.Resources.Theme +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.graphics.Rect +import android.os.Build +import android.view.View +import androidx.annotation.AnimRes +import androidx.annotation.AnimatorRes +import androidx.annotation.DimenRes +import androidx.annotation.DrawableRes +import androidx.annotation.FontRes +import androidx.annotation.RequiresApi +import androidx.fragment.app.Fragment + +//region Context +inline fun Context.getDimen(@DimenRes id: Int) = resources.getDimension(id) + +inline fun Context.getDimenPixelSize(@DimenRes id: Int) = resources.getDimensionPixelSize(id) + +inline fun Context.getAnimation(@AnimatorRes @AnimRes id: Int) = resources.getAnimation(id) + +inline fun Context.getIdentifier(name: String, defType: String, defPackage: String) = + resources.getIdentifier(name, defType, defPackage) + +inline fun Context.getDrawable(@DrawableRes id: Int, theme: Theme? = null) = resources.getDrawable(id, theme) + +@RequiresApi(Build.VERSION_CODES.O) +inline fun Context.getFont(@FontRes id: Int) = resources.getFont(id) + +inline fun Context.displayMetrics() = resources.displayMetrics + +inline fun Context.createBitmap(@DrawableRes id: Int, opts: BitmapFactory.Options? = null, rect: Rect? = null) = + resources.createBitmap(id, opts, rect) + +inline fun Context.obtainBitmapSize(@DrawableRes id: Int) = resources.obtainBitmapSize(id) + +inline fun Context.createCompressedBitmap( + @DrawableRes id: Int, simpleSize: Int = 1, + bitmapConf: Bitmap.Config? = null +) = resources.createCompressedBitmap(id, simpleSize, bitmapConf) + +inline fun Context.createScaledBitmap(@DrawableRes id: Int, width: Int, height: Int) = + resources.createScaledBitmap(id, width, height) + +inline fun Context.createScaledBitmap(@DrawableRes id: Int, widthRatio: Float, heightRatio: Float) = + resources.createScaledBitmap(id, widthRatio, heightRatio) + +inline fun Context.createScaledBitmap(@DrawableRes id: Int, ratio: Float) = resources.createScaledBitmap(id, ratio) + +inline fun Context.createRegionBitmap( + @DrawableRes id: Int, rect: Rect, + opts: BitmapFactory.Options = BitmapFactory.Options() +) = resources.createRegionBitmap(id, rect, opts) +//endregion + +//region Activity +inline fun Activity.getDimen(@DimenRes id: Int) = resources.getDimension(id) + +inline fun Activity.getDimenPixelSize(@DimenRes id: Int) = resources.getDimensionPixelSize(id) + +inline fun Activity.getAnimation(@AnimatorRes @AnimRes id: Int) = resources.getAnimation(id) + +inline fun Activity.getIdentifier(name: String, defType: String, defPackage: String) = + resources.getIdentifier(name, defType, defPackage) + +inline fun Activity.getDrawable(@DrawableRes id: Int, theme: Theme? = null) = resources.getDrawable(id, theme) +@RequiresApi(Build.VERSION_CODES.O) + +inline fun Activity.getFont(@FontRes id: Int) = resources.getFont(id) + +inline fun Activity.displayMetrics() = resources.displayMetrics + +inline fun Activity.createBitmap(@DrawableRes id: Int, opts: BitmapFactory.Options? = null, rect: Rect? = null) = + resources.createBitmap(id, opts, rect) + +inline fun Activity.obtainBitmapSize(@DrawableRes id: Int) = resources.obtainBitmapSize(id) + +inline fun Activity.createCompressedBitmap( + @DrawableRes id: Int, simpleSize: Int = 1, + bitmapConf: Bitmap.Config? = null +) = resources.createCompressedBitmap(id, simpleSize, bitmapConf) + +inline fun Activity.createScaledBitmap(@DrawableRes id: Int, width: Int, height: Int) = + resources.createScaledBitmap(id, width, height) + +inline fun Activity.createScaledBitmap(@DrawableRes id: Int, widthRatio: Float, heightRatio: Float) = + resources.createScaledBitmap(id, widthRatio, heightRatio) + +inline fun Activity.createScaledBitmap(@DrawableRes id: Int, ratio: Float) = resources.createScaledBitmap(id, ratio) + +inline fun Activity.createRegionBitmap( + @DrawableRes id: Int, rect: Rect, + opts: BitmapFactory.Options = BitmapFactory.Options() +) = resources.createRegionBitmap(id, rect, opts) +//endregion + +//region Fragment +inline fun Fragment.getDimen(@DimenRes id: Int) = resources.getDimension(id) + +inline fun Fragment.getDimenPixelSize(@DimenRes id: Int) = resources.getDimensionPixelSize(id) + +inline fun Fragment.getAnimation(@AnimatorRes @AnimRes id: Int) = resources.getAnimation(id) + +inline fun Fragment.getIdentifier(name: String, defType: String, defPackage: String) = + resources.getIdentifier(name, defType, defPackage) + +inline fun Fragment.getDrawable(@DrawableRes id: Int, theme: Theme? = null) = resources.getDrawable(id, theme) + +@RequiresApi(Build.VERSION_CODES.O) +inline fun Fragment.getFont(@FontRes id: Int) = resources.getFont(id) + +inline fun Fragment.displayMetrics() = resources.displayMetrics + +inline fun Fragment.createBitmap(@DrawableRes id: Int, opts: BitmapFactory.Options? = null, rect: Rect? = null) = + resources.createBitmap(id, opts, rect) + +inline fun Fragment.obtainBitmapSize(@DrawableRes id: Int) = resources.obtainBitmapSize(id) + +inline fun Fragment.createCompressedBitmap( + @DrawableRes id: Int, simpleSize: Int = 1, + bitmapConf: Bitmap.Config? = null +) = resources.createCompressedBitmap(id, simpleSize, bitmapConf) + +inline fun Fragment.createScaledBitmap(@DrawableRes id: Int, width: Int, height: Int) = + resources.createScaledBitmap(id, width, height) + +inline fun Fragment.createScaledBitmap(@DrawableRes id: Int, widthRatio: Float, heightRatio: Float) = + resources.createScaledBitmap(id, widthRatio, heightRatio) + +inline fun Fragment.createScaledBitmap(@DrawableRes id: Int, ratio: Float) = resources.createScaledBitmap(id, ratio) +inline fun Fragment.createRegionBitmap( + @DrawableRes id: Int, rect: Rect, + opts: BitmapFactory.Options = BitmapFactory.Options() +) = resources.createRegionBitmap(id, rect, opts) +//endregion + +//region View +inline fun View.getDimen(@DimenRes id: Int) = resources.getDimension(id) + +inline fun View.getDimenPixelSize(@DimenRes id: Int) = resources.getDimensionPixelSize(id) + +inline fun View.getAnimation(@AnimatorRes @AnimRes id: Int) = resources.getAnimation(id) + +inline fun View.getIdentifier(name: String, defType: String, defPackage: String) = + resources.getIdentifier(name, defType, defPackage) + +inline fun View.getDrawable(@DrawableRes id: Int, theme: Theme? = null) = resources.getDrawable(id, theme) + +@RequiresApi(Build.VERSION_CODES.O) +inline fun View.getFont(@FontRes id: Int) = resources.getFont(id) + +inline fun View.displayMetrics() = resources.displayMetrics + +inline fun View.createBitmap(@DrawableRes id: Int, opts: BitmapFactory.Options? = null, rect: Rect? = null) = + resources.createBitmap(id, opts, rect) + +inline fun View.obtainBitmapSize(@DrawableRes id: Int) = resources.obtainBitmapSize(id) + +inline fun View.createCompressedBitmap(@DrawableRes id: Int, simpleSize: Int = 1, bitmapConf: Bitmap.Config? = null) = + resources.createCompressedBitmap(id, simpleSize, bitmapConf) + +inline fun View.createScaledBitmap(@DrawableRes id: Int, width: Int, height: Int) = + resources.createScaledBitmap(id, width, height) + +inline fun View.createScaledBitmap(@DrawableRes id: Int, widthRatio: Float, heightRatio: Float) = + resources.createScaledBitmap(id, widthRatio, heightRatio) + +inline fun View.createScaledBitmap(@DrawableRes id: Int, ratio: Float) = resources.createScaledBitmap(id, ratio) + +inline fun View.createRegionBitmap( + @DrawableRes id: Int, rect: Rect, + opts: BitmapFactory.Options = BitmapFactory.Options() +) = resources.createRegionBitmap(id, rect, opts) +//endregion diff --git a/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/View.kt b/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/View.kt index d1e3d75..de0eb0d 100644 --- a/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/View.kt +++ b/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/View.kt @@ -82,15 +82,13 @@ fun Context.alert(message: Int, title: Int? = null, init: (AlertDialog.Builder.( init?.let { init() } } -inline fun Context.navigationBarHeiht() = - resources.getIdentifier("navigation_bar_height", "dimen", "android") - .takeIf { 0 < it } - ?.let { resources.getDimensionPixelSize(it) } ?: 0 - -inline fun Context.statusBarHeight() = - resources.getIdentifier("status_bar_height", "dimen", "android") - .takeIf { 0 < it } - ?.let { resources.getDimensionPixelSize(it) } ?: 0 +inline fun Context.navigationBarHeiht() = getIdentifier("navigation_bar_height", "dimen", "android") + .takeIf { 0 < it } + ?.let { getDimenPixelSize(it) } ?: 0 + +inline fun Context.statusBarHeight() = getIdentifier("status_bar_height", "dimen", "android") + .takeIf { 0 < it } + ?.let { getDimenPixelSize(it) } ?: 0 inline fun Activity.statusBarHeight() = Rect() .apply { window.decorView.getWindowVisibleDisplayFrame(this) } diff --git a/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/recyclerview/itemdecorator/GridSpacingItemDecorator.kt b/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/recyclerview/itemdecorator/GridSpacingItemDecorator.kt index fb6ca35..7459bff 100644 --- a/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/recyclerview/itemdecorator/GridSpacingItemDecorator.kt +++ b/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/recyclerview/itemdecorator/GridSpacingItemDecorator.kt @@ -4,12 +4,6 @@ import android.graphics.Rect import android.view.View import androidx.recyclerview.widget.RecyclerView -/** - * The item decorator for grid type recycler view. - * - * @author jieyi - * @since 11/11/17 - */ class GridSpacingItemDecorator(private val spanCount: Int, private val spacing: Int, private val includeEdge: Boolean) : RecyclerView.ItemDecoration() { override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) { diff --git a/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/recyclerview/itemdecorator/HorizontalItemDecorator.kt b/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/recyclerview/itemdecorator/HorizontalItemDecorator.kt index ecbe7e4..ca90f8f 100644 --- a/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/recyclerview/itemdecorator/HorizontalItemDecorator.kt +++ b/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/recyclerview/itemdecorator/HorizontalItemDecorator.kt @@ -4,12 +4,6 @@ import android.graphics.Rect import android.view.View import androidx.recyclerview.widget.RecyclerView -/** - * The item decorator for horizontal recycler view. - * - * @author jieyi - * @since 11/11/17 - */ class HorizontalItemDecorator(private val space: Int) : RecyclerView.ItemDecoration() { override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) { val position: Int = parent.getChildAdapterPosition(view) diff --git a/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/recyclerview/itemdecorator/VerticalItemDecorator.kt b/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/recyclerview/itemdecorator/VerticalItemDecorator.kt index 7c6ba78..e79a031 100644 --- a/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/recyclerview/itemdecorator/VerticalItemDecorator.kt +++ b/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/recyclerview/itemdecorator/VerticalItemDecorator.kt @@ -4,12 +4,6 @@ import android.graphics.Rect import android.view.View import androidx.recyclerview.widget.RecyclerView -/** - * The item decorator for vertical type recycler view. - * - * @author jieyi - * @since 11/11/17 - */ class VerticalItemDecorator( private val topBottom: Int, private val leftRight: Int = topBottom