Skip to content

Commit

Permalink
added attributes
Browse files Browse the repository at this point in the history
* fix bug create bitmap
  • Loading branch information
smerdinanton committed May 12, 2023
1 parent 101d1ae commit 3843eeb
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 98 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

implementation 'com.github.chapaevanton:targetingviews:v0.6.1-alpha'
implementation project(path: ':targetingviews')
}
19 changes: 9 additions & 10 deletions app/src/main/java/com/dpt/targetingviews/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ package com.dpt.targetingviews

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.dpt.inclineviews.InclinePlatformView

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// findViewById<InclinePlatformView>(R.id.incline_platform1)
// .setAngle(45.0)
// findViewById<InclinePlatformView>(R.id.incline_platform2)
// .setIcon(R.drawable.baseline_directions_car_24)
// .setAngle(-45.0)
// findViewById<InclinePlatformView>(R.id.incline_platform3)
// .isInverted(true)
// .setAngle(-45.0)
// findViewById<InclinePlatformView>(R.id.incline_platform4)
// findViewById<InclinePlatformView>(R.id.incline_platform5)
findViewById<InclinePlatformView>(R.id.incline_platform1)

findViewById<InclinePlatformView>(R.id.incline_platform2)
.setAngle(-45.0)
findViewById<InclinePlatformView>(R.id.incline_platform3)
.isInverted(true)
.setAngle(-35.0)
findViewById<InclinePlatformView>(R.id.incline_platform4)
.setAngle(-25.0)
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_boat_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M20,21c-1.39,0 -2.78,-0.47 -4,-1.32c-2.44,1.71 -5.56,1.71 -8,0C6.78,20.53 5.39,21 4,21H2v2h2c1.38,0 2.74,-0.35 4,-0.99c2.52,1.29 5.48,1.29 8,0c1.26,0.65 2.62,0.99 4,0.99h2v-2H20zM3.95,19H4c1.6,0 3.02,-0.88 4,-2c0.98,1.12 2.4,2 4,2s3.02,-0.88 4,-2c0.98,1.12 2.4,2 4,2h0.05l1.9,-6.68c0.11,-0.37 0.04,-1.06 -0.66,-1.28L20,10.62V6c0,-1.1 -0.9,-2 -2,-2h-3V1H9v3H6C4.9,4 4,4.9 4,6v4.62l-1.29,0.42c-0.63,0.19 -0.81,0.84 -0.66,1.28L3.95,19zM6,6h12v3.97L12,8L6,9.97V6z"/>
</vector>
14 changes: 13 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:platform="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -11,7 +13,12 @@
<com.dpt.inclineviews.InclinePlatformView
android:id="@+id/incline_platform1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
platform:angle="2.1"
platform:gradient_mode="side"
platform:icon="@drawable/baseline_boat_24"
platform:inverted="true"
platform:scale_mode="linear" />

<com.dpt.inclineviews.InclinePlatformView
android:id="@+id/incline_platform2"
Expand All @@ -23,4 +30,9 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<com.dpt.inclineviews.InclinePlatformView
android:id="@+id/incline_platform4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.TargetingViews" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<style name="Theme.TargetingViews" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.TargetingViews" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<style name="Theme.TargetingViews" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
Expand Down
2 changes: 1 addition & 1 deletion targetingviews/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ afterEvaluate {

groupId = 'com.github.chapaevanton'
artifactId = 'targetingviews'
version = 'v0.6.1-alpha'
version = 'v0.6.3-alpha'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import kotlin.math.*

class InclinePlatformView @JvmOverloads constructor(
context: Context,
attributeSet: AttributeSet? = null
) : View(context, attributeSet) {
attrs: AttributeSet? = null
) : View(context, attrs) {

private val pxViewSize = 200
private val pxStrokeWidth = 3F
Expand Down Expand Up @@ -66,12 +66,46 @@ class InclinePlatformView @JvmOverloads constructor(
private var iconRes: Int = R.drawable.base_icon
private var gradientMode = GradientMode.FRONT
private var scaleMode = ScaleMode.NONE
private lateinit var iconBitmap: Bitmap
private var iconBitmap: Bitmap? = null
private var isInverted = false
private var isNightMode = false
private var degreeText = "0,0º"
private var degree = 0F

init {
if (attrs != null) {
val attrArray = context.obtainStyledAttributes(attrs, R.styleable.InclinePlatformView)
try {
val gradientMode = attrArray.getInt(
R.styleable.InclinePlatformView_gradient_mode,
GradientMode.FRONT.ordinal
).let { GradientMode.values()[it] }

val scaleMode = attrArray.getInt(
R.styleable.InclinePlatformView_scale_mode,
ScaleMode.NONE.ordinal
).let { ScaleMode.values()[it] }

val inverted = attrArray.getBoolean(R.styleable.InclinePlatformView_inverted, false)

val iconRes = attrArray.getResourceId(
R.styleable.InclinePlatformView_icon,
R.drawable.base_icon
)

val degree = attrArray.getFloat(R.styleable.InclinePlatformView_angle, 0F).toDouble()

setGradientMode(gradientMode)
setScaleMode(scaleMode)
isInverted(inverted)
setIcon(iconRes)
setAngle(degree)
} finally {
attrArray.recycle()
}
}
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val widthExpect = paddingLeft + paddingRight + pxViewSize
val heightExpect = paddingTop + paddingBottom + pxViewSize
Expand All @@ -95,10 +129,7 @@ class InclinePlatformView @JvmOverloads constructor(

pxCanvasRadius = pxCanvasCenter - strokePaint.strokeWidth

centerPoint.set(
pxCanvasCenter + pxCanvasPadding,
pxCanvasCenter + pxCanvasPadding
)
centerPoint.set(pxCanvasCenter + pxCanvasPadding, pxCanvasCenter + pxCanvasPadding)
inclineRect.set(
pxCanvasPadding + strokePaint.strokeWidth,
pxCanvasPadding + strokePaint.strokeWidth,
Expand Down Expand Up @@ -128,17 +159,13 @@ class InclinePlatformView @JvmOverloads constructor(
pxCanvasHeight - pxCanvasRadius * 0.5F - pxCanvasPadding * 0.5F
)

iconBitmap = createBitmap(
iconRes,
pxBitmapSize,
pxBitmapSize,
if (isNightMode) Color.GRAY else Color.BLACK
)
bitmapMatrix.setTranslate(
pxCanvasCenter - iconBitmap.width * 0.5F + pxCanvasPadding,
pxCanvasCenter - iconBitmap.height + pxCanvasPadding
)

iconBitmap = createBitmap()
iconBitmap?.let {
bitmapMatrix.setTranslate(
pxCanvasCenter - it.width * 0.5F + pxCanvasPadding,
pxCanvasCenter - it.height + pxCanvasPadding
)
}
setMeasuredDimension(pxCanvasWidth, pxCanvasHeight)
}

Expand All @@ -153,7 +180,7 @@ class InclinePlatformView @JvmOverloads constructor(
}
}

fun setIcon(iconRes: Int) = apply {
fun setIcon(@DrawableRes iconRes: Int) = apply {
this.iconRes = iconRes
}

Expand Down Expand Up @@ -199,50 +226,22 @@ class InclinePlatformView @JvmOverloads constructor(
}

private fun Canvas.drawBackground() {
drawCircle(
centerPoint.x,
centerPoint.y,
pxCanvasRadius,
backgroundPaint
)
drawArc(
inclineRect,
degree,
180F,
true,
inclineBackgroundPaint
)
drawCircle(centerPoint.x, centerPoint.y, pxCanvasRadius, backgroundPaint)
drawArc(inclineRect, degree, 180F, true, inclineBackgroundPaint)
}

private fun Canvas.drawStroke() {

fun Canvas.drawSerif() {
repeat(4) {
drawLine(
serifRect.left,
serifRect.top,
serifRect.right,
serifRect.bottom,
serifPaint
)
drawLine(serifRect.left, serifRect.top, serifRect.right, serifRect.bottom, serifPaint)
rotate(5F, centerPoint.x, centerPoint.y)
}
}

drawLine(
lineLeftRect.left,
lineLeftRect.top,
lineLeftRect.right,
lineLeftRect.bottom,
centerSerifPaint
)
drawLine(
lineRightRect.left,
lineRightRect.top,
lineRightRect.right,
lineRightRect.bottom,
centerSerifPaint
)
drawLine(lineLeftRect.left, lineLeftRect.top, lineLeftRect.right, lineLeftRect.bottom, centerSerifPaint)
drawLine(lineRightRect.left, lineRightRect.top, lineRightRect.right, lineRightRect.bottom, centerSerifPaint)

rotate(-20F, centerPoint.x, centerPoint.y)
drawSerif()
rotate(5F, centerPoint.x, centerPoint.y)
Expand All @@ -252,27 +251,16 @@ class InclinePlatformView @JvmOverloads constructor(
rotate(5F, centerPoint.x, centerPoint.y)
drawSerif()
rotate(155F, centerPoint.x, centerPoint.y)
drawCircle(
centerPoint.x,
centerPoint.y,
pxCanvasRadius,
strokePaint
)
drawCircle(centerPoint.x, centerPoint.y, pxCanvasRadius, strokePaint)
}

private fun Canvas.drawData() {
drawText(
degreeText,
textPoint.x,
textPoint.y,
textPaint
)
drawText(degreeText, textPoint.x, textPoint.y, textPaint)

rotate(degree, centerPoint.x, centerPoint.y)
drawBitmap(
iconBitmap,
bitmapMatrix,
bitmapPaint
)
iconBitmap?.let {
drawBitmap(it, bitmapMatrix, bitmapPaint)
}
}

private fun createPaint(colorString: String? = null): Paint =
Expand All @@ -282,19 +270,12 @@ class InclinePlatformView @JvmOverloads constructor(
isFilterBitmap = true
}

private fun createBitmap(
intRes: Int?,
width: Int,
height: Int,
@ColorInt
color: Int? = null
): Bitmap {
return intRes?.let {
val drawable = ContextCompat.getDrawable(context, intRes)?.apply {
setTint(color ?: Color.BLACK)
}
drawable?.toBitmap(width, height)
} ?: Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
private fun createBitmap(@ColorInt color: Int? = null): Bitmap? {
if (pxBitmapSize == 0) return null
val drawable = ContextCompat.getDrawable(context, iconRes)?.apply {
setTint(color ?: Color.BLACK)
}
return drawable?.toBitmap(pxBitmapSize, pxBitmapSize)
}

@ColorInt
Expand Down
28 changes: 28 additions & 0 deletions targetingviews/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="gradient_mode" format="enum">
<enum name="front" value="0" />
<enum name="side" value="1" />
</attr>

<attr name="scale_mode" format="enum">
<enum name="none" value="0" />
<enum name="linear" value="1" />
<enum name="logarithmic" value="2" />
</attr>

<attr name="inverted" format="boolean" />

<attr name="icon" format="reference" />

<attr name="angle" format="float" />

<declare-styleable name="InclinePlatformView">
<attr name="gradient_mode" />
<attr name="scale_mode" />
<attr name="inverted" />
<attr name="icon" />
<attr name="angle" />
</declare-styleable>

</resources>

0 comments on commit 3843eeb

Please sign in to comment.