Android get color drawable

Color Drawable Class

Definition

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

A specialized Drawable that fills the Canvas with a specified color.

Remarks

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

Creates a new black ColorDrawable.

Creates a new black ColorDrawable.

A constructor used when creating managed representations of JNI objects; called by the runtime.

Properties

Returns the alpha value of this drawable’s color.

Tells if this Drawable will be automatically mirrored when its layout direction is RTL right-to-left. -or- Set whether this Drawable is automatically mirrored when its layout direction is RTL (right-to left).

(Inherited from Drawable) Bounds

Return the drawable’s bounds Rect. -or- Specify a bounding rectangle for the Drawable.

(Inherited from Drawable) Callback

Return the current Callback implementation attached to this Drawable.

(Inherited from Drawable) ChangingConfigurations

Return a mask of the configuration parameters for which this drawable may change, requiring that it be re-created. -or- Set a mask of the configuration parameters for which this drawable may change, requiring that it be re-created.

(Inherited from Drawable) Class

Returns the runtime class of this Object .

(Inherited from Object) Color

Gets the drawable’s color value. -or- Sets the drawable’s color value.

Returns the current color filter, or null if none set.

(Inherited from Drawable) Current (Inherited from Drawable) DirtyBounds

Return the drawable’s dirty bounds Rect.

(Inherited from Drawable) Handle

The handle to the underlying Android instance.

(Inherited from Object) HasFocusStateSpecified

Indicates whether this drawable has at least one state spec explicitly specifying android.R.attr#state_focused .

(Inherited from Drawable) IntrinsicHeight

Returns the drawable’s intrinsic height.

(Inherited from Drawable) IntrinsicWidth

Returns the drawable’s intrinsic width.

(Inherited from Drawable) IsFilterBitmap (Inherited from Drawable) IsProjected

Whether this drawable requests projection.

(Inherited from Drawable) IsStateful

Indicates whether this drawable will change its appearance based on state.

(Inherited from Drawable) IsVisible (Inherited from Drawable) JniIdentityHashCode (Inherited from Object) JniPeerMembers LayoutDirection

Returns the resolved layout direction for this Drawable.

(Inherited from Drawable) Level

Retrieve the current level.

(Inherited from Drawable) MinimumHeight

Returns the minimum height suggested by this Drawable.

(Inherited from Drawable) MinimumWidth

Returns the minimum width suggested by this Drawable.

(Inherited from Drawable) Opacity

Return the opacity/transparency of this Drawable.

Return in insets the layout insets suggested by this Drawable for use with alignment operations during layout.

(Inherited from Drawable) PeerReference (Inherited from Object) ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Returns a Region representing the part of the Drawable that is completely transparent.

(Inherited from Drawable)

Methods

Applies the specified theme to this Drawable and its children.

(Inherited from Drawable)

Читайте также:  Record on android phone
CanApplyTheme() (Inherited from Drawable) ClearColorFilter()

Removes the color filter for this drawable.

(Inherited from Drawable) Clone()

Creates and returns a copy of this object.

(Inherited from Object) CopyBounds()

Return a copy of the drawable’s bounds in a new Rect.

(Inherited from Drawable) CopyBounds(Rect)

Return a copy of the drawable’s bounds in a new Rect.

(Inherited from Drawable) Dispose() (Inherited from Object) Dispose(Boolean) (Inherited from Object) Draw(Canvas)

Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).

Indicates whether some other object is «equal to» this one.

(Inherited from Object) GetConstantState()

Return a ConstantState instance that holds the shared state of this Drawable.

(Inherited from Drawable) GetHashCode()

Returns a hash code value for the object.

(Inherited from Object) GetHotspotBounds(Rect)

Populates outRect with the hotspot bounds.

(Inherited from Drawable) GetOutline(Outline)

Called to get the drawable to populate the Outline that defines its drawing area.

(Inherited from Drawable) GetPadding(Rect)

Return in padding the insets suggested by this Drawable for placing content inside the drawable’s bounds.

(Inherited from Drawable) GetState()

Describes the current state, as a union of primitve states, such as android.R.attr#state_focused , android.R.attr#state_selected , etc.

(Inherited from Drawable) Inflate(Resources, XmlReader, IAttributeSet)

Inflate this Drawable from an XML resource.

(Inherited from Drawable) Inflate(Resources, XmlReader, IAttributeSet, Resources+Theme)

Inflate this Drawable from an XML resource.

(Inherited from Drawable) InflateAsync(Resources, XmlReader, IAttributeSet) (Inherited from Drawable) InflateAsync(Resources, XmlReader, IAttributeSet, Resources+Theme) (Inherited from Drawable) InvalidateSelf()

Use the current Callback implementation to have this Drawable redrawn.

(Inherited from Drawable) JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object) JumpToCurrentState()

If this Drawable does transition animations between states, ask that it immediately jump to the current state and skip any active animations.

(Inherited from Drawable) Mutate()

Make this drawable mutable.

(Inherited from Drawable) Notify()

Wakes up a single thread that is waiting on this object’s monitor.

(Inherited from Object) NotifyAll()

Wakes up all threads that are waiting on this object’s monitor.

(Inherited from Object) OnBoundsChange(Rect)

Override this in your subclass to change appearance if you vary based on the bounds.

(Inherited from Drawable) OnLayoutDirectionChanged(Int32)

Called when the drawable’s resolved layout direction changes.

(Inherited from Drawable) OnLevelChange(Int32)

Override this in your subclass to change appearance if you vary based on level.

(Inherited from Drawable) OnStateChange(Int32[])

Override this in your subclass to change appearance if you recognize the specified state.

(Inherited from Drawable) ScheduleSelf(Action, Int64) (Inherited from Drawable) ScheduleSelf(IRunnable, Int64)

Use the current Callback implementation to have this Drawable scheduled.

(Inherited from Drawable) SetAlpha(Int32)

Applies the given alpha to the underlying color.

Specify a bounding rectangle for the Drawable.

(Inherited from Drawable) SetCallback(Drawable+ICallback)

Bind a Callback object to this Drawable.

(Inherited from Drawable) SetColorFilter(Color, PorterDuff+Mode)

Specify a color and Porter-Duff mode to be the color filter for this drawable.

(Inherited from Drawable) SetColorFilter(ColorFilter)

Sets the color filter applied to this color.

Set to true to have the drawable dither its colors when drawn to a device with fewer than 8-bits per color component.

(Inherited from Drawable) SetFilterBitmap(Boolean)

Set to true to have the drawable filter its bitmaps with bilinear sampling when they are scaled or rotated.

(Inherited from Drawable)

Читайте также:  Что применяют для андроид
SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object) SetHotspot(Single, Single)

Specifies the hotspot’s location within the drawable.

(Inherited from Drawable) SetHotspotBounds(Int32, Int32, Int32, Int32)

Sets the bounds to which the hotspot is constrained, if they should be different from the drawable bounds.

(Inherited from Drawable) SetLayoutDirection(LayoutDirection)

Set the layout direction for this drawable.

(Inherited from Drawable) SetLevel(Int32)

Specify the level for the drawable.

(Inherited from Drawable) SetState(Int32[])

Specify a set of states for the drawable.

(Inherited from Drawable) SetTint(Int32)

Specifies tint color for this drawable.

(Inherited from Drawable) SetTintBlendMode(BlendMode)

Specifies a tint blending mode for this drawable.

(Inherited from Drawable) SetTintList(ColorStateList)

Specifies tint color for this drawable as a color state list.

(Inherited from Drawable) SetTintMode(PorterDuff+Mode)

Specifies a tint blending mode for this drawable.

(Inherited from Drawable) SetVisible(Boolean, Boolean)

Set whether this Drawable is visible.

(Inherited from Drawable) ToArray () (Inherited from Object) ToString()

Returns a string representation of the object.

(Inherited from Object) UnregisterFromRuntime() (Inherited from Object) UnscheduleSelf(Action) (Inherited from Drawable) UnscheduleSelf(IRunnable)

Use the current Callback implementation to have this Drawable unscheduled.

(Inherited from Drawable) Wait()

Causes the current thread to wait until another thread invokes the java.lang.Object#notify() method or the java.lang.Object#notifyAll() method for this object.

(Inherited from Object) Wait(Int64)

Causes the current thread to wait until another thread invokes the java.lang.Object#notify() method or the java.lang.Object#notifyAll() method for this object.

(Inherited from Object) Wait(Int64, Int32)

Causes the current thread to wait until another thread invokes the java.lang.Object#notify() method or the java.lang.Object#notifyAll() method for this object.

(Inherited from Object)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

Performs an Android runtime-checked type conversion.

Источник

Manipulating images and Drawables with Android’s ColorFilter

Tinting, custom effects and reusability for visual Android app resources

Image and Drawable resources are an integral part of any Android app. Since day (i.e. API level) 1, the Android framework has provided a means of manipulating the colors of these assets on a per-pixel level, as they are drawn to screen, with the ColorFilter class.

The documentation for the base abstract class stipulates that “a color filter can be used with a Paint to modify the color of each pixel drawn with that paint”. This is a powerful tool that can be used for:

  • Applying effects and adjustments to images
  • Tinting of icons and logos

Ultimately this encourages the good practice of reusing resources for scenarios that require different color variations, resulting in reduced app size.

Note: ColorFilter is an abstract class that should never be used directly, and the constructor was deprecated in API level 26. Only the subclasses should be used, which we will discuss further down.

Where can this be used? 🤔

Before we explore the existing subclasses and their capabilities, we need to discuss where we can actually use ColorFilter .

Canvas

As hinted at in the documentation description, we can use ColorFilter when drawing to Canvas in a custom View . Specifically, a ColorFilter is set on a Paint which then affects everything that is drawn with it:

Drawable

A ColorFilter can be applied to a Drawable , which will affect how it is rendered in View s and layouts:

In addition to this, there exists a Drawable convenience function to apply a PorterDuffColorFilter (more on that later):

Lastly, it is important to note that a tint can be applied to a Drawable . This is separate and will be overridden if a ColorFilter is set via either one of the above functions.

Читайте также:  Русские новеллы для андроид

ImageView

A ColorFilter can be applied to an ImageView , which will affect how its current image will be rendered:

As with Drawable , convenience functions exist for applying a PorterDuffColorFilter :

Introducing our sample image 🖼️

We are now going to dive into the three subclasses of ColorFilter ( PorterDuffColorFilter , LightingColorFilter and ColorMatrixColorFilter ). In order to demonstrate this, we will make use of a visual aid in the form of a sample image:

It has photographic detail while also having “shape” as a result of the transparent areas. This will allow all of the subclasses to be demonstrated.

PorterDuffColorFilter 1️⃣

We have already touched on this briefly. This is a color filter that accepts a single color that is applied to all source pixels along with a Porter-Duff composite mode. There are many modes that are suitable to different scenarios. Typically, this is used to apply a “blanket” color (eg. To tint an icon).

LightingColorFilter 2️⃣

This color filter can be used to simulate lighting effects on an image. The constructor accepts two parameters, the first to multiply the source color ( colorMultiply ) and the second to add to the source color ( colorAdd ).

While the source color alpha channel is not affected, the R, G and B channels are computed like so:

Note: The above is using Android KTX Color extension functions for accessing the red, blue and green channels (alpha is also available).

ColorMatrixColorFilter 3️⃣

This is arguably the most flexible (but also the most complex) color filter.

It is quite similar to LightingColorFilter , in that we can tweak each pixel’s color channels using values that are multiplicative and additive. However, a ColorMatrixColorFilter is constructed with a ColorMatrix , which is essentially a wrapper class for a 4×5 matrix. This gives us 20 values, used in a certain way, that allow us to transform colors using all of the existing channels, including alpha.

Before we dive into using matrices, let’s first take a look at some of the convenience functions offered by ColorMatrix :

Fear not if some of these color concepts do not make sense! The point here is that ColorMatrix offers a lot of flexibility, and these convenience functions are simply matrix operations under the hood.

We know that ColorMatrix wraps a 4×5 matrix. Given this matrix, how do we arrive at our resultant color channels? They are computed like so:

The 4 rows in fact represent the resultant R, G, B and A channels. For each channel, the 5 columns allow you to combine the existing R, G, B, and A channels and a wildcard value in a plethora of ways.

This provides us with a lot of possibilities. We can adjust the brightness/contrast of images, ignore some channels, invert an image or even create basic filters and effects.

Limitations and other options 🛠️

While ColorFilter is powerful and flexible, image manipulation is a vast field and it simply cannot cover everything.

For example, the current pixel value that is being passed through the filter would be handy to know, but is not available. Additionally, you are limited to the three subclasses that ColorFilter currently provides. It appears as if you cannot create a custom subclass, due to native code restrictions.

In instances like this, what other options do we have?

The graphics framework has other useful classes such as Shader and MaskFilter . We could turn to RenderScript , which offers Bitmap utilities that still keep us mostly within traditional Android graphics territory. There is also OpenGL, which is perhaps the most extreme power vs. complexity tradeoff, but opens up all of the possibilities of custom GLSL shaders.

Overall, ColorFilter is still a fantastic tool for working with app resources.

I hope this post has provided some insight into ColorFilter and the flexibility it provides when working with images and Drawable s. If you have any questions, thoughts or suggestions then I’d love to hear from you!

Источник

Оцените статью