DrawMe


Source link: https://github.com/rafakob/DrawMe

DrawMe

With DrawMe you can easily create views with custom background shapes using only XML layout files. Quick example:

Standard way:

<!--drawable/back.xml--> <shape

xmlns:android="http://schemas.android.com/apk/res/android"

android:shape="rectangle">

<corners android:radius="5dp" />
  <solid android:color="@android:color/white" />
  <stroke

android:width="3dp"

android:color="@android:color/black" /> </shape>
<!--layout/activity_main.xml--> <FrameLayout
  android:layout_width="100dp"
  android:layout_height="100dp"
  android:background="@drawable/back.xml"/>

With DrawMe:

<com.rafakob.drawme.DrawMeFrameLayout
  android:layout_width="100dp"
  android:layout_height="100dp"
  drawme:dm_backColor="@android:color/white"
  drawme:dm_strokeColor="@android:color/black"
  drawme:dm_radius="5dp"
  drawme:dm_stroke="3dp"/>

Library also supports different states: normal, pressed, disabled. It means that you can define different background and stroke colors for each state. The best thing about it is that DrawMe is "compatible" with pre and post Lollipop. On API +21 it uses native ripple press effect, and below it uses standard StateListDrawable. Here's example:

Standard way:

<!--drawable/custom_button.xml--> <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_enabled="false">

<shape android:shape="rectangle">

 <solid android:color="@color/colorDarkGray"/>

</shape>
  </item>
  <item android:state_pressed="false">

<shape android:shape="rectangle">

 <solid android:color="@color/colorButtonGreen"/>

</shape>
  </item>
  <item android:state_pressed="true">

<shape android:shape="rectangle">

 <solid android:color="@color/colorButtonGreenFocused"/>

</shape>
  </item> </selector>
<!--drawable-v21/custom_button.xml--> <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_enabled="false">

<shape android:shape="rectangle">

 <solid android:color="@color/colorDarkGray"/>

</shape>
  </item>
  <item android:state_enabled="true">

<ripple

 xmlns:android="http://schemas.android.com/apk/res/android"

 android:color="@color/colorButtonGreenFocused">

 <item>

  <color android:color="@color/colorButtonGreen"/>

 </item>

</ripple>
  </item> </selector>
<!--layout/activity_main.xml--> <Button
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:background="@drawable/custom_button.xml"/>

With DrawMe:

<com.rafakob.drawme.DrawMeButton
  android:layout_width="match_parent"
  android:layout_height="50dp"
  drawme:dm_backColor="@color/colorButtonGreen"
  drawme:dm_backColorPressed="@color/colorButtonGreenFocused"
  drawme:dm_backColorDisabled="@color/colorDarkGray"
  drawme:dm_shapeRadiusHalfHeight="true"/>

Result

Pre Lollipop

API +21

Install

Add it in your root build.gradle at the end of repositories:

allprojects {
  repositories {

...
maven {
 url "https://jitpack.io" 
}
  
}
 
}

Add the dependency:

dependencies {

compile 'com.github.rafakob:DrawMe:VERSION' 
}

Features

Provided attributes are only accessiable from XML, you can't modify them from JAVA code (at least for now).

I tried to use as many default values as possible, so that user wouldn't have to specify too many attributes and colors. For instance you can define background color only for a normal state - pressed and disabled will be generated.

To fully understand everything just play around with it for a while. No rocket science here :)

EXAMPLES

Background shape

Attribute Format Default Description
dm_stroke dimension 0 Stroke width.
dm_radius dimension 0 Corner radius.
dm_radiusBottomLeft dimension -1 "-1" means that attribute will be ignored.
dm_radiusBottomRight dimension -1 as above
dm_radiusTopLeft dimension -1 as above
dm_radiusTopRight dimension -1 as above
dm_shapeRadiusHalfHeight boolean false True - use "full radius", other radius dimensions will be ignored.
dm_shapeEqualWidthHeight boolean false True - shape will have equal height and width.
  • Set dm_shapeRadiusHalfHeight and dm_shapeEqualWidthHeight in order to create a circle.
  • You can combine dm_radius with other radius dimensions, it works just like padding.

States and ripple

Attribute Format Default Description
dm_rippleEffect boolean true True - use ripple effect on API +21. False - use standard StateListDrawable on all devices.
dm_rippleUseControlHighlight boolean true Use theme attribute R.attr.colorControlHighlight as a ripple color. It's avaiable only on +21, and it's basically a 1F000000 color. On lower API "mimic" this highlight by mixing background color with a black mask (it should look almost the same as on +21). Matters only when pressed background color hasn't been specified.
dm_statePressed boolean true True - create pressed state. False - ignore it.
dm_stateDisabled boolean true True - create disabled state. False - ignore it.

First button is in normal state. Second is pressed with default ripple highlight. Third one uses custom color as pressed ripple effect:

Colors

Attribute Format Default Description
dm_backColor color Color.TRANSPARENT Background color - default state.
dm_backColorPressed color Dark highlight (like ripple) Background color - pressed state.
dm_backColorDisabled color Grayed out backColor Background color - disabled state.
dm_strokeColor color Color.GRAY Stroke color - default state.
dm_strokeColorPressed color Dark highlight (like ripple) Stroke color - pressed state.
dm_strokeColorDisabled color Grayed out strokeColor Stroke color - disabled state.
  • If you're not gonna define any attribute, view will have transaprent borderless background with pressed state like ripple effect.
  • Use android:stateListAnimator="@null" to disable default press shadow/elevation on +21.

Layouts

Avaiable layouts:

  • DrawMeFrameLayout
  • DrawMeLinearLayout
  • DrawMeRelativeLayout
  • DrawMeImageButton

All above attributes can be applied to them.

TextViews

Avaiable views:

  • DrawMeButton
  • DrawMeTextView
  • DrawMeEditText
Attribute Format Default Description
dm_font string null Name of a custom font, eg. Lato-Regular.ttf.
dm_textColor color System default Text color - default state. Same as android:textColor.
dm_textColorPressed color textColor Text color - pressed state.
dm_textColorDisabled color textColor Text color - disabled state.
dm_drawableTint color none Equivalent to android:drawableTint but it's compatible with preLollipop devices.
dm_drawableTintMode enum none Equivalent to android:drawableTintMode but it's compatible with preLollipop devices.
  • Custom fonts has to be put in assets/fonts. Library uses font caching.
  • Tintintg is performed using DrawableCompat.wrap() method.

Advanced - mask, adaptive highlight

Attribute Format Default
dm_maskColorPressed color #1F00000
dm_maskColorPressedInverse color #1DFFFFFF
dm_maskColorDisabled color #6DFFFFFF
dm_maskBrightnessThreshold float [0 - 1] 0

By default ripple color is just a blackish shadow over normal state color. I've added an option to adjust that shadow mask. Same applies to disabled color. Just modify dm_maskColorPressed and dm_maskColorDisabled:

But what when I have a black or very dark buttons? Ripple effect won't be visiable. In that case you have three options:

  • leave it as is
  • change dm_maskColorPressed to a lighter color
  • use an adaptive method by modifing dm_maskBrightnessThreshold

So how the adaption works?

  • Library calculates background color brightness: 0.0 means that background is black, 1.0 means it's white.
  • Compare background brightness with dm_maskBrightnessThreshold. If it's lower than use dm_maskColorPressedInverse instead of dm_maskColorPressed.

Example:

  • maskBrightnessThreshold = 0.2
  • Button X has background brightness 0.8 -> use dm_maskColorPressed -> ripple will be darker than background color
  • Button Y has background brightness 0.1 -> use dm_maskColorPressedInverse -> ripple will be lighter than background color

About

If you find any bugs feel free to report an issue or create a pull request. Suggestions and feature requestes are very welcome.

Use it wherever and however you want.

Changelog

0.1.6 (2016-04-04):#####
  • Support for theme buttonStyle. ##### 0.1.5 (2016-04-01):#####
  • Added dm_ prefix for all attributes.

License

Copyright 2016 Rafa? Koby?ko  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. 

Resources

UniversalFontComponents is a library that allows you to set a custom font in xml for all your TextViews. You can also set a library-provided Roboto font so that your fonts are consistent across different versions of Android.

A grace comic reader widget, expand ListView.

RxAnimation is animation event wrapping observable.

You can get android animation event observable. And you can call animation chain and combination more simple.

A simple tool that makes it more convenient to install, uninstall and creating bug reports and more for multiple apps on multiple devices with one command. Additionally uninstalling allows to use wildcards as package name. This is basically a front-end for the Android Debug Bridge (ADB) which is required to run.

A tool that helps signing and zip aligning single or multiple Android application packages (APKs) with either debug or provided release certificates. It supports v1 and v2 Android signing scheme has embedded a debug keystore and auto verifies after signing.

Vidsta is a simple and easy to use video player, with customisable options for seamless integration. It's very lightweight, and features listeners for most possible events.

A sample app is also available.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes