Skip to content

Commit 89e2562

Browse files
committed
first commit
0 parents  commit 89e2562

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1145
-0
lines changed

.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/libraries
5+
/.idea/modules.xml
6+
/.idea/workspace.xml
7+
.DS_Store
8+
/build
9+
/captures
10+
.externalNativeBuild

.idea/caches/build_file_checksums.ser

593 Bytes
Binary file not shown.

app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
apply plugin: 'com.android.application'
2+
3+
apply plugin: 'kotlin-android'
4+
5+
apply plugin: 'kotlin-android-extensions'
6+
7+
android {
8+
compileSdkVersion 28
9+
defaultConfig {
10+
applicationId "com.iyao.sample"
11+
minSdkVersion 14
12+
targetSdkVersion 28
13+
versionCode 1
14+
versionName "1.0"
15+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16+
}
17+
buildTypes {
18+
release {
19+
minifyEnabled false
20+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21+
}
22+
}
23+
}
24+
25+
dependencies {
26+
implementation fileTree(include: ['*.jar'], dir: 'libs')
27+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
28+
implementation 'com.android.support:appcompat-v7:28.0.0'
29+
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
30+
testImplementation 'junit:junit:4.12'
31+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
32+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
33+
implementation project(':easyat')
34+
}

app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile

app/src/main/AndroidManifest.xml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.iyao.sample">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.iyao.sample
2+
3+
import android.os.Bundle
4+
import android.support.v7.app.AppCompatActivity
5+
import android.text.SpannableStringBuilder
6+
import android.text.method.ScrollingMovementMethod
7+
import com.iyao.sample.method.*
8+
import kotlinx.android.synthetic.main.activity_main.*
9+
import java.util.*
10+
11+
class MainActivity : AppCompatActivity() {
12+
13+
private val methods = arrayOf(QQ, WeChat, Weibo)
14+
private var iterator: Iterator<Method> = methods.iterator()
15+
private val methodContext = MethodContext()
16+
private val users = arrayListOf(
17+
User("1", "激浊扬清"),
18+
User("2", "清风引佩下瑶台"),
19+
User("3", "浊泾清渭"),
20+
User("4", "刀光掩映孔雀屏"),
21+
User("5", "清风徐来"),
22+
User("6", "英雄无双风流婿"),
23+
User("7", "源清流洁"),
24+
User("8", "占断人间天上福"),
25+
User("9", "清音幽韵"),
26+
User("10", "碧箫声里双鸣凤"),
27+
User("11", "风清弊绝"),
28+
User("12", "天教艳质为眷属"),
29+
User("13", "独清独醒"),
30+
User("14", "千金一刻庆良宵"))
31+
override fun onCreate(savedInstanceState: Bundle?) {
32+
super.onCreate(savedInstanceState)
33+
setContentView(R.layout.activity_main)
34+
btnAppend.setOnClickListener {
35+
if (methodContext.method == null) {
36+
switch()
37+
}
38+
val user = users[Random().nextInt(14)].copy()
39+
(normalEdit.text as SpannableStringBuilder)
40+
.append(methodContext.newSpannable(user))
41+
.append(" ")
42+
}
43+
btnSwitch.setOnClickListener {
44+
switch()
45+
}
46+
btnPrint.setOnClickListener { _ ->
47+
val editable = normalEdit.text
48+
txtLogs.text = editable.getSpans(0, normalEdit.length(), User::class.java).joinToString("\n") {
49+
"$it, ${editable.getSpanStart(it)}, ${editable.getSpanEnd(it)}"
50+
}
51+
}
52+
txtLogs.movementMethod = ScrollingMovementMethod.getInstance()
53+
}
54+
55+
private fun switch() {
56+
val method = circularMethod()
57+
methodContext.method = method
58+
btnSwitch.text = method.javaClass.simpleName
59+
methodContext.init(normalEdit)
60+
}
61+
62+
private tailrec fun circularMethod(): Method {
63+
return if (iterator.hasNext()) {
64+
iterator.next()
65+
} else {
66+
iterator = methods.iterator()
67+
circularMethod()
68+
}
69+
}
70+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.iyao.sample
2+
3+
import android.graphics.Color
4+
import android.text.Spannable
5+
import android.text.SpannableString
6+
import android.text.style.ForegroundColorSpan
7+
import com.iyao.eastat.span.DataBindingSpan
8+
import com.iyao.eastat.span.DirtySpan
9+
10+
data class User(val id: String, var name: String): DataBindingSpan,
11+
DirtySpan {
12+
13+
fun getSpannedName(): Spannable {
14+
return SpannableString("@$name").apply {
15+
setSpan(ForegroundColorSpan(Color.CYAN), 0, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
16+
}
17+
}
18+
19+
override fun isDirty(text: Spannable): Boolean {
20+
val spanStart = text.getSpanStart(this)
21+
val spanEnd = text.getSpanEnd(this)
22+
return spanStart >= 0 && spanEnd >= 0 && text.substring(spanStart, spanEnd) != name
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.iyao.sample.method
2+
3+
import android.text.Spannable
4+
import android.widget.EditText
5+
import com.iyao.sample.User
6+
7+
interface Method {
8+
9+
fun init(editText: EditText)
10+
fun newSpannable(user: User): Spannable
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.iyao.sample.method
2+
3+
import android.text.Spannable
4+
import android.widget.EditText
5+
import com.iyao.sample.User
6+
7+
class MethodContext: Method {
8+
var method: Method? = null
9+
override fun init(editText: EditText) {
10+
method?.init(editText)
11+
}
12+
13+
override fun newSpannable(user: User): Spannable {
14+
return method?.newSpannable(user) ?: throw NullPointerException("method: null")
15+
}
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.iyao.sample.method
2+
3+
import android.graphics.*
4+
import android.graphics.drawable.BitmapDrawable
5+
import android.graphics.drawable.ColorDrawable
6+
import android.graphics.drawable.Drawable
7+
import android.os.Build
8+
import android.text.*
9+
import android.text.style.ImageSpan
10+
import android.widget.EditText
11+
import com.iyao.eastat.SpanFactory
12+
import com.iyao.sample.User
13+
import kotlin.math.min
14+
15+
16+
object QQ: Method {
17+
18+
19+
20+
private lateinit var layout: DynamicLayout
21+
private val highLight = Path()
22+
private val highLightPaint = Paint(Paint.ANTI_ALIAS_FLAG)
23+
private var width = 0
24+
private lateinit var textPaint: TextPaint
25+
override fun init(editText: EditText) {
26+
editText.text = null
27+
val eLayout = editText.layout
28+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
29+
highLightPaint.color = editText.highlightColor
30+
} else {
31+
highLightPaint.color = Color.YELLOW
32+
}
33+
highLightPaint.style = Paint.Style.FILL
34+
textPaint = eLayout.paint
35+
width = eLayout.width
36+
layout = DynamicLayout(SpannableStringBuilder(), eLayout.paint, eLayout.width, eLayout.alignment, eLayout.spacingMultiplier, eLayout.spacingAdd, true)
37+
editText.setOnKeyListener(null)
38+
editText.setEditableFactory(Editable.Factory.getInstance())
39+
}
40+
41+
override fun newSpannable(user: User): Spannable {
42+
return SpanFactory.newSpannable("@${user.name}", user, ImageSpan(createDrawable(user.getSpannedName())))
43+
}
44+
45+
@Suppress("DEPRECATION")
46+
private fun createDrawable(source: CharSequence): Drawable {
47+
return if (::layout.isInitialized) {
48+
val builder = layout.text as SpannableStringBuilder
49+
builder.clear()
50+
builder.append(source)
51+
val want = textPaint.measureText(builder, 0, builder.length).toInt()
52+
if (want != layout.width) {
53+
layout = DynamicLayout(builder, layout.paint, min(want, width), layout.alignment, layout.spacingMultiplier, layout.spacingAdd, true)
54+
}
55+
val bitmap = Bitmap.createBitmap(layout.width, layout.height, Bitmap.Config.ARGB_8888)
56+
val rect = Rect(0, 0, bitmap.width, bitmap.height)
57+
highLight.reset()
58+
highLight.addRect(RectF(rect), Path.Direction.CCW)
59+
layout.draw(Canvas(bitmap), highLight, highLightPaint, 0)
60+
BitmapDrawable(bitmap).apply {
61+
bounds = rect
62+
}
63+
} else throw IllegalAccessException()
64+
}
65+
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.iyao.sample.method
2+
3+
import android.text.Spannable
4+
import android.view.KeyEvent
5+
import android.widget.EditText
6+
import com.iyao.eastat.KeyCodeDeleteHelper
7+
import com.iyao.eastat.NoCopySpanEditableFactory
8+
import com.iyao.eastat.SpanFactory
9+
import com.iyao.sample.User
10+
import com.iyao.eastat.span.DirtySpan
11+
import com.iyao.eastat.watcher.DirtySpanWatcher
12+
13+
object WeChat: Method {
14+
override fun init(editText: EditText) {
15+
editText.text = null
16+
editText.setEditableFactory(NoCopySpanEditableFactory(
17+
DirtySpanWatcher { it is DirtySpan }))
18+
editText.setOnKeyListener { v, keyCode, event ->
19+
if (keyCode == KeyEvent.KEYCODE_DEL && event.action == KeyEvent.ACTION_DOWN) {
20+
KeyCodeDeleteHelper.onDelDown((v as EditText).text)
21+
}
22+
return@setOnKeyListener false
23+
}
24+
}
25+
26+
override fun newSpannable(user: User): Spannable {
27+
return SpanFactory.newSpannable("@${user.name}", user)
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.iyao.sample.method
2+
3+
import android.text.Spannable
4+
import android.view.KeyEvent
5+
import android.widget.EditText
6+
import com.iyao.eastat.KeyCodeDeleteHelper
7+
import com.iyao.eastat.NoCopySpanEditableFactory
8+
import com.iyao.eastat.SpanFactory
9+
import com.iyao.sample.User
10+
import com.iyao.eastat.span.DataBindingSpan
11+
import com.iyao.eastat.watcher.SelectionSpanWatcher
12+
13+
object Weibo: Method {
14+
override fun init(editText: EditText) {
15+
editText.text = null
16+
editText.setEditableFactory(NoCopySpanEditableFactory(
17+
SelectionSpanWatcher(
18+
DataBindingSpan::class)))
19+
editText.setOnKeyListener { v, keyCode, event ->
20+
if (keyCode == KeyEvent.KEYCODE_DEL && event.action == KeyEvent.ACTION_DOWN) {
21+
return@setOnKeyListener KeyCodeDeleteHelper.onDelDown(
22+
(v as EditText).text)
23+
}
24+
return@setOnKeyListener false
25+
}
26+
}
27+
28+
override fun newSpannable(user: User): Spannable {
29+
return SpanFactory.newSpannable(user.getSpannedName(), user)
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:aapt="http://schemas.android.com/aapt"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportHeight="108"
6+
android:viewportWidth="108">
7+
<path
8+
android:fillType="evenOdd"
9+
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
10+
android:strokeColor="#00000000"
11+
android:strokeWidth="1">
12+
<aapt:attr name="android:fillColor">
13+
<gradient
14+
android:endX="78.5885"
15+
android:endY="90.9159"
16+
android:startX="48.7653"
17+
android:startY="61.0927"
18+
android:type="linear">
19+
<item
20+
android:color="#44000000"
21+
android:offset="0.0" />
22+
<item
23+
android:color="#00000000"
24+
android:offset="1.0" />
25+
</gradient>
26+
</aapt:attr>
27+
</path>
28+
<path
29+
android:fillColor="#FFFFFF"
30+
android:fillType="nonZero"
31+
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
32+
android:strokeColor="#00000000"
33+
android:strokeWidth="1" />
34+
</vector>

0 commit comments

Comments
 (0)