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.
Related
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;
}
I am working with bitmap images whose transparent parts are colored in magenta (in some languages it is possible to set a color as transparent). I try to transparent pixels which are in magenta in the original bitmap image.
I load the bitmap from SD-card:
Bitmap bitmap = BitmapFactory.decodeFile(myImagePath);
copy it to another bitmap to make it mutable:
Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_8888,true);
Then scan it pixel by pixel to find pixels in magenta and try to change their transparency.
for(int x=0;x<bitmap2.getWidth();x++){
for(int y=0;y<bitmap2.getHeight();y++){
if(bitmap2.getPixel(x, y)==Color.rgb(0xff, 0x00, 0xff))
{
int alpha = 0x00;
bitmap2.setPixel(x, y , Color.argb(alpha,0xff,0xff,0xff)); // changing the transparency of pixel(x,y)
}
}
}
But those pixels which I expect to become transparent are converted to black. By changing the alpha, I found that the final color varies from the mentioned color in argb() (without mentioning the alpha) to black. For instance, Color.argb(0xff,0xff,0xff,0xff) gets white, Color.argb(0x80,0xff,0xff,0xff) gets gray and Color.argb(0x00,0xff,0xff,0xff) gets black.
I don't undrestand what's wrong.
Could it be possible that there is no alpha channel and I should first set/define it? if yes, how?
EDIT1:
According to the comment of Der Gol...lum I have modified my code:
Paint mPaint = new Paint();
mPaint.setAlpha(0);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
mPaint.setAntiAlias(true);
Bitmap bitmap = BitmapFactory.decodeFile(myBackImagePath).copy(Bitmap.Config.ARGB_8888 , true);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bitmap, 0, 0, mPaint);
if(bitmap.getPixel(0, 0)==Color.rgb(0xff, 0x00, 0xff))
{
for(int x=0;x<bitmap.getWidth();x++){
for(int y=0;y<bitmap.getHeight();y++){
if(bitmap.getPixel(x, y)==Color.rgb(0xff, 0x00, 0xff))
{
bitmap.setPixel(x, y,Color.TRANSPARENT);
}
}
}
But the result is more or less the same. Using different PorterDuffModes causes either transparency of the entire bitmap or make the targeted pixels black:
Would anybody have any idea?
I could finally find the problem.
My png images had no alpha channel or maybe their alpha channel were not activated.
what I did to solve this problem is to add:
bitmap.setHasAlpha(true);
and it works how I expected.
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.
My goal is to have a "green screen" effect for an ImageView and a border. I have a png border with a somewhat thick solid outline, the shape is irregular, and is transparent both inside and outside the border. I want to use it as a border of another image, say a photo, and because the border shape is irregular, some parts of the photo may lie outside of the border.
All this contraption sits at the center of a layout with a complex gradient background, so I cannot just cheat the border imageview and fill the outside part with color similar to the underlying parent background.
My initial idea is to use some image editing tool to fill the inside and outside of the png with different colors (green inside, black outside), place the image behind it, and perform some pixel magic with the resulting bitmap such that all black pixels outside the border will "punch through" the whole canvas thus showing the parent background (the complex gradient background), and all green pixels inside the border will become transparent and reveal the photo behind it.
This sounds somewhat too complicated for a simple-looking goal so I'm postponing the implementation (not even sure if it's possible) until I'm convinced that:
A. There is no existing library I can use with the same effect
B. There is a better solution that results to the same effect, but is a lot less complex.
Got it to work using Bitmap.setPixel
public static Bitmap combine(Bitmap b1, Bitmap b2) {
try {
int maxWidth = b1.getWidth();
int maxHeight = b1.getHeight();
Bitmap bmOverlay = Bitmap.createBitmap(maxWidth, maxHeight, b1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(b2, 0, 0, null);
canvas.drawBitmap(b1, 0, 0, null);
for (int index = 0; index < bmOverlay.getWidth(); index++) {
for (int index2 = 0; index2 < bmOverlay.getHeight(); index2++) {
if (bmOverlay.getPixel(index, index2) == Color.rgb(0, 255, 0)) { // or whatever outside color you set on your png
bmOverlay.setPixel(index, index2, Color.TRANSPARENT);
}
}
}
return bmOverlay;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
The trick is to leave the inside of the border as transparent, then fill the outside with a solid color (such as #00ff00) with a tolerance of 0. Won't work as well with shadowed or alpha gradient borders, but should be fine for solid colors.
Usage:
Bitmap b1 = BitmapFactory.decodeResource(getResources(), R.drawable.profile_pic_border_alt);
Bitmap b2 = BitmapFactory.decodeResource(getResources(), R.drawable.panggap);
b2 = ThumbnailUtils.extractThumbnail(panggap, border.getWidth(), border.getHeight());
Bitmap b = combine(b1, b2);
imageView.setImageBitmap(b);
Try to using ColorFilter to do that: http://developer.android.com/intl/es/reference/android/graphics/ColorFilter.html
In this discussion you should find what you want: Applying ColorFilter to ImageView with ShapedDrawable
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));