SeekBar: change apperarance in code with drawable rectangles - android

I am a beginner at android, but hoping to release an app shortly.
My problem is, that I would like to have a seekbar with colored areas according to user/database input.
I have tried some things with ShapeDrawable but I am comletely lost and the only result I got was making the seekbar completely black.
I am not allowed to post images, but here is link to a photoshopped image of what I am looking for:
http://www.burninglobster.com/device-2012-10-29-155734ps.png

You can manually draw to bitmap via canvas according to your input data.
Bitmap.Config bmpConfig = Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(seekbar.getWidth(), seekbar.getHeight(), bmpConfig);
Canvas canvas = new Canvas(this.bmp);
canvas.drawRect(...); // draw whatever you need
seekbar.setBackgroundDrawable(new BitmapDrawable(bmp));
Hope this helps.

Related

How to make view.getDrawingCache() preserving transparency

If I have a view with a transparent background and I do bitmap = view.getDrawingCache();, that bitmap is unfortunately no more transparent. A black background is set in background.
I have even tried
view.setDrawingCacheBackgroundColor(Color.TRANSPARENT);
without success.
Actually this method allows to set the background color without any alpha support, Color.TRANSPARENT which is 0x00000000 is actually black if you don't care about the alpha part...
If I use Color.RED, the background is indeed very red.
Any idea to make this work? Is this a limitation of current Android API? Can I use draw() instead? but it's less performant that this view.getDrawingCache() I suppose (no cache)?
Thanks
I also get image with black background by calling getDrawingCache() method. Actually, the image has transparent background. Mistake is saving the image in jpeg format, so the gallery app shows transparent pixels in black color.
If you put it as an overlay, it'll work perfectly. There is no issue here.
Draw caching is somewhat of a relic of pre-HW-accelerated Android, so some stuff might be a bit confusing/not as well documented.
Transparency should work just fine as long as you leave out View#setDrawingCacheBackgroundColor(int) as this might cause the cache to drop down to 16 bit color space (see View#mDrawingCacheBackgroundColor).
This code
view.setDrawingCacheEnabled(true);
// wait for first layout
...
Bitmap b = view.getDrawingCache();
should give you an ARGB_8888 bitmap with transparent background. (You can also this in the Android Studio debugger by settings a breakpoint after the get call and 'View Bitmap' on the variable.
Try this code after setting the layout background color transparent in XML file
layout.setDrawingCacheEnabled(true);
Bitmap bmp = layout.getDrawingCache();
File mFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "smiley1.png");
FileOutputStream outStream;
outStream = new FileOutputStream(mFile);
bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);

drawBitmap() of canvas gives white image on parent image android

I'm newbie with canvas.
I'm trying to draw child bitmap on parent bitmap using canvas.drawbitmap(childbitmap,matrix,point) method. I'm getting number of bitmaps using loop and trying to overlay image on all that bitmaps But somehow the output which I got haven't original child image. It looks like white image. so I'm able to seen child image on parent one but don't know why it looks like this?
Let me put my code over here with output image.
Canvas mCanvas = new Canvas(bitmap);
Paint p = new Paint();
Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(),
R.drawable.ic_launcher);
mCanvas.drawBitmap(icon,0,0,p);
This above code will run with loop and it will give multiple "bitmap" object on which I have to draw canvas. So now I'm taking launcher icon as child bitmap.while checking final output it shows like below image :
Check the white image instead of launcher icon.So what's wrong with my code?
Waiting for your best suggestion ASAP.
You are drawing your bitmap on top with the current canvas. So you have to call mCanvas.drawBitmap a second time to draw the foreground bitmap.

Drawing scaled bitmaps on a SurfaceView -- no antialiasing

I'm sorry if this topic has been brought before, but all my searches on the web and google groups did not help me.
I'm currently developing a little game with the Android SDK, and use hi-res bitmaps that I resize accordingly to match the device's resolution (letting the system do it for me is
not "crisp" enough).
I use a SurfaceView, on which I paint in one pass a canvas filling the whole surface. The paint uses setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)) to allow masking.
Beforehand, I retrieve various bitmaps -- which are resized at initialization with createScaledBitmap() and put in a cache -- and I apply the bitmaps with a paint on this canvas, before drawing this canvas on the SurfaceView.
My problem is, whatever I try, whatever paint settings I use (dithering, antialias, etc..), the resized bitmaps are not antialiased and the drawing present jagged edges. I tried everything.
The only little success I had was using inSampleSize to approach the desired scaled size and force a first pass of antialiasing, before invoking createScaledBitmap on the retrieved
hi-res bitmap, but it is not beautiful enough. I just can't allow to create multitudes of pre-sized bitmaps for every combination of resolution. What did I miss ?
Thanks a lot in advance
First when you load your bitmap you make sure that you don't lose any image quality by settings options to argb_8888:
Options options = new Options();
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap pic = BitmapFactory.decodeResource(getResources(), R.id.pic, options);
When you scale the bitmap turn on the filter:
pic = Bitmap.createScaledBitmap(pic, screenW, screenH, true);
However if one streaches the image too much inevitably it degrades in quality.
When you use paint you can improve quality but lose on speed with turning on ditherig and filtering:
Paint paint = new Paint();
paint.setFlags(Paint.DITHER_FLAG);
paint.setFilterBitmap(true);
Finally the entire activity window could be set on argb_4444 instead on argb_8888 (OS < 2.3). You can chage this if you instert this line before setContentView:
getWindow().setFormat(PixelFormat.RGBA_8888);
If it comes down to it, you can manually antialias without too much trouble. Just apply a simple lowpass filter (something like an NxN average) to the pixel data before asking the bitmap object to rescale itself.
you may clear canvas buffer by youself! such as follows:
canvas.drawColor(Color.TRANSPARENT, android.graphics.PorterDuff.Mode.CLEAR);

Android: how to simply draw a bitmap on another bitmap

I'm new to Android dev and I'm having a hard time trying to do something which seems obvious to me: drawing little images on top of a bigger image.
Let's say that I have a 500x500 image and I want to draw icons at different locations. Icons are png files that I load with:
Bitmap img =
BitmapFactory.decodeResource(getResources(),
R.drawable.idIcon1)
My "background image" is a LayerDrawable.
Then, I am totally lost... Do I have to create a canvas ? How to draw on my "background image" my icons at different positions?
int positionLeft=0;
int positionTop=0;
Bitmap newBitmap =Bitmap.createBitmap(backgroundBitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawBitmap(backgroundBitmap, positionLeft, positionTop,null);
positionLeft=100;
positionTop=100;
canvas.drawBitmap(iconBitmap,positionLeft,positionTop,null);
imageView.setImageBitmap(newBitmap);
You're making simple things difficult. Just use a layout with android:background attribute, and then add ImageViews dynamically with the necessary bitmaps inside.

Tiling a Bitmap on a Canvas

I would like to create a 'graph paper' look to the Bitmap I am drawing via a Canvas, and trying to figure out the best way to do this.
I can't pass a source Bitmap containing the graph paper background to the Canvas constructor, as I am getting the Canvas in a SurfaceView via the .lockCanvas() call.
Some solutions I've tried:
I've tried implementing this solution in my SurfaceView's Thread.run(), but the issue I believe is when the BitmapDrawable is converted to a Bitmap... it loses the tiling properties.
canvas = mSurfaceHolder.lockCanvas(null);
BitmapDrawable TileMe = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.editor_graph));
TileMe.setTileModeX(Shader.TileMode.REPEAT);
TileMe.setTileModeY(Shader.TileMode.REPEAT);
Bitmap b = TileMe.getBitmap();
canvas.drawBitmap(b, 0, 0, null);
If I use the Canvas.drawBitmap method that takes a destination RectF as a parameter, it looks like the bitmap will be tiled to fill the RectF... but how do I declare a RectF reliably that fills the entire view area?
Setting the Activities background to the desired graph paper look also doesn't work, as the bitmap/canvas layout is opaque and blocks that from being seen.
Any ideas how to achieve this?
You have two easy solutions:
Either use a BitmapDrawable, but instead of extracting the Bitmap, just call BitmapDrawable.draw(Canvas). Don't forget to set the drawable's bounds to fill your drawing area.
Create a Paint with a BitmapShader and draw a rectangle with it (this is basically what BitmapDrawable does).
I'm sure there is a way to get a tiled effect using a SurfaceView. Unfortunately, it looks like you can't use the BitmapDrawable with a canvas. So you would probably have to implement you own custom tiling method by creating your own series of Rect's on the Canvas and drawing a scaled bitmap to each one.
It honestly wouldn't be that hard. Just get the width/height of the view, and create an array of Rect's based on this data that you will draw the Bitmap to.
Alternatively, if you don't need to make modifications to the actual tiled background on the fly, just draw it as a background and draw the SurfaceView on top of it. That post you linked provided multiple solutions to tiling a BitmapDrawable that you could implement.

Categories

Resources