-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
456bb8d
commit ae37e5a
Showing
5 changed files
with
159 additions
and
12 deletions.
There are no files selected for viewing
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
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
75 changes: 75 additions & 0 deletions
75
Sample/src/main/kotlin/ru/santaev/outlinespan/MainActivity.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package ru.santaev.outlinespan | ||
|
||
import android.annotation.SuppressLint | ||
import android.databinding.DataBindingUtil | ||
import android.graphics.Color | ||
import android.os.Bundle | ||
import android.support.v7.app.AppCompatActivity | ||
import android.text.Spannable | ||
import android.text.SpannableString | ||
import santaev.ru.outlinespan.R | ||
import santaev.ru.outlinespan.databinding.ActivityMainBinding | ||
import java.util.* | ||
|
||
|
||
class MainActivity : AppCompatActivity() { | ||
|
||
private lateinit var binding: ActivityMainBinding | ||
|
||
@SuppressLint("SetTextI18n") | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
initUi() | ||
} | ||
|
||
@SuppressLint("SetTextI18n") | ||
private fun initUi() { | ||
binding = DataBindingUtil.setContentView(this, R.layout.activity_main) | ||
|
||
|
||
val outlineSpan = OutlineSpan( | ||
strokeColor = Color.RED, | ||
strokeWidth = 4F | ||
) | ||
val outlineSpanBlue = OutlineSpan( | ||
strokeColor = Color.BLUE, | ||
strokeWidth = 2F | ||
) | ||
val text = "Outlined text" | ||
val spannable = SpannableString(text) | ||
spannable.setSpan(outlineSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) | ||
spannable.setSpan(outlineSpanBlue, 9, text.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) | ||
|
||
binding.simpleText.text = "Simple text" | ||
binding.outlinedText.text = spannable | ||
|
||
updateText() | ||
|
||
binding.root.setOnClickListener { updateText() } | ||
} | ||
|
||
|
||
private fun updateText() { | ||
val outlineSpan = OutlineSpan( | ||
strokeColor = Color.RED, | ||
strokeWidth = 2F | ||
) | ||
val text = getString(R.string.text) | ||
val spannable = SpannableString(text) | ||
val words = text.split(' ') | ||
val idx = Random().nextInt(words.size) | ||
val charIdx = words | ||
.asSequence() | ||
.take(idx) | ||
.sumBy { it.length + 1 } | ||
spannable.setSpan( | ||
outlineSpan, | ||
charIdx, | ||
charIdx + words[idx].length, | ||
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE | ||
) | ||
|
||
binding.outlinedText2.text = spannable | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,64 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
<layout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".MainActivity"> | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
|
||
</android.support.constraint.ConstraintLayout> | ||
<FrameLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical" | ||
tools:context="ru.santaev.outlinespan.MainActivity"> | ||
|
||
<LinearLayout | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center" | ||
android:orientation="vertical" | ||
tools:ignore="UselessParent"> | ||
|
||
<android.support.v7.widget.AppCompatTextView | ||
android:id="@+id/simpleText" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center" | ||
android:textSize="48sp" | ||
app:layout_constraintBottom_toTopOf="@+id/outlinedText2" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintHorizontal_bias="0.5" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
tools:text="Simple text" /> | ||
|
||
<android.support.v7.widget.AppCompatTextView | ||
android:id="@+id/outlinedText" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center" | ||
android:textSize="48sp" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintHorizontal_bias="0.5" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/simpleText" | ||
android:textColor="@android:color/white" | ||
tools:text="Outlined text" /> | ||
|
||
<android.support.v7.widget.AppCompatTextView | ||
android:id="@+id/outlinedText2" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center" | ||
android:layout_marginLeft="16dp" | ||
android:layout_marginRight="16dp" | ||
android:textSize="16sp" | ||
app:layout_constraintBottom_toTopOf="@+id/outlinedText" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintHorizontal_bias="0.5" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/simpleText" | ||
tools:text="Outlined text" /> | ||
|
||
</LinearLayout> | ||
|
||
</FrameLayout> | ||
</layout> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
<resources> | ||
<resources xmlns:tools="http://schemas.android.com/tools"> | ||
<string name="app_name">OutlineSpan</string> | ||
<string name="text" tools:ignore="Typos">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam lectus massa, fermentum at ante quis, finibus bibendum augue. Ut hendrerit porttitor libero vel tincidunt. Ut cursus quam et dui sollicitudin faucibus consectetur non libero. Praesent vel ipsum feugiat, accumsan risus non, pellentesque mauris. Vivamus ut nibh ac arcu blandit auctor finibus eget urna. Phasellus quis metus at purus ultrices varius. Pellentesque ac lacus a ligula ornare ultrices. Sed eu auctor eros. Etiam lobortis ut nibh et auctor. Praesent vitae nibh nibh. Cras ultrices nisi vel ante tincidunt egestas.</string> | ||
</resources> |