Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demonstrating how to tile a (Vector) Drawable
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and limitations under
the License.
-->
<ImageView
android:id="@+id/image"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
tools:context="com.example.tiledrawable.MainActivity" />
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.example.tiledrawable
import android.graphics.Shader
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.view.View
import android.widget.ImageView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val iv = findViewById<ImageView>(R.id.image)
val d = ContextCompat.getDrawable(this, R.drawable.pattern)
iv.setImageDrawable(TileDrawable(d, Shader.TileMode.REPEAT))
iv.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR or
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
}
}
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and limitations under
the License.
-->
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="94dp"
android:height="59dp"
android:viewportWidth="946"
android:viewportHeight="589">
<path
android:pathData="M 0 0 L 946 0 L 946 589 L 0 589 Z"
android:fillColor="#393939"/>
<path
android:pathData="M 106.555 106.429 L 99.144 131.937 C 96.651 140.518 101.123 145.089 109.748 142.778 L 137.299 135.396 C 145.925 133.084 147.54 126.846 141.118 120.623 L 122.194 102.287 C 115.74 96.033 109.062 97.801 106.555 106.429 Z M 102.714 105.313 C 106.062 93.79 116.359 91.063 124.978 99.414 L 143.902 117.75 C 152.555 126.134 149.963 136.143 138.334 139.259 L 110.784 146.642 C 99.147 149.76 91.941 142.394 95.303 130.821 L 102.714 105.313 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 302.454 208.945 L 279.177 221.741 C 271.346 226.046 271.276 232.441 279.009 236.905 L 303.71 251.166 C 311.445 255.632 316.998 252.362 316.857 243.421 L 316.441 217.074 C 316.299 208.088 310.327 204.616 302.454 208.945 Z M 300.527 205.439 C 311.043 199.659 320.251 205.012 320.44 217.011 L 320.856 243.358 C 321.047 255.405 312.137 260.65 301.71 254.631 L 277.009 240.369 C 266.576 234.346 266.689 224.042 277.25 218.236 L 300.527 205.439 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 829.454 478.945 L 806.177 491.741 C 798.346 496.046 798.276 502.441 806.009 506.905 L 830.71 521.166 C 838.445 525.632 843.998 522.362 843.857 513.421 L 843.441 487.074 C 843.299 478.088 837.327 474.616 829.454 478.945 Z M 827.527 475.439 C 838.043 469.659 847.251 475.012 847.44 487.011 L 847.856 513.358 C 848.047 525.405 839.137 530.65 828.71 524.631 L 804.009 510.369 C 793.576 504.346 793.689 494.042 804.25 488.236 L 827.527 475.439 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 290.562 50.939 L 279.039 57.029 C 275.786 58.748 275.745 60.885 278.926 62.721 L 291.832 70.172 C 295.015 72.01 296.873 70.899 296.755 67.227 L 296.339 54.285 C 296.218 50.525 293.895 49.177 290.562 50.939 Z M 288.693 47.403 C 294.64 44.26 300.121 47.44 300.337 54.157 L 300.753 67.098 C 300.972 73.907 295.73 77.042 289.832 73.637 L 276.926 66.185 C 271.019 62.775 271.137 56.681 277.17 53.493 L 288.693 47.403 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 264.039 501.029 C 260.786 502.748 260.745 504.885 263.926 506.721 L 276.832 514.172 C 280.015 516.01 281.873 514.899 281.755 511.227 L 281.339 498.286 C 281.218 494.525 278.895 493.177 275.562 494.939 L 264.039 501.029 Z M 285.337 498.157 L 285.753 511.098 C 285.972 517.907 280.73 521.042 274.832 517.637 L 261.926 510.185 C 256.019 506.775 256.137 500.681 262.17 497.493 L 273.693 491.403 C 279.64 488.26 285.121 491.44 285.337 498.157 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 575.617 326.701 C 569.078 324.756 565.725 328.021 567.493 334.619 L 573.517 357.099 C 575.288 363.708 579.853 364.878 584.578 359.938 L 599.1 344.756 C 603.87 339.77 602.528 334.709 595.918 332.742 L 575.617 326.701 Z M 601.991 347.521 L 587.469 362.703 C 580.588 369.897 572.231 367.756 569.653 358.135 L 563.629 335.654 C 561.052 326.036 567.22 320.029 576.758 322.867 L 597.059 328.908 C 606.536 331.728 608.828 340.374 601.991 347.521 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 823.743 95.471 C 825.874 104.148 832.032 105.871 838.346 99.557 L 858.515 79.388 C 864.83 73.074 863.109 66.863 854.436 64.685 L 828.879 58.268 C 820.162 56.079 815.264 60.949 817.407 69.674 L 823.743 95.471 Z M 829.853 54.388 L 855.41 60.806 C 867.096 63.74 869.856 73.704 861.343 82.217 L 841.175 102.385 C 832.656 110.904 822.732 108.128 819.858 96.425 L 813.522 70.628 C 810.66 58.975 818.214 51.466 829.853 54.388 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 299 384 C 292.925 384 288 379.075 288 373 C 288 366.925 292.925 362 299 362 C 305.075 362 310 366.925 310 373 C 310 379.075 305.075 384 299 384 Z M 299 380 C 302.866 380 306 376.866 306 373 C 306 369.134 302.866 366 299 366 C 295.134 366 292 369.134 292 373 C 292 376.866 295.134 380 299 380 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 178.365 322.487 C 176.685 323.27 174.648 322.502 173.827 320.741 L 170.202 322.432 C 171.948 326.176 176.354 327.838 180.055 326.112 L 178.365 322.487 Z M 179.945 317.888 C 180.766 319.649 180.045 321.703 178.365 322.487 L 180.055 326.112 C 183.756 324.386 185.316 319.943 183.57 316.198 L 179.945 317.888 Z M 197.851 313.401 C 196.171 314.184 194.134 313.416 193.313 311.655 L 189.687 313.345 C 191.434 317.09 195.84 318.752 199.541 317.026 L 197.851 313.401 Z M 199.43 308.802 C 200.251 310.563 199.53 312.617 197.851 313.401 L 199.541 317.026 C 203.242 315.3 204.802 310.856 203.055 307.112 L 199.43 308.802 Z M 183.57 316.198 C 182.749 314.437 183.47 312.383 185.149 311.599 C 186.829 310.816 188.866 311.584 189.687 313.345 L 193.313 311.655 C 191.566 307.91 187.16 306.248 183.459 307.974 C 179.758 309.7 178.198 314.144 179.945 317.888 L 183.57 316.198 Z M 203.055 307.112 C 202.234 305.351 202.955 303.297 204.635 302.513 C 206.315 301.73 208.352 302.498 209.173 304.259 L 212.798 302.568 C 211.052 298.824 206.646 297.162 202.945 298.888 C 199.244 300.614 197.684 305.057 199.43 308.802 L 203.055 307.112 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 90.781 225.546 C 89.47 224.236 89.495 222.059 90.869 220.685 L 88.041 217.857 C 85.119 220.778 85.065 225.487 87.952 228.375 L 90.781 225.546 Z M 95.642 225.458 C 94.268 226.832 92.092 226.857 90.781 225.546 L 87.952 228.375 C 90.84 231.262 95.549 231.208 98.471 228.286 L 95.642 225.458 Z M 105.984 240.749 C 104.673 239.439 104.698 237.262 106.072 235.888 L 103.244 233.059 C 100.322 235.981 100.268 240.69 103.155 243.578 L 105.984 240.749 Z M 110.845 240.661 C 109.471 242.035 107.294 242.06 105.984 240.749 L 103.155 243.578 C 106.043 246.465 110.752 246.411 113.673 243.489 L 110.845 240.661 Z M 98.471 228.286 C 99.845 226.912 102.021 226.887 103.332 228.198 C 104.643 229.509 104.618 231.685 103.244 233.059 L 106.072 235.888 C 108.994 232.966 109.048 228.257 106.16 225.37 C 103.273 222.482 98.564 222.536 95.642 225.458 L 98.471 228.286 Z M 113.673 243.489 C 115.047 242.115 117.224 242.09 118.535 243.401 C 119.845 244.711 119.82 246.888 118.446 248.262 L 121.275 251.091 C 124.197 248.169 124.251 243.46 121.363 240.572 C 118.476 237.685 113.767 237.739 110.845 240.661 L 113.673 243.489 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 670.596 63.254 C 668.743 63.287 667.195 61.757 667.161 59.814 L 663.161 59.884 C 663.234 64.015 666.583 67.325 670.666 67.254 L 670.596 63.254 Z M 673.91 59.696 C 673.944 61.639 672.45 63.222 670.596 63.254 L 670.666 67.254 C 674.749 67.183 677.981 63.758 677.909 59.626 L 673.91 59.696 Z M 692.093 62.879 C 690.24 62.912 688.691 61.382 688.658 59.439 L 684.658 59.509 C 684.73 63.64 688.08 66.95 692.163 66.879 L 692.093 62.879 Z M 695.406 59.321 C 695.44 61.264 693.946 62.847 692.093 62.879 L 692.163 66.879 C 696.246 66.807 699.478 63.382 699.406 59.251 L 695.406 59.321 Z M 677.909 59.626 C 677.875 57.683 679.369 56.1 681.223 56.068 C 683.076 56.036 684.624 57.566 684.658 59.509 L 688.658 59.439 C 688.585 55.307 685.236 51.997 681.153 52.069 C 677.07 52.14 673.838 55.565 673.91 59.696 L 677.909 59.626 Z M 699.406 59.251 C 699.372 57.308 700.866 55.725 702.719 55.693 C 704.573 55.66 706.121 57.19 706.155 59.133 L 710.154 59.063 C 710.082 54.932 706.732 51.622 702.649 51.693 C 698.567 51.765 695.334 55.19 695.406 59.321 L 699.406 59.251 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 761.623 402.573 C 760.312 401.262 760.337 399.085 761.711 397.711 L 758.883 394.883 C 755.961 397.805 755.907 402.514 758.795 405.401 L 761.623 402.573 Z M 766.484 402.484 C 765.11 403.858 762.934 403.883 761.623 402.573 L 758.795 405.401 C 761.682 408.289 766.391 408.235 769.313 405.313 L 766.484 402.484 Z M 776.826 417.776 C 775.515 416.465 775.54 414.288 776.914 412.914 L 774.086 410.086 C 771.164 413.008 771.11 417.717 773.997 420.604 L 776.826 417.776 Z M 781.687 417.687 C 780.313 419.061 778.136 419.086 776.826 417.776 L 773.997 420.604 C 776.885 423.491 781.594 423.437 784.516 420.516 L 781.687 417.687 Z M 769.313 405.313 C 770.687 403.939 772.864 403.914 774.174 405.224 C 775.485 406.535 775.46 408.712 774.086 410.086 L 776.914 412.914 C 779.836 409.992 779.89 405.283 777.003 402.396 C 774.115 399.509 769.406 399.563 766.484 402.484 L 769.313 405.313 Z M 784.516 420.516 C 785.89 419.142 788.066 419.117 789.377 420.427 C 790.688 421.738 790.663 423.915 789.289 425.289 L 792.117 428.117 C 795.039 425.195 795.093 420.486 792.205 417.599 C 789.318 414.711 784.609 414.765 781.687 417.687 L 784.516 420.516 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 439.623 349.573 C 438.312 348.262 438.337 346.085 439.711 344.711 L 436.883 341.883 C 433.961 344.805 433.907 349.514 436.795 352.401 L 439.623 349.573 Z M 444.484 349.484 C 443.11 350.858 440.934 350.883 439.623 349.573 L 436.795 352.401 C 439.682 355.289 444.391 355.235 447.313 352.313 L 444.484 349.484 Z M 454.826 364.776 C 453.515 363.465 453.54 361.288 454.914 359.914 L 452.086 357.086 C 449.164 360.008 449.11 364.717 451.997 367.604 L 454.826 364.776 Z M 459.687 364.687 C 458.313 366.061 456.136 366.086 454.826 364.776 L 451.997 367.604 C 454.885 370.491 459.594 370.437 462.516 367.516 L 459.687 364.687 Z M 447.313 352.313 C 448.687 350.939 450.864 350.914 452.174 352.224 C 453.485 353.535 453.46 355.712 452.086 357.086 L 454.914 359.914 C 457.836 356.992 457.89 352.283 455.003 349.396 C 452.115 346.509 447.406 346.563 444.484 349.484 L 447.313 352.313 Z M 462.516 367.516 C 463.89 366.142 466.066 366.117 467.377 367.427 C 468.688 368.738 468.663 370.915 467.289 372.289 L 470.117 375.117 C 473.039 372.195 473.093 367.486 470.205 364.599 C 467.318 361.711 462.609 361.765 459.687 364.687 L 462.516 367.516 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 599 208 C 592.925 208 588 203.075 588 197 C 588 190.925 592.925 186 599 186 C 605.075 186 610 190.925 610 197 C 610 203.075 605.075 208 599 208 Z M 599 204 C 602.866 204 606 200.866 606 197 C 606 193.134 602.866 190 599 190 C 595.134 190 592 193.134 592 197 C 592 200.866 595.134 204 599 204 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 633 478 C 626.925 478 622 473.075 622 467 C 622 460.925 626.925 456 633 456 C 639.075 456 644 460.925 644 467 C 644 473.075 639.075 478 633 478 Z M 633 474 C 636.866 474 640 470.866 640 467 C 640 463.134 636.866 460 633 460 C 629.134 460 626 463.134 626 467 C 626 470.866 629.134 474 633 474 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 444 101 C 437.925 101 433 96.075 433 90 C 433 83.925 437.925 79 444 79 C 450.075 79 455 83.925 455 90 C 455 96.075 450.075 101 444 101 Z M 444 97 C 447.866 97 451 93.866 451 90 C 451 86.134 447.866 83 444 83 C 440.134 83 437 86.134 437 90 C 437 93.866 440.134 97 444 97 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 658 548 C 651.925 548 647 543.075 647 537 C 647 530.925 651.925 526 658 526 C 664.075 526 669 530.925 669 537 C 669 543.075 664.075 548 658 548 Z M 658 544 C 661.866 544 665 540.866 665 537 C 665 533.134 661.866 530 658 530 C 654.134 530 651 533.134 651 537 C 651 540.866 654.134 544 658 544 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 76 413 C 69.925 413 65 408.075 65 402 C 65 395.925 69.925 391 76 391 C 82.075 391 87 395.925 87 402 C 87 408.075 82.075 413 76 413 Z M 76 409 C 79.866 409 83 405.866 83 402 C 83 398.134 79.866 395 76 395 C 72.134 395 69 398.134 69 402 C 69 405.866 72.134 409 76 409 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 445.063 460.573 L 434.465 471.17 C 433.402 472.233 432.798 474.491 433.187 475.941 L 437.066 490.418 C 437.455 491.87 439.108 493.522 440.558 493.91 L 455.035 497.789 C 456.487 498.178 458.744 497.573 459.806 496.511 L 470.404 485.913 C 471.466 484.851 472.071 482.593 471.682 481.143 L 467.803 466.666 C 467.414 465.214 465.761 463.562 464.311 463.173 L 449.834 459.294 C 448.382 458.905 446.124 459.511 445.063 460.573 Z M 442.235 457.744 C 444.306 455.673 448.036 454.672 450.869 455.431 L 465.346 459.31 C 468.176 460.068 470.908 462.798 471.667 465.631 L 475.546 480.107 C 476.304 482.937 475.306 486.668 473.232 488.742 L 462.634 499.339 C 460.563 501.411 456.833 502.412 454 501.653 L 439.523 497.774 C 436.693 497.016 433.961 494.286 433.202 491.453 L 429.323 476.976 C 428.565 474.147 429.563 470.416 431.637 468.342 L 442.235 457.744 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 542.465 113.17 C 541.402 114.233 540.798 116.491 541.187 117.941 L 545.066 132.418 C 545.455 133.87 547.108 135.522 548.558 135.91 L 563.035 139.789 C 564.487 140.178 566.744 139.573 567.806 138.511 L 578.404 127.913 C 579.466 126.851 580.071 124.593 579.682 123.143 L 575.803 108.666 C 575.414 107.214 573.761 105.562 572.311 105.173 L 557.834 101.294 C 556.382 100.905 554.124 101.511 553.063 102.573 L 542.465 113.17 Z M 558.869 97.431 L 573.346 101.31 C 576.176 102.068 578.908 104.798 579.667 107.631 L 583.546 122.107 C 584.304 124.937 583.306 128.668 581.232 130.742 L 570.634 141.339 C 568.563 143.411 564.833 144.412 562 143.653 L 547.523 139.774 C 544.693 139.016 541.961 136.286 541.202 133.453 L 537.323 118.976 C 536.565 116.147 537.563 112.416 539.637 110.342 L 550.235 99.744 C 552.306 97.673 556.036 96.672 558.869 97.431 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 774.863 262.101 C 773.393 262.414 771.657 263.979 771.193 265.406 L 766.561 279.66 C 766.097 281.09 766.584 283.376 767.588 284.491 L 777.617 295.629 C 778.622 296.746 780.846 297.468 782.314 297.155 L 796.974 294.039 C 798.444 293.727 800.181 292.162 800.644 290.735 L 805.276 276.481 C 805.74 275.051 805.253 272.765 804.249 271.649 L 794.22 260.512 C 793.215 259.395 790.991 258.673 789.523 258.985 L 774.863 262.101 Z M 788.691 255.073 C 791.557 254.464 795.231 255.656 797.193 257.835 L 807.222 268.973 C 809.182 271.15 809.986 274.928 809.08 277.717 L 804.449 291.971 C 803.543 294.757 800.674 297.342 797.806 297.952 L 783.146 301.068 C 780.28 301.677 776.607 300.485 774.644 298.306 L 764.616 287.168 C 762.655 284.991 761.851 281.213 762.757 278.424 L 767.388 264.17 C 768.294 261.384 771.163 258.799 774.032 258.189 L 788.691 255.073 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 151.34 501.162 L 142.333 501.447 C 141.715 501.467 140.788 502.043 140.503 502.578 L 136.296 510.49 C 136.014 511.021 136.055 512.122 136.382 512.64 L 141.182 520.267 C 141.515 520.794 142.495 521.315 143.118 521.296 L 152.125 521.011 C 152.743 520.991 153.67 520.415 153.955 519.88 L 158.162 511.968 C 158.444 511.437 158.403 510.336 158.076 509.818 L 153.276 502.191 C 152.944 501.664 151.963 501.143 151.34 501.162 Z M 151.214 497.164 C 153.255 497.1 155.572 498.331 156.661 500.06 L 161.462 507.688 C 162.551 509.418 162.65 512.048 161.694 513.846 L 157.487 521.758 C 156.528 523.561 154.3 524.944 152.252 525.009 L 143.244 525.294 C 141.203 525.358 138.886 524.127 137.797 522.398 L 132.996 514.771 C 131.907 513.04 131.808 510.41 132.764 508.612 L 136.971 500.7 C 137.93 498.897 140.158 497.514 142.206 497.449 L 151.214 497.164 Z"
android:fillColor="#000000"/>
<path
android:pathData="M 180.019 393.802 L 192.019 379.802 C 192.737 378.963 192.64 377.7 191.802 376.981 C 190.963 376.263 189.7 376.36 188.981 377.198 L 176.981 391.198 C 176.263 392.037 176.36 393.3 177.198 394.019 C 178.037 394.737 179.3 394.64 180.019 393.802 Z M 406.044 230.871 L 422.044 247.871 C 422.801 248.675 424.066 248.713 424.871 247.956 C 425.675 247.199 425.713 245.934 424.956 245.129 L 408.956 228.129 C 408.199 227.325 406.934 227.287 406.129 228.044 C 405.325 228.801 405.287 230.066 406.044 230.871 Z M 669.121 155.671 L 682.121 137.671 C 682.768 136.776 682.566 135.525 681.671 134.879 C 680.776 134.232 679.525 134.434 678.879 135.329 L 665.879 153.329 C 665.232 154.224 665.434 155.475 666.329 156.121 C 667.224 156.768 668.475 156.566 669.121 155.671 Z M 600.741 408.453 L 613.741 432.453 C 614.268 433.424 615.481 433.785 616.453 433.259 C 617.424 432.732 617.785 431.519 617.259 430.547 L 604.259 406.547 C 603.732 405.576 602.519 405.215 601.547 405.741 C 600.576 406.268 600.215 407.481 600.741 408.453 Z M 880.148 281.367 L 869.148 265.367 C 868.522 264.457 867.277 264.226 866.367 264.852 C 865.457 265.478 865.226 266.723 865.852 267.633 L 876.852 283.633 C 877.478 284.543 878.723 284.774 879.633 284.148 C 880.543 283.522 880.774 282.277 880.148 281.367 Z"
android:fillColor="#000000"/>
</vector>
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.example.tiledrawable
import android.graphics.Bitmap
import android.graphics.BitmapShader
import android.graphics.Canvas
import android.graphics.ColorFilter
import android.graphics.Paint
import android.graphics.PixelFormat
import android.graphics.Shader
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
class TileDrawable(drawable: Drawable, tileMode: Shader.TileMode) : Drawable() {
private val paint: Paint
init {
paint = Paint().apply {
shader = BitmapShader(getBitmap(drawable), tileMode, tileMode)
}
}
override fun draw(canvas: Canvas) {
canvas.drawPaint(paint)
}
override fun setAlpha(alpha: Int) {
paint.alpha = alpha
}
override fun getOpacity() = PixelFormat.TRANSLUCENT
override fun setColorFilter(colorFilter: ColorFilter?) {
paint.colorFilter = colorFilter
}
private fun getBitmap(drawable: Drawable): Bitmap {
if (drawable is BitmapDrawable) {
return drawable.bitmap
}
val bmp = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight,
Bitmap.Config.ARGB_8888)
val c = Canvas(bmp)
drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
drawable.draw(c)
return bmp
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment