Is There any solution to set image darkly?
<LinearLayout
android:background="#drawable/terminalImage"
android:layout_width="match_parent"
android:layout_height="match_parent">
I wrote like this, so I can't use background anymore. Is there any option or solution to set background image darkly?
Add backgdround tint which will add color upon the Existing background.
android:backgroundTint="#android:color/background_dark"
You can Add Gradient on Image .
Check this Post[How to put black transparent on image in android
Or You can try this post .
Alpha-gradient on Android
you can check the opacity from here.
How to make a background 20% transparent on Android
For do it Programmaitcally
private Bitmap darkenBitMap(Bitmap bm) {
Canvas canvas = new Canvas(bm);
Paint p = new Paint(Color.RED);
//ColorFilter filter = new LightingColorFilter(0xFFFFFFFF , 0x00222222); // lighten
ColorFilter filter = new LightingColorFilter(0xFF7F7F7F, 0x00000000); // darken
p.setColorFilter(filter);
canvas.drawBitmap(bm, new Matrix(), p);
return bm;
}
Related
Is it possible to put an alpha on an imageview?
Not on the image but directly on the view?
At the moment I need to photoshop the image and
I don't want to edit every image.
It should look like this:
You might be able to fake the effect you're trying to achieve using:
android:foreground="#drawable/image_overlay"
or
imageView.setForeground(imageOverlayDrawable);
This will not actually make the image transparent, but if you have a static solid background color it should be enough to create the illusion of the image blending with the background.
If that's not an option try something like this:
// Get the image from wherever it lives and create a Bitmap of it
...
// Draw the image to a canvas so that we can modify it
Canvas canvas = new Canvas(image);
Paint imagePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(imageBitmap, 0, 0, imagePaint);
// Create a paint to draw the overlay with
PorterDuffXfermode porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY);
Paint overlayPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
overlayPaint.setXfermode(porterDuffXfermode);
// The overlay in this case needs to be a white gradient (where fully opaque means that
// the image will be left untouched and fully transparent will completely erase the image)
Bitmap overlayBitmap = BitmapFactory.decodeResource(getResources(), R.id.drawable.gradient_overlay, Bitmap.Config.ARGB_8888);
// Apply the overlay to create the alpha effect
canvas.drawBitmap(overlayBitmap, 0, 0, overlayPaint);
// Update the ImageView
imageView.setImageBitmap(bitmap);
Android Studio 2.0 Preview 4
I am trying to add a blue transparent overlay color to an existing couplePhoto.jpg and display in a ImageView.
This is the effect I am trying to get, like google newsstand app.
This is my current photo that I am trying to change, but each time the photo is loaded into the ImageView nothing has changed.
I think the best way of doing this is to use a transparent color that will overlay the photo.
Using the following color with some alpha that will overlay onto the photo.
<color name="colorBackgroundShape">#99303F9F</color>
Here are my attempts below.
/* Attempt 1 */
ivBackground.setColorFilter(R.color.colorBackgroundShape, PorterDuff.Mode.DARKEN);
ivBackground.setColorFilter(R.color.colorBackgroundShape);
ivBackground.setImageBitmap(bitmapCouple);
/* Attempt 2 */
Bitmap bitmapCouple = BitmapFactory.decodeResource(getResources(), R.drawable.couplePhoto, options);
bitmap.eraseColor(getResources().getColor(R.color.colorBackgroundShape));
ivBackground.setImageBitmap(bitmapCouple);
/* Attempt 3 */
Bitmap bitmapCouple = BitmapFactory.decodeResource(getResources(), R.drawable.couplePhoto, options);
Bitmap newBitmap = Bitmap.createBitmap(bitmapCouple.getWidth(), bitmapCouple.getHeight(), bitmapCouple.getConfig());
Canvas canvas = new Canvas(newBitmap);
canvas.drawColor(getResources().getColor(R.color.colorBackgroundShape));
canvas.drawBitmap(bitmapCouple, 0, 0, null);
ivBackground.setImageBitmap(bitmapCouple);
/* Attempt 4 */
Bitmap bitmapCouple = BitmapFactory.decodeResource(getResources(), R.drawable.couplePhoto, options);
Canvas canvas = new Canvas();
canvas.drawColor(getResources().getColor(R.color.colorBackgroundShape));
canvas.drawBitmap(bitmapCouple, new Matrix(), null);
ivBackground.setImageBitmap(bitmapCouple);
Many thanks for any suggestions,
Your first try was closest to truth. In this case you can set anything to ImageView and then apply color filter (mine is transparent_black).
imageView.setImageBitmap(daBitmap);
PorterDuffColorFilter greyFilter =
new PorterDuffColorFilter(getResources().getColor(R.color.transparent_black), PorterDuff.Mode.SRC_ATOP);
imageView.setColorFilter(greyFilter);
Check this:
Bitmap photo = BitmapFactory.decodeResource(mContext.getResources(), place.getImageResourceId(mContext));
Palette.generateAsync(photo, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
int bgColor = palette.getMutedColor(mContext.getResources().getColor(android.R.color.black));
holder.placeNameHolder.setBackgroundColor(bgColor);
}
});
By using generateAsync(...) to generate a color palette in the background, you’ll receive a callback when the palette has been generated in the form of onGenerated(...). Here you can access the generated color palette and set the background color of holder.placeNameHolder. If the color doesn’t exist, the method will apply a fallback color — in this case, android.R.color.black.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/view"
android:layout_width="300dp"
android:scaleType="fitXY"
android:layout_height="300dp"
/>
<ImageView
android:id="#+id/imgVie"
android:layout_width="300dp"
android:scaleType="fitXY"
android:background="Your color"
android:layout_height="300dp"
/>
</RelativeLayout>
You can use FrameLayout with two imageviews in it. In one load your image and in other load in the other imageview
I'm attempting to colorize a graphic using setColorFilter. The following code seems to work fine on lollipop, but it seems to have no effect on kitkat, the icon is rendered in it's original colors:
Drawable icon = ContextCompat.getDrawable(context, R.drawable.ic_chat_button).mutate();
icon.setColorFilter(context.getResources().getColor(R.color.control_tint_color), PorterDuff.Mode.SRC_ATOP);
icon.invalidateSelf();
The mutate and invalidateSelf calls don't seem to have any effect on the problem here, just leaving them in as an example of part of what's been tried to figure out what's going on.
FWIW, I'm using the drawable as part of a LayerDrawable in a StateListDrawable that gets used as either the background for a button or as the drawable for an ImageView The results are consistent (ie., wrong on kitkat) either way. I've also tried putting the icon drawable directly into the StateListDrawable again with no change in behavior. In all cases, it works fine on lollipop, but doesn't work on kitkat.
As an experiment, I took the tinted Drawable out of the StateListDrawable but not the LayerDrawable and it works as expected. Apparently there's something flawed in KitKat's implementation of StateListDrawable that prevents it from working, that has been remedied in later versions.
Ultimately, it seems like the problem is that KitKat doesn't support using a ColorFilter (or implicitly an alpha) on a Drawable that will in turn be in a StateListDrawable. My solution was to use the same to code to construct the complex Drawable and then render that into a simple BitMapDrawable:
static Drawable createDrawable(Context context, int color, boolean disabled) {
OvalShape oShape = new OvalShape();
ShapeDrawable background = new ShapeDrawable(oShape);
background.getPaint().setColor(color);
ShapeDrawable shader = new ShapeDrawable(oShape);
shader.setShaderFactory(new ShapeDrawable.ShaderFactory() {
#Override
public Shader resize(int width, int height) {
return new LinearGradient(0, 0, 0, height,
new int[]{
Color.WHITE,
Color.GRAY,
Color.DKGRAY,
Color.BLACK
}, null, Shader.TileMode.REPEAT);
}
});
Drawable icon = ContextCompat.getDrawable(context, R.drawable.ic_chat_button).mutate();
icon.setColorFilter(context.getResources().getColor(R.color.control_tint_color), PorterDuff.Mode.SRC_IN);
Drawable layer = new LayerDrawable(new Drawable[]{ shader, background, icon });
layer.setAlpha(disabled ? 128 : 255);
// Note that on KitKat, setting a ColorFilter on a Drawable contained in a StateListDrawable
// apparently doesn't work, although it does on later versions, so we have to render the colored
// bitmap into a BitmapDrawable and then put that into the StateListDrawable
Bitmap bitmap = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
layer.setBounds(0, 0, layer.getIntrinsicWidth(), layer.getIntrinsicHeight());
layer.draw(canvas);
return new BitmapDrawable(context.getResources(), bitmap);
}
Rather than attach the coloring to something like "disabled" state (as in the accepted answer) I found the answer to be simpler by focusing on recoloring and let my usage leverage how to include the now-tinted image in the StateListDrawable. (And FYI, I've tried to translate from the Xamarin C# I'm using, but the below code may not complie correctly as Java)
static Drawable recolorDrawable(Drawable icon, int toColor)
{
Bitmap bitmap = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas myCanvas = new Canvas(bitmap);
icon.setColorFilter(toColor, PorterDuff.Mode.SRC_IN);
icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
icon.draw(myCanvas);
return new BitmapDrawable(context.getResources(), bitmap);
}
Finally, in all fairness to the accepted answer, I'm rather foreign to Android development and can thank him for showing the pieces I needed before then simplifying them.
In My Application I want to support such functionality where users can select color from the list (which is downloaded from WebService) and as User selects any color(say Orange) all the Buttons in my application will have background color as Orange.
I have checked solutions like http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html but with that I need to have different styles created with different color in my app.
My need is I only support one style only background colors need to be changed according selection and for that I don't want to fetch all my buttons at runtime and apply that color.
So please suggest any other better solutions to apply selected color to all the buttons in the application without fetching all buttons in activities and setting their background color.
You could fill a Bitmap with a particular color.
private BitmapDrawable makeColorFillDrawable(int color, Drawable d) {
Bitmap maskBitmap = ((BitmapDrawable) d).getBitmap();
final int width = maskBitmap.getWidth();
final int height = maskBitmap.getHeight();
maskBitmap = null;
final Bitmap outBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(outBitmap);
d.setBounds(0, 0, width, height);
d.setColorFilter(color, PorterDuff.Mode.SRC_IN);
d.draw(canvas);
d.setColorFilter(null);
d.setCallback(null);
return new BitmapDrawable(getResources(), outBitmap);
}
Save the color you fetch from your server in your SharedPreferences. Then subclass Button and apply the color fill drawable based on the value you saved. All you'd need to do then is replace <Button.../> in your XML with the path to your custom one.
Here's an example with the ActionBar icon.
final Drawable d = getResources().getDrawable(android.R.drawable.sym_def_app_icon);
getActionBar().setIcon(makeColorFillDrawable(Color.RED, d));
I want to set home wallpaper with white bitmap:
Bitmap bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(0xfff);
WallpaperManager wall = WallpaperManager.getInstance(this);
try {
wall.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
And the wallpaper becomes black. What's wrong is here?
Just add bitmap.eraseColor(Color.WHITE);
as second line
My first guess would be your color choice, assuming this is the value in your actual code and not edited.
Color ints in java take the form ARGB, so Color.WHITE is 0xFFFFFFFF, Color.BLUE is 0xFF0000FF, etc.
The color in your code (0xFFF) would expand to 0x00000FFF which is Blue with a little green mixed in, but the alpha channel is zero, so the Canvas is basically written with a transparent color.
If you are using standard colors, I would stick to the constants in the Color class as parameters here, but if you want to define the color yourself, remember to place the full color or use Canvas.drawRGB() instead.