Skip to content
This repository has been archived by the owner on May 19, 2022. It is now read-only.

Commit

Permalink
fix gradient icon size, fix dp and px unit
Browse files Browse the repository at this point in the history
  • Loading branch information
Mystery00 committed Jul 29, 2018
1 parent 7f35371 commit 3e3766d
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@ class MainActivity : AppCompatActivity() {
bottomTabView.config
.setSelectedColor(Color.BLACK)
.setUnSelectedColor(Color.BLUE)
.setMarginTop(16f)
.setMarginBottom(24f)
.setItemIconSize(24f)
.setItemTextSize(24f)
.setItemIconSize(16f)
.setLineHeight(1f)
.isShowRipple(true)
.isShowGradientColors(false)
.setGradientColors(intArrayOf(Color.WHITE, Color.BLACK))
bottomTabView.init()
.isShowGradientColors(true)
.setGradientColors(intArrayOf(Color.BLUE, Color.WHITE))
bottomTabView.setMenuList(menuList)
bottomTabView.setOnItemSelectedListener {
println(it.name)
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
android:id="@+id/bottomTabView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:icon_margin="4dp"
app:item_text_size="10sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:margin_bottom="2dp"
app:margin_top="2dp"
app:selected_color="@color/colorAccent"
app:unselected_color="@color/colorPrimary" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
Expand All @@ -14,6 +15,7 @@ import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import vip.mystery0.bottomTabView.util.DensityTools
import vip.mystery0.bottomTabView.util.ImageUtil
import java.util.ArrayList

class BottomTabView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : LinearLayout(context, attrs, defStyleAttr) {
Expand All @@ -36,15 +38,17 @@ class BottomTabView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
if (typedArray.hasValue(R.styleable.BottomTabView_unselected_color))
config.unSelectedColor = typedArray.getColor(R.styleable.BottomTabView_unselected_color, config.unSelectedColor)
if (typedArray.hasValue(R.styleable.BottomTabView_margin_top))
config.marginTop = typedArray.getDimension(R.styleable.BottomTabView_margin_top, config.marginTop)
config.marginTop = DensityTools.px2dp(context, typedArray.getDimension(R.styleable.BottomTabView_margin_top, 0f))
if (typedArray.hasValue(R.styleable.BottomTabView_margin_bottom))
config.marginBottom = typedArray.getDimension(R.styleable.BottomTabView_margin_bottom, config.marginBottom)
config.marginBottom = DensityTools.px2dp(context, typedArray.getDimension(R.styleable.BottomTabView_margin_bottom, 0f))
if (typedArray.hasValue(R.styleable.BottomTabView_line_height))
config.lineHeight = typedArray.getDimension(R.styleable.BottomTabView_line_height, config.lineHeight)
config.lineHeight = DensityTools.px2dp(context, typedArray.getDimension(R.styleable.BottomTabView_line_height, 0f))
if (typedArray.hasValue(R.styleable.BottomTabView_item_text_size))
config.itemTextSize = typedArray.getDimension(R.styleable.BottomTabView_item_text_size, config.itemTextSize)
config.itemTextSize = DensityTools.px2dp(context, typedArray.getDimension(R.styleable.BottomTabView_item_text_size, config.itemTextSize))
if (typedArray.hasValue(R.styleable.BottomTabView_item_icon_size))
config.itemIconSize = typedArray.getDimension(R.styleable.BottomTabView_item_icon_size, config.itemIconSize)
config.itemIconSize = DensityTools.px2dp(context, typedArray.getDimension(R.styleable.BottomTabView_item_icon_size, 0f))
if (typedArray.hasValue(R.styleable.BottomTabView_icon_margin))
config.iconMargin = DensityTools.px2dp(context, typedArray.getDimension(R.styleable.BottomTabView_icon_margin, 0f))
if (typedArray.hasValue(R.styleable.BottomTabView_show_ripple))
config.isShowRipple = typedArray.getBoolean(R.styleable.BottomTabView_show_ripple, config.isShowRipple)
if (typedArray.hasValue(R.styleable.BottomTabView_show_gradient_colors))
Expand Down Expand Up @@ -109,25 +113,30 @@ class BottomTabView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
}

private fun drawDrawable(bottomTabItem: BottomTabItem): Drawable? {
val size = DensityTools.dp2px(context, config.itemIconSize)
if (!bottomTabItem.isChecked) {
val drawable = ContextCompat.getDrawable(context, bottomTabItem.unSelectedIcon)!!
DrawableCompat.setTint(drawable.mutate(), config.unSelectedColor)
return drawable
return ImageUtil.zoomDrawable(context, drawable, size, size)
}
val size = DensityTools.dp2px(context, config.itemIconSize)
val drawable = ContextCompat.getDrawable(context, bottomTabItem.selectedIcon)!!
val bitmap = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
val bitmapShader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
//创建线性着色器并配置着色
val linearGradient = LinearGradient(size / 2f, 0f, size / 2f, size.toFloat(), config.gradientColors, null, Shader.TileMode.CLAMP)
val composeShader = ComposeShader(bitmapShader, linearGradient, PorterDuff.Mode.MULTIPLY)
val paint = Paint()
paint.shader = composeShader
canvas.drawRect(0f, 0f, size.toFloat(), size.toFloat(), paint)
return BitmapDrawable(context.resources, bitmap)
val scaleBitmap = ImageUtil.zoomBitmap(bitmap, size, size)
if (config.isShowGradientColors) {
val gradientCanvas = Canvas(scaleBitmap)
val bitmapShader = BitmapShader(scaleBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
//创建线性着色器并配置着色
val linearGradient = LinearGradient(size / 2f, 0f, size / 2f, size, config.gradientColors, null, Shader.TileMode.CLAMP)
val composeShader = ComposeShader(bitmapShader, linearGradient, PorterDuff.Mode.MULTIPLY)
val paint = Paint()
paint.shader = composeShader
gradientCanvas.drawRect(0f, 0f, size, size, paint)
}
bitmap.recycle()
return BitmapDrawable(context.resources, scaleBitmap)
}

private fun createItemView(bottomTabItem: BottomTabItem): View {
Expand All @@ -142,10 +151,11 @@ class BottomTabView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
itemView.layoutParams = frameLayoutParams
val textView = itemView.findViewById<TextView>(R.id.textView)
textView.text = bottomTabItem.name
textView.textSize = config.itemTextSize
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, config.itemTextSize)
textView.setCompoundDrawablesWithIntrinsicBounds(null, drawDrawable(bottomTabItem), null, null)
textView.compoundDrawablePadding = config.iconMargin.toInt()
val textViewLayoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT)
textViewLayoutParams.setMargins(0, DensityTools.dp2px(context, config.marginTop), 0, DensityTools.dp2px(context, config.marginBottom))
textViewLayoutParams.setMargins(0, DensityTools.dp2px(context, config.marginTop).toInt(), 0, DensityTools.dp2px(context, config.marginBottom).toInt())
textViewLayoutParams.gravity = Gravity.CENTER
textView.layoutParams = textViewLayoutParams
textView.gravity = Gravity.CENTER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ class BottomTabViewConfig {
@ColorInt
var unSelectedColor: Int = Color.BLUE

var marginTop: Float = 8f//单位dp
var marginBottom: Float = 8f//单位dp
var marginTop = 2f//单位dp
var marginBottom = 2f//单位dp
var itemTextSize = 14f//单位sp
var itemIconSize = 24f//单位dp
var iconMargin = 2f//图标间距,单位dp

var lineHeight = 1f//单位px
var lineHeight = 0.33f//单位dp
@ColorInt
var lineColor = Color.parseColor("#e5e5e5")
@ColorInt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ object DensityTools {
* @param dpValue dp值
* @return 转换之后的px值
*/
fun dp2px(context: Context, dpValue: Float): Int {
return (dpValue * context.resources.displayMetrics.density + 0.5).toInt()
fun dp2px(context: Context, dpValue: Float): Float {
return (dpValue * context.resources.displayMetrics.density + 0.5).toFloat()
}

/**
Expand All @@ -19,8 +19,8 @@ object DensityTools {
* @param pxValue px值
* @return 转换之后的dp值
*/
fun px2dp(context: Context, pxValue: Float): Int {
return (pxValue / context.resources.displayMetrics.density + 0.5).toInt()
fun px2dp(context: Context, pxValue: Float): Float {
return (pxValue / context.resources.displayMetrics.density + 0.5).toFloat()
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package vip.mystery0.bottomTabView.util

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.drawable.Drawable
import android.graphics.PixelFormat
import android.graphics.drawable.BitmapDrawable


object ImageUtil {
fun zoomBitmap(bitmap: Bitmap, width: Float, height: Float): Bitmap {
val oldWidth = bitmap.width
val oldHeight = bitmap.height
val scaleWidth = width / oldWidth.toFloat()
val scaleHeight = height / oldHeight.toFloat()
val matrix = Matrix()
matrix.postScale(scaleWidth, scaleHeight)
return Bitmap.createBitmap(bitmap, 0, 0, oldWidth, oldHeight, matrix, true)
}

fun zoomDrawable(context: Context, drawable: Drawable, width: Float, height: Float): Drawable {
val bitmap = drawableToBitmap(drawable)
return BitmapDrawable(context.resources, zoomBitmap(bitmap, width, height))
}

fun drawableToBitmap(drawable: Drawable): Bitmap {
val width = drawable.intrinsicWidth//取drawable的长宽
val height = drawable.intrinsicHeight
val config = if (drawable.opacity != PixelFormat.OPAQUE)
Bitmap.Config.ARGB_8888 else Bitmap.Config.RGB_565//取drawable的颜色格式
val bitmap = Bitmap.createBitmap(width, height, config)//建立对应bitmap
val canvas = Canvas(bitmap)//建立对应bitmap的画布
drawable.setBounds(0, 0, width, height)
drawable.draw(canvas)//把drawable内容画到画布中
return bitmap
}
}
9 changes: 5 additions & 4 deletions bottomTabView/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
<attr name="unselected_color" format="reference|color" />
<attr name="margin_top" format="dimension" />
<attr name="margin_bottom" format="dimension" />
<attr name="line_height" format="dimension"/>
<attr name="line_height" format="dimension" />
<attr name="item_text_size" format="dimension" />
<attr name="item_icon_size" format="dimension"/>
<attr name="show_ripple" format="boolean"/>
<attr name="show_gradient_colors" format="boolean"/>
<attr name="item_icon_size" format="dimension" />
<attr name="icon_margin" format="dimension" />
<attr name="show_ripple" format="boolean" />
<attr name="show_gradient_colors" format="boolean" />
</declare-styleable>
</resources>

0 comments on commit 3e3766d

Please sign in to comment.