|
| 1 | +package com.cyl.classifymenu.example.menu |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.graphics.Bitmap |
| 5 | +import android.graphics.Canvas |
| 6 | +import android.graphics.Color |
| 7 | +import android.graphics.LinearGradient |
| 8 | +import android.graphics.Paint |
| 9 | +import android.graphics.Shader |
| 10 | +import android.graphics.drawable.BitmapDrawable |
| 11 | +import android.graphics.drawable.Drawable |
| 12 | +import android.net.Uri |
| 13 | +import android.util.TypedValue |
| 14 | +import android.view.View |
| 15 | +import android.widget.LinearLayout |
| 16 | +import android.widget.ScrollView |
| 17 | +import android.widget.TextView |
| 18 | +import com.cyl.classifymenu.CYLEdgeInsets |
| 19 | +import com.cyl.classifymenu.CYLIndexPath |
| 20 | +import com.cyl.classifymenu.CYLMenuDelegate |
| 21 | +import com.cyl.classifymenu.CYLMenuFoldControl |
| 22 | +import com.cyl.classifymenu.CYLMenuFoldControlContent |
| 23 | +import com.cyl.classifymenu.CYLMenuFoldControlPosition |
| 24 | +import com.cyl.classifymenu.CYLMenuSelectionMode |
| 25 | +import com.cyl.classifymenu.CYLMenuStyleHighlightedBackgroundColor |
| 26 | +import com.cyl.classifymenu.CYLMenuStyleImageSize |
| 27 | +import com.cyl.classifymenu.CYLMenuStyleImageTitleSpacing |
| 28 | +import com.cyl.classifymenu.CYLMenuStyleInteritemSpacing |
| 29 | +import com.cyl.classifymenu.CYLMenuStyleItemHeight |
| 30 | +import com.cyl.classifymenu.CYLMenuStyleLayoutCentered |
| 31 | +import com.cyl.classifymenu.CYLMenuStyleLineSpacing |
| 32 | +import com.cyl.classifymenu.CYLMenuStyleNormal |
| 33 | +import com.cyl.classifymenu.CYLMenuStyleSelected |
| 34 | +import com.cyl.classifymenu.CYLMenuStyleTagContentInsets |
| 35 | +import com.cyl.classifymenu.CYLSize |
| 36 | +import com.cyl.classifymenu.CYLTag |
| 37 | +import com.cyl.classifymenu.CYLTagAppearanceBackgroundColor |
| 38 | +import com.cyl.classifymenu.CYLTagAppearanceBorderColor |
| 39 | +import com.cyl.classifymenu.CYLTagAppearanceBorderWidth |
| 40 | +import com.cyl.classifymenu.CYLTagAppearanceCornerRadius |
| 41 | +import com.cyl.classifymenu.CYLTagAppearanceIconColor |
| 42 | +import com.cyl.classifymenu.CYLTagAppearanceTextColor |
| 43 | +import com.cyl.classifymenu.CYLTagButton |
| 44 | +import com.cyl.classifymenu.CYLTagItemAnimation |
| 45 | +import com.cyl.classifymenu.CYLTagItemDeletable |
| 46 | +import com.cyl.classifymenu.CYLTagItemIdentifier |
| 47 | +import com.cyl.classifymenu.CYLTagItemImage |
| 48 | +import com.cyl.classifymenu.CYLTagItemSelectedImage |
| 49 | +import com.cyl.classifymenu.CYLTagItemStyle |
| 50 | +import com.cyl.classifymenu.CYLTagItemTitle |
| 51 | +import com.cyl.classifymenu.CYLTagListView |
| 52 | +import com.cyl.classifymenu.CYLTagListViewFoldControl |
| 53 | +import com.cyl.classifymenu.CYLTagListViewScrollEnabled |
| 54 | +import com.cyl.classifymenu.CYLTagListViewSections |
| 55 | +import com.cyl.classifymenu.CYLTagListViewSelectedItems |
| 56 | +import com.cyl.classifymenu.CYLTagListViewSelectionMode |
| 57 | +import com.cyl.classifymenu.CYLTagListViewStyle |
| 58 | +import com.cyl.classifymenu.CYLTagListViewTags |
| 59 | +import com.cyl.classifymenu.CYLTagSectionItems |
| 60 | +import com.cyl.classifymenu.CYLTagSectionTitle |
| 61 | +import com.cyl.classifymenu.tagListViewWithConfiguration |
| 62 | +import com.cyl.classifymenu.tagListViewWithConfigurationFile |
| 63 | + |
| 64 | +/** |
| 65 | + * A footnote caption of these screens (16 dp insets). |
| 66 | + */ |
| 67 | +private fun caption(context: Context, text: String) = TextView(context).apply { |
| 68 | + val d = context.resources.displayMetrics.density |
| 69 | + this.text = text |
| 70 | + setTextSize(TypedValue.COMPLEX_UNIT_SP, 13f) |
| 71 | + setTextColor(Color.argb(153, 60, 60, 67)) |
| 72 | + setPadding((16 * d).toInt(), (8 * d).toInt(), (16 * d).toInt(), 0) |
| 73 | +} |
| 74 | + |
| 75 | +private fun column(context: Context, views: List<View>): View = ScrollView(context).apply { |
| 76 | + setBackgroundColor(Color.rgb(242, 242, 247)) |
| 77 | + addView(LinearLayout(context).apply { |
| 78 | + orientation = LinearLayout.VERTICAL |
| 79 | + setPadding(0, (12 * context.resources.displayMetrics.density).toInt(), 0, 0) |
| 80 | + views.forEach { addView(it, LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)) } |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * `CYLTagCloudDemoViewController`: android-tagview's way of making a tag cloud, twice: in code (`addTagsWithTitles`, |
| 86 | + * `addTag`, `addTags`, click and delete handlers) and from a dictionary (`CYLTagListViewTags`). |
| 87 | + */ |
| 88 | +class CYLTagCloudScreen(private val context: Context) : CYLMenuScreen { |
| 89 | + override val title: CharSequence = CYLExampleLocalizedString(context, "标签云(android-tagview)") |
| 90 | + val codeTagListView = CYLTagListView(context) |
| 91 | + val mapTagListView: CYLTagListView = CYLTagListView.tagListViewWithConfiguration(context, mapConfiguration()) |
| 92 | + val eventLabel = caption(context, CYLExampleLocalizedString(context, "点击标签,或点 × 删除")) |
| 93 | + override val view: View |
| 94 | + |
| 95 | + companion object { |
| 96 | + val continents = listOf("Asia", "Africa", "North America", "South America", "Antarctica", "Europe", "Australia") |
| 97 | + |
| 98 | + fun detailedTag() = CYLTag(7, "Tag Text", "#DDDDDD").apply { |
| 99 | + textColor = Color.WHITE |
| 100 | + highlightedBackgroundColor = Color.rgb(84, 84, 84) |
| 101 | + cornerRadius = 20f |
| 102 | + textSize = 14f |
| 103 | + borderWidth = 1f |
| 104 | + borderColor = Color.WHITE |
| 105 | + isDeletable = true |
| 106 | + } |
| 107 | + |
| 108 | + fun styledTags(context: Context): List<CYLTag> { |
| 109 | + val blue = CYLTag(CYLExampleLocalizedString(context, "可删除"), "#00BFFF").apply { |
| 110 | + textColor = Color.WHITE |
| 111 | + borderWidth = 0f |
| 112 | + isDeletable = true |
| 113 | + highlightedBackgroundColor = Color.rgb(0, 128, 179) |
| 114 | + } |
| 115 | + val large = CYLTag(CYLExampleLocalizedString(context, "大字号"), Color.rgb(255, 149, 0)).apply { |
| 116 | + textColor = Color.WHITE |
| 117 | + textSize = 22f |
| 118 | + borderWidth = 0f |
| 119 | + } |
| 120 | + val image = CYLTag(CYLExampleLocalizedString(context, "背景图")).apply { |
| 121 | + backgroundImage = backgroundImage(context) |
| 122 | + textColor = Color.WHITE |
| 123 | + borderWidth = 0f |
| 124 | + } |
| 125 | + val custom = CYLTag(CYLExampleLocalizedString(context, "自定义删除"), Color.rgb(255, 45, 85)).apply { |
| 126 | + textColor = Color.WHITE |
| 127 | + borderWidth = 0f |
| 128 | + isDeletable = true |
| 129 | + deleteIndicatorTitle = "⊗" |
| 130 | + deleteIndicatorColor = Color.argb(204, 255, 255, 255) |
| 131 | + deleteIndicatorSize = 18f |
| 132 | + } |
| 133 | + return listOf(blue, large, image, custom) |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * A purple-to-teal gradient, 64 × 32 (as the iOS demo draws it). |
| 138 | + */ |
| 139 | + fun backgroundImage(context: Context): Drawable { |
| 140 | + val d = context.resources.displayMetrics.density |
| 141 | + val bitmap = Bitmap.createBitmap((64 * d).toInt(), (32 * d).toInt(), Bitmap.Config.ARGB_8888) |
| 142 | + Canvas(bitmap).drawRect(0f, 0f, bitmap.width.toFloat(), bitmap.height.toFloat(), Paint().apply { |
| 143 | + shader = LinearGradient(0f, 0f, bitmap.width.toFloat(), 0f, Color.rgb(175, 82, 222), Color.rgb(48, 176, 199), Shader.TileMode.CLAMP) |
| 144 | + }) |
| 145 | + return BitmapDrawable(context.resources, bitmap) |
| 146 | + } |
| 147 | + |
| 148 | + fun mapConfiguration(): Map<String, Any?> { |
| 149 | + val blue = mapOf(CYLMenuStyleNormal to mapOf(CYLTagAppearanceBackgroundColor to "#00BFFF", CYLTagAppearanceTextColor to "#FFFFFF", |
| 150 | + CYLTagAppearanceBorderWidth to 0, CYLTagAppearanceCornerRadius to 0), CYLMenuStyleHighlightedBackgroundColor to "#88363636") |
| 151 | + return mapOf( |
| 152 | + CYLTagListViewStyle to mapOf(CYLMenuStyleLineSpacing to 5, CYLMenuStyleInteritemSpacing to 3, CYLMenuStyleTagContentInsets to "{3, 10, 3, 10}"), |
| 153 | + CYLTagListViewScrollEnabled to false, |
| 154 | + CYLTagListViewTags to listOf("Asia", "Europe", |
| 155 | + mapOf(CYLTagItemTitle to "Africa", CYLTagItemIdentifier to 3, CYLTagItemDeletable to true, CYLTagItemStyle to blue), |
| 156 | + mapOf(CYLTagItemTitle to "Australia", CYLTagItemImage to "system:globe")), |
| 157 | + ) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + init { |
| 162 | + codeTagListView.lineSpacing = 5f |
| 163 | + codeTagListView.interitemSpacing = 3f |
| 164 | + codeTagListView.tagContentInsets = CYLEdgeInsets(3f, 10f, 3f, 10f) |
| 165 | + codeTagListView.isScrollEnabled = false |
| 166 | + codeTagListView.addTagsWithTitles(continents) |
| 167 | + codeTagListView.addTag(detailedTag()) |
| 168 | + codeTagListView.addTags(styledTags(context)) |
| 169 | + codeTagListView.tagClickHandler = { index, tag -> showEvent(CYLExampleLocalizedString(context, "点击第 %ld 个「%@」", index, tag.title)) } |
| 170 | + codeTagListView.tagDeleteHandler = { index, tag -> showEvent(CYLExampleLocalizedString(context, "删除第 %ld 个「%@」", index, tag.title)) } |
| 171 | + mapTagListView.tagDeleteHandler = { index, tag -> showEvent(CYLExampleLocalizedString(context, "删除第 %ld 个「%@」", index, tag.title)) } |
| 172 | + view = column(context, listOf( |
| 173 | + caption(context, CYLExampleLocalizedString(context, "代码初始化:addTags(withTitles:)、addTag(_:)")), codeTagListView, |
| 174 | + caption(context, CYLExampleLocalizedString(context, "字典初始化:CYLTagListViewTags")), mapTagListView, eventLabel)) |
| 175 | + } |
| 176 | + |
| 177 | + fun showEvent(text: String) { |
| 178 | + eventLabel.text = text |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +/** |
| 183 | + * `CYLTagConfigurationDemoViewController`: one tag list from the configuration file |
| 184 | + * (`CYLMenuExample.bundle/Configurations/animated-tags.json`) and one from a dictionary (several, animated icons). |
| 185 | + */ |
| 186 | +class CYLTagConfigurationScreen(private val context: Context) : CYLMenuScreen, CYLMenuDelegate { |
| 187 | + override val title: CharSequence = CYLExampleLocalizedString(context, "配置初始化") |
| 188 | + var fileTagListView: CYLTagListView? = null |
| 189 | + private set |
| 190 | + var configurationError: Exception? = null |
| 191 | + private set |
| 192 | + val mapTagListView: CYLTagListView = CYLTagListView.tagListViewWithConfiguration(context, mapConfiguration(context)) |
| 193 | + val eventLabel = caption(context, CYLExampleLocalizedString(context, if (CYLTagButton.isAnimationEngineAvailable()) "动效引擎:CYLTabBarItemAnimationFactory 已链接" else "动效引擎未链接:显示静态图标")) |
| 194 | + override val view: View |
| 195 | + |
| 196 | + companion object { |
| 197 | + const val CONFIGURATION_FILE = "CYLMenuExample.bundle/Configurations/animated-tags.json" |
| 198 | + |
| 199 | + private fun animation(name: String) = Uri.parse("file:///android_asset/CYLMenuExample.bundle/Animations/$name") |
| 200 | + |
| 201 | + fun mapConfiguration(context: Context): Map<String, Any?> { |
| 202 | + val indigo = Color.rgb(88, 86, 214) |
| 203 | + val normal = mapOf(CYLTagAppearanceIconColor to indigo, CYLTagAppearanceBorderColor to indigo, CYLTagAppearanceBorderWidth to 1, CYLTagAppearanceCornerRadius to 8) |
| 204 | + val selected = mapOf(CYLTagAppearanceBackgroundColor to indigo, CYLTagAppearanceBorderColor to indigo, CYLTagAppearanceIconColor to Color.WHITE, CYLTagAppearanceCornerRadius to 8) |
| 205 | + val style = mapOf(CYLMenuStyleImageSize to CYLSize(20f, 20f), CYLMenuStyleImageTitleSpacing to 4, CYLMenuStyleItemHeight to 36, |
| 206 | + CYLMenuStyleLayoutCentered to true, CYLMenuStyleNormal to normal, CYLMenuStyleSelected to selected) |
| 207 | + val items = listOf( |
| 208 | + mapOf(CYLTagItemTitle to "Lottie", CYLTagItemAnimation to animation("Lottie/tab_me_animate.json")), |
| 209 | + mapOf(CYLTagItemTitle to "GIF", CYLTagItemAnimation to animation("GIF/cyl_tab_sample_animated.gif")), |
| 210 | + mapOf(CYLTagItemTitle to "Rive", CYLTagItemAnimation to animation("Rive/menu_button.riv")), |
| 211 | + mapOf(CYLTagItemImage to "system:heart", CYLTagItemSelectedImage to "system:heart.fill"), |
| 212 | + "Text only", |
| 213 | + ) |
| 214 | + return mapOf( |
| 215 | + CYLTagListViewSelectionMode to CYLMenuSelectionMode.CYLMenuSelectionModeMultiple, |
| 216 | + CYLTagListViewFoldControl to CYLMenuFoldControl.foldControlWithPosition(CYLMenuFoldControlPosition.CYLMenuFoldControlPositionNone, CYLMenuFoldControlContent.CYLMenuFoldControlContentIcon), |
| 217 | + CYLTagListViewScrollEnabled to false, |
| 218 | + CYLTagListViewSelectedItems to listOf(CYLIndexPath(1, 0)), |
| 219 | + CYLTagListViewStyle to style, |
| 220 | + CYLTagListViewSections to listOf(mapOf(CYLTagSectionTitle to CYLExampleLocalizedString(context, "字典(map)初始化"), CYLTagSectionItems to items)), |
| 221 | + ) |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + init { |
| 226 | + val views = ArrayList<View>() |
| 227 | + views.add(caption(context, CYLExampleLocalizedString(context, "配置文件初始化:%@", CONFIGURATION_FILE))) |
| 228 | + try { |
| 229 | + val list = CYLTagListView.tagListViewWithConfigurationFile(context, CONFIGURATION_FILE) |
| 230 | + list.menuDelegate = this |
| 231 | + fileTagListView = list |
| 232 | + views.add(list) |
| 233 | + } catch (error: Exception) { |
| 234 | + configurationError = error |
| 235 | + views.add(caption(context, error.localizedMessage ?: "?")) |
| 236 | + } |
| 237 | + mapTagListView.menuDelegate = this |
| 238 | + views.add(caption(context, CYLExampleLocalizedString(context, "字典(map)初始化:-initWithConfiguration:"))) |
| 239 | + views.add(mapTagListView) |
| 240 | + views.add(eventLabel) |
| 241 | + view = column(context, views) |
| 242 | + } |
| 243 | + |
| 244 | + override fun menuDidSelectItemAtIndexPath(menu: Any, indexPath: CYLIndexPath) { |
| 245 | + val list = menu as? CYLTagListView ?: return |
| 246 | + eventLabel.text = CYLExampleLocalizedString(context, "点击「%@」", list.menuDataSource?.menuTitleForItemAtIndexPath(indexPath).orEmpty()) |
| 247 | + } |
| 248 | +} |
0 commit comments