Android: BitmapDrawable.Draw(Canvas) Doesn't seem to work - android

I am trying to tile a 20x20 background onto my Custom View but for some reason I am unable too.
BitmapDrawable background;
background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.back));
background.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
background.draw(canvas);
Does anyone have an idea why it isn't working?

Don't set the bounds to the size of the tile: set them to the total area to be tiled. In your case:
background.setBounds(0, 0, myView.getWidth(), myView.getHeight());

You forgot to give your drawable bounds. You need to call drawable.setBounds() at least once before drawing it.

I seem to have fixed this problem with the following code
//background
Bitmap _back_bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.background);
BitmapDrawable backTiled = new BitmapDrawable(_back_bmp);
backTiled.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
backTiled.setBounds(0, 0, this.getWidth(), this.getHeight());
this.back_bmp = backTiled.getTileModeX();
this.setBackgroundDrawable(backTiled);
But I have my own problem now. Nothing can be drawn to the canvas?

Related

SeekBar: getting empty bitmap from Drawable until set it to ImageView OR how to get resulting Bitmap from 9patch

So I want to set a Bitmap created from drawable into a SeekBar's progress. And I make it so:
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Drawable drawable = getResources().getDrawable(R.drawable.seekbar_bg_full);
Canvas canvas = new Canvas(bmp);
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas); // I assume here drawable must be drawn but its not
// canvas.drawBitmap(bmp, 0 , 0, null); // does nothing as 4 me
// encode/decode to detach bitmap from 9patch
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 0, baos);
final byte[] bytes = baos.toByteArray();
bmp.recycle();
bmp = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
// ClipDrawable is intented to be used as progressDrawable in SeekBar
ClipDrawable progressDrawable = new ClipDrawable(new BitmapDrawable(getResources(),bmp), Gravity.LEFT, ClipDrawable.HORIZONTAL);
// if not set this drawable to an ImageView then no progress will be shown by SeekBar at all
//ImageView imgFake = (ImageView) findViewById(R.id.fakeImageView);
//imgFake.setImageDrawable(progressDrawable);
mySeekBar.setProgressDrawable(progressDrawable);
width and height are valid values here (like 460 and 30). As you can see there are 2 code lines about ImageView are commented. This ImageView persists on a layout and its visibility is INVISIBLE. If I comment those 2 lines like shown then there will be no visible progress, like drawable is empty or transparent. Looks like this ImageView makes drawable to really draw itself. But I don't like to use a fake ImageView just to make the "magic" happen so the question is - how to make it work without this fake ImageView.
Please don't suggest me ways how to properly set SeekBar progress like:
ClipDrawable progressDrawable = new ClipDrawable(getResources().getDrawable(R.drawable.seekbar_bg_full), Gravity.LEFT, ClipDrawable.HORIZONTAL);
mySeekBar.setProgressDrawable(progressDrawable);
or xml selectors ways or any altrnative ways since I know about it already and my quiestion is not really about it. I just need to make it work my way.
I just need to make my bitmap or canvas or whatever really drawn.
A bit more details if you want (optional to read). The problem is about the drawable seekbar_bg_full - its a 9-patch png. And all need is to obtain a not NinePatchDrawable-linked resulting Bitmap. Assume I have a 460x30px view with 9patch image set as src or background and the 9patch image is stretched just like it should to. So I need to get a Bitmap this view contains and this Bitmap should not be linked somehow to a 9patch. Thats why I encode bitmap to an byte-array and then decode it back - its just to get rid of 9patch. If there is a more esay way to get a resulting bitmap from 9patch (some magic around NinePatchDrawable) - I would like to know about it.
Ok, I figured out how to get rid of fake ImageView and make drawable to draw itself: all I have to do is to call setBounds() method on a drawable:
ClipDrawable progressDrawable = new ClipDrawable(new BitmapDrawable(getResources(),bmp), Gravity.LEFT, ClipDrawable.HORIZONTAL);
progressDrawable.setBounds(0, 0, width, height);
mySeekBar.setProgressDrawable(progressDrawable);
Now I don't have to use ImageView, at last!
However my code is a really long story to get rid of 9patch features in drawable.

Altering the opacity of part of a View

I would like to get the following effect. I have an image on my screen. When a navigation drawer (or another obstructing view) comes over the top of my image, I want to instead of having it cover the image, show the negative of the image.
The pictures below may help to show what I mean.
Any ideas on how to accomplish this? I'd like to get the same effect for the menu icon.
The way I would go about this is:
Enable the drawing cache on the view you want to apply the negative:
view.setDrawingCacheEnabled(true);
Get a snapshot of the view when the drawer is clicked or dragged with :
Bitmap bmp = view.getDrawingCache();
Finally override the onDraw() method of the drawer and draw the bitmap on it. Apply a shader to do the "negative" effect. See: http://chiuki.github.io/android-shaders-filters/#/
canvas.drawColor(Color.WHITE);
BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(R.drawable.your_drawable);
Bitmap bm = bd.getBitmap();
Paint paint = new Paint();
Where the INT ranges from 0 (fully transparent) to 255 (fully opaque).
paint.setAlpha(0);
canvas.drawBitmap(bm, 0, 0, paint);

how to crop top,left,right and bottom portion of imageview in android

In my application i am getting transparent pixels in my Imageview while taking screenshot.How to cut top,left,right and bottom portion of Imageview.I tried below code but not working
Bitmap bitmap = Bitmap.createBitmap(imgView.getWidth(),imgView.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
imgView.draw(canvas);
Bitmap result =Bitmap.createBitmap(bitmap,imgView.getLeft()+10, imgView.getTop()+50, imgView.getWidth()-20, imgView.getHeight()-100);
bitmap.recycle();
please help me to solve this issue.
I am not very sure what are you referring with "not working", but from first sight I would suggest you to change from
Bitmap result =Bitmap.createBitmap(bitmap,imgView.getLeft()+10, imgView.getTop()+50, imgView.getWidth()-20, imgView.getHeight()-100);
to
Bitmap result =Bitmap.createBitmap(bitmap, 10, 50, bitmap.getWidth()-20, bitmap.getHeight()-100);
But I would not recommend you to use this kind of approach which explicitly setting width and height value.

Trouble making parts of a bitmap transparent

I'm developing an android app where i need to capture text and save it as a transparent image. Capturing the text has been done but making a transparent png file is where i'm stuck as i'm not familiar with image pixel manipulation at all. Here's what I have so far... i first create a blank bitmap and fill it with a white background, then i set the paint's transparency to 0 (full transparency) and then draw the source bitmap into the destination bitmap using the XOR modes.. but when i run the app all i see is a blank white image. i'll be glad if someone points out what i'm doing wrong and how to fix it. Thanks in advance.
b = Bitmap.createBitmap(tw, th,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
Rect dest = new Rect(0,0,b.getWidth(),b.getHeight());
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
canvas.drawRect(0, 0, b.getWidth(), b.getHeight(), paint);
paint.setAlpha(0);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));
canvas.drawBitmap(bmp,null,dest,paint);
Have you looked at : How to change a bitmap's opacity?
Seems like
paint.setAlpha(0);
won't do anything as you need to set the alpha channel to something greater than 0...
Use:
Color.argb(0,0,0,0)
The first parameter is the alpha. Set it to 0 for complete transparency.

Android: BitmapDrawable.Draw(Canvas) Doesn't seem to work

I am trying to tile a 20x20 background onto my Custom View but for some reason I am unable too.
BitmapDrawable background;
background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.back));
background.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
background.draw(canvas);
Does anyone have an idea why it isn't working?
You can use BitmapDrawable, but you have to set the bounds first so it knows how much tiling to do:
BitmapDrawable background;
background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(),R.drawable.back));
//in this case, you want to tile the entire view
background.setBounds(0, 0, myView.getWidth(), myView.getHeight());
background.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
background.draw(canvas);
You are probably getting warnings in your log regarding SKImageDecoder failing. If you are creating the resource via xml you need to retreive it via (BitmapDrawable) getResources().getdrawable(id)
You have it backwards. Instead of passing your View's canvas to the bitmap's draw method, draw your Bitmap to the View's canvas using Canvas.drawBitmap

Categories

Resources