validar texto studio salto que obtener mascara linea eventos edittext datos caja android

texto - ¿Cómo cambiar los colores de un Drawable en Android?



salto de linea edittext android (17)

Creo que puedes usar Drawable.setColorFilter( 0xffff0000, Mode.MULTIPLY ) . Esto establecería los píxeles blancos en rojo, pero no creo que afecte a los píxeles transparentes.

Ver Drawable#setColorFilter

Estoy trabajando en una aplicación de Android, y tengo un dibujo que estoy cargando desde una imagen de origen. En esta imagen, me gustaría convertir todos los píxeles blancos a un color diferente, digamos azul, y luego almacenar en caché el objeto Dibujable resultante para poder usarlo más tarde.

Por ejemplo, digamos que tengo un archivo PNG de 20x20 que tiene un círculo blanco en el medio y que todo lo que está fuera del círculo es transparente. ¿Cuál es la mejor manera de convertir ese círculo blanco en azul y guardar en caché los resultados? ¿Cambia la respuesta si quiero usar esa imagen de origen para crear varios Drawables nuevos (por ejemplo, azul, rojo, verde, naranja, etc.)?

Supongo que querré usar un ColorMatrix de alguna manera, pero no estoy seguro de cómo.


Debes hacer esto para todas las APIs:

Drawable myIcon = getResources().getDrawable( R.drawable.button ); ColorFilter filter = new LightingColorFilter( Color.BLACK, Color.BLACK); myIcon.setColorFilter(filter);


Echa un vistazo a este código de ejemplo " ColorMatrixSample.java "

/* * Copyright (C) 2007 The Android Open Source Project * * 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.android.apis.graphics; import com.example.android.apis.R; import android.app.Activity; import android.content.Context; import android.graphics.*; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; public class ColorMatrixSample extends GraphicsActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new SampleView(this)); } private static class SampleView extends View { private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); private ColorMatrix mCM = new ColorMatrix(); private Bitmap mBitmap; private float mSaturation; private float mAngle; public SampleView(Context context) { super(context); mBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.balloons); } private static void setTranslate(ColorMatrix cm, float dr, float dg, float db, float da) { cm.set(new float[] { 2, 0, 0, 0, dr, 0, 2, 0, 0, dg, 0, 0, 2, 0, db, 0, 0, 0, 1, da }); } private static void setContrast(ColorMatrix cm, float contrast) { float scale = contrast + 1.f; float translate = (-.5f * scale + .5f) * 255.f; cm.set(new float[] { scale, 0, 0, 0, translate, 0, scale, 0, 0, translate, 0, 0, scale, 0, translate, 0, 0, 0, 1, 0 }); } private static void setContrastTranslateOnly(ColorMatrix cm, float contrast) { float scale = contrast + 1.f; float translate = (-.5f * scale + .5f) * 255.f; cm.set(new float[] { 1, 0, 0, 0, translate, 0, 1, 0, 0, translate, 0, 0, 1, 0, translate, 0, 0, 0, 1, 0 }); } private static void setContrastScaleOnly(ColorMatrix cm, float contrast) { float scale = contrast + 1.f; float translate = (-.5f * scale + .5f) * 255.f; cm.set(new float[] { scale, 0, 0, 0, 0, 0, scale, 0, 0, 0, 0, 0, scale, 0, 0, 0, 0, 0, 1, 0 }); } @Override protected void onDraw(Canvas canvas) { Paint paint = mPaint; float x = 20; float y = 20; canvas.drawColor(Color.WHITE); paint.setColorFilter(null); canvas.drawBitmap(mBitmap, x, y, paint); ColorMatrix cm = new ColorMatrix(); mAngle += 2; if (mAngle > 180) { mAngle = 0; } //convert our animated angle [-180...180] to a contrast value of [-1..1] float contrast = mAngle / 180.f; setContrast(cm, contrast); paint.setColorFilter(new ColorMatrixColorFilter(cm)); canvas.drawBitmap(mBitmap, x + mBitmap.getWidth() + 10, y, paint); setContrastScaleOnly(cm, contrast); paint.setColorFilter(new ColorMatrixColorFilter(cm)); canvas.drawBitmap(mBitmap, x, y + mBitmap.getHeight() + 10, paint); setContrastTranslateOnly(cm, contrast); paint.setColorFilter(new ColorMatrixColorFilter(cm)); canvas.drawBitmap(mBitmap, x, y + 2*(mBitmap.getHeight() + 10), paint); invalidate(); } } }

La API correspondiente está disponible here :


Ejemplo corto para cambiar el color isWorking acuerdo con el campo isWorking .

Mi forma xml:

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="@android:color/holo_blue_bright" /> <corners android:radius="30dp" /> <size android:height="15dp" android:width="15dp" /> </shape>

Mi método para cambiar:

private Drawable getColoredDrawable(int drawableResId, boolean isworking) { Drawable d = getResources().getDrawable(R.drawable.shape); ColorFilter filter = new LightingColorFilter( isworking ? Color.GREEN : Color.RED, isworking ? Color.GREEN : Color.RED); d.setColorFilter(filter); return d; }

Ejemplo de uso:

text1.setCompoundDrawablesWithIntrinsicBounds(getColoredDrawable(R.drawable.shape, isworking()), null, null, null);


El nuevo soporte v4 devuelve el tinte a api 4.

puedes hacerlo asi

public static Drawable setTint(Drawable d, int color) { Drawable wrappedDrawable = DrawableCompat.wrap(d); DrawableCompat.setTint(wrappedDrawable, color); return wrappedDrawable; }


En tu Actividad puedes colorear tus recursos de imagen PNG con un solo color:

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); myColorTint(); setContentView(R.layout.activity_main); } private void myColorTint() { int tint = Color.parseColor("#0000FF"); // R.color.blue; PorterDuff.Mode mode = PorterDuff.Mode.SRC_ATOP; // add your drawable resources you wish to tint to the drawables array... int drawables[] = { R.drawable.ic_action_edit, R.drawable.ic_action_refresh }; for (int id : drawables) { Drawable icon = getResources().getDrawable(id); icon.setColorFilter(tint,mode); } }

Ahora, cuando use R.drawable. *, Debe estar coloreado con el tinte deseado. Si necesita colores adicionales, entonces debería poder silenciar () el dibujo.


Es muy simple cuando usas una biblioteca para hacer eso por ti. Prueba esta library

Puedes llamar así:

Icon.on(holderView).color(R.color.your_color).icon(R.mipmap.your_icon).put();


Este fragmento de código funcionó para mí:

PorterDuffColorFilter porterDuffColorFilter = new PorterDuffColorFilter(getResources().getColor(R.color.your_color),PorterDuff.Mode.MULTIPLY); imgView.getDrawable().setColorFilter(porterDuffColorFilter); imgView.setBackgroundColor(Color.TRANSPARENT)


Esto funciona con todo con el fondo:

Vista de texto, botón ...

TextView text = (TextView) View.findViewById(R.id.MyText); text.setBackgroundResource(Icon); text.getBackground().setColorFilter(getResources().getColor(Color), PorterDuff.Mode.SRC_ATOP);


Hay tantas soluciones, pero nadie sugirió que si el archivo xml de recursos de color ya tiene color, podemos elegir directamente desde allí también como se muestra a continuación:

ImageView imageView = (ImageView) findViewById(R.id.imageview); imageView.setColorFilter(getString(R.color.your_color));


Prueba este código:

ImageView lineColorCode = (ImageView)convertView.findViewById(R.id.line_color_code); int color = Color.parseColor("#AE6118"); //The color u want lineColorCode.setColorFilter(color);


Pude hacer esto con el siguiente código, que se toma de una actividad (el diseño es muy simple, solo contiene un ImageView y no se publica aquí).

private static final int[] FROM_COLOR = new int[]{49, 179, 110}; private static final int THRESHOLD = 3; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_colors); ImageView iv = (ImageView) findViewById(R.id.img); Drawable d = getResources().getDrawable(RES); iv.setImageDrawable(adjust(d)); } private Drawable adjust(Drawable d) { int to = Color.RED; //Need to copy to ensure that the bitmap is mutable. Bitmap src = ((BitmapDrawable) d).getBitmap(); Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true); for(int x = 0;x < bitmap.getWidth();x++) for(int y = 0;y < bitmap.getHeight();y++) if(match(bitmap.getPixel(x, y))) bitmap.setPixel(x, y, to); return new BitmapDrawable(bitmap); } private boolean match(int pixel) { //There may be a better way to match, but I wanted to do a comparison ignoring //transparency, so I couldn''t just do a direct integer compare. return Math.abs(Color.red(pixel) - FROM_COLOR[0]) < THRESHOLD && Math.abs(Color.green(pixel) - FROM_COLOR[1]) < THRESHOLD && Math.abs(Color.blue(pixel) - FROM_COLOR[2]) < THRESHOLD; }


Sé que esta pregunta fue mucho antes de Lollipop, pero me gustaría agregar una buena forma de hacer esto en Android 5. +. Realiza un dibujable xml que hace referencia al original y establece un tinte como el siguiente:

<?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/ic_back" android:tint="@color/red_tint"/>

Echa un vistazo a mi blog en este para obtener más información .


Si tiene un dibujo que es un color sólido y desea cambiarlo a un color sólido diferente, puede utilizar un ColorMatrixColorFilter . Se conserva la transparencia.

int iColor = Color.parseColor(color); int red = (iColor & 0xFF0000) / 0xFFFF; int green = (iColor & 0xFF00) / 0xFF; int blue = iColor & 0xFF; float[] matrix = { 0, 0, 0, 0, red, 0, 0, 0, 0, green, 0, 0, 0, 0, blue, 0, 0, 0, 1, 0 }; ColorFilter colorFilter = new ColorMatrixColorFilter(matrix); drawable.setColorFilter(colorFilter);


También uso ImageView para los iconos (en ListView o en la pantalla de configuración). Pero creo que hay una forma mucho más sencilla de hacerlo.

Use tint para cambiar la superposición de color en el icono seleccionado.

En xml,

Android: tinte = "@ color / acento"
android: src = "@ drawable / ic_event"

Funciona bien ya que viene de AppCompat


Puedes resolverlo usando las bibliotecas de compatibilidad de Android. :)

// mutate to not share its state with any other drawable Drawable drawableWrap = DrawableCompat.wrap(drawable).mutate(); DrawableCompat.setTint(drawableWrap, ContextCompat.getColor(getContext(), R.color.your_color))


view.getDrawable().mutate().setColorFilter(0xff777777, PorterDuff.Mode.MULTIPLY);

Gracias a @sabadow