I don't know how to create a drawable and add it to toolbar like the image above (red circle). The number is corresponding to today's date.
If you have any ideas, please help me.
UPDATE:
Thanks for your ideas. This what I come up with:
Create my custom Drawable (eg: TextDrawable extends Drawable)
Override the draw(Canvas canvas) method
Programmatically add my custom Drawable to my Toolbar:
inside: onCreateOptionsMenu(Menu menu)
menu.add(0, 0, 1, getString(R.string.jump_to_today)).setIcon(new TextDrawable("22")).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
For now, it's display a text date like I want (haven't has the calendar background). I think I will have to write it on my draw method.
Thanks for your help!
Trying writing text on an image...
Firstly, you need a calendar image without text ("22" in this case). Then, you write text "22" on the image. Something like this:
Bitmap calendarBitmap = ... // Load your calendar image here
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(10);
canvas.drawText("22", x, y, paint);
Which x, y are the coordinates of the text's position on the image. You also need to change Color.BLACK to the image's color for consistency.
Finally, you get calendarBitmap as a result.
I think the best way is to get the image you want as the calendar (maybe you can find it inside android resources). Then transform it into a nine patch and use it as the background drawable of your TextView.
You can read google documentation about creating nine patch. You will be able to define in wich region the text should be by defining the content area.
Related
I have a question. I'm trying to make a simple file manager, and when the user clicks on an item, that item is supposed to be tinted to look more orangeish (or any other color I specify). I was looking at the Paint's .setColorFilter(ColorFilter filter) method, and I wanted to use it to set the color I want the image to be tinted in and then later call drawBitmap(imagepath, x, y, p <--my Paint class with a color filter) method to display the Bitmap with a different color. The problem is that I have looked at the ColorFilter class (the one I'm supposed to pass to setColorFilter()) and it has an empty constructor, and only one method, which doesn't at all do what the class's name suggests it does.
Can someone give me some directions?
Thanks in advance.
Links:
Paint,
ColorFilter
Here it is:
Paint p = new Paint();
p.setColorFilter(new PorterDuffColorFilter(Color.parseColor("#B3B3B3"), PorterDuff.Mode.DARKEN));
Just put the hex color value for the color you want and then select a PorterDuff mode.
Have fun!
I am building a drawing app and would like to create custom brushes. I assumed there was a way to set your android.graphics.Paint object to use a bitmap along the path instead of a color, but this doesn't seem to be the case.. I suppose i could just save the path and draw bitmaps to it, but that seems excessive.. before i start trying to come up with a custom solution i was wondering if anyone has already tackled this. The idea is lets say i had a 'star' icon, id like to be able to draw a line of stars based on stroke width to the path the user has drawn..
Psuedo code:
drawPath = new Path();
drawPaint = new Paint();
//drawPaint.setColor(paintColor);
drawPaint.setBitmap(bitmap); /// <-- wouldn't that be sweet
Nevermind.. i found it.. i knew i could do this..
drawPaint.setShader(new BitmapShader(bm, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT));
I want to make in my app possibilty to draw circles by user. The drawing is quite simple - user just press on canvas somewhere and then predefined circle
The difficult part here is to draw it with some drawable (picture) as a fill. It is quite simple when it is about rectangle. Then you just need to write:
Drawable drawable = getResources().getDrawable(R.drawable.my_background_picture);
drawable.setBounds(myRectangle);
drawable.draw(myCanvas);
Everything is done on onDraw() method of my custom view.
Unfortunatelly there isn't such simple method to make it with circle. The one that I've found is slight modification from Vogella's tutorial:
InputStream resource = getResources().openRawResource(R.drawable.sand);
Bitmap bitmap = BitmapFactory.decodeStream(resource);
BitmapShader shader;
shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
myCanvas.drawRoundRect(myRectangle, 120, 120, paint);
At first sight it looks ok, but it's not. This commands make just something like frame on a picture below, so you move hollow circle on a picture and that's all. Not as with rectangle where you actually move rectangular bitmap.
So, my question is - Is there a way to make circle drawable that can be also moved/resized?
Why make a drawable? You can easily draw a circle via the canvas.drawCircle command. You can also easily make one via a Path object.
Edit:
If you need a drawable, try making a ShapeDrawable based off an OvalShape.
I want to add a text on the picture taken from device camera. So I am launching my activity once Camera intent action is fired. My question is how to show a text on the picture and once user clicks on save button the picture will be saved in device's gallery with the text.Please give some idea.
Thanks
Following Snippet will give you some pointers
Here come your logic.
Here i am loading png file from drawable resource.You need to fetch it from directory.
If you want to make it more attractive Use Custom fonts by saving them in Assets/fonts
Don't directly hard-code Text you want to put rather load it from somewhere.
Use some different color.
I have manually set the Text Size .You should put some logic (Dont hard-code it)
In drawText i have harddoded Text height as 30.You should apply some logic to get Text Height.
Finally You have Background Bitmap save it.
ImageView imageView = (ImageView) findViewById(R.id.image);
Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.sample).copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(background);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
paint.setTextSize(50);
paint.setShadowLayer(2.0f, 1.0f, 1.0f, Color.BLACK);
canvas.drawText("Hello Image", (background.getWidth() - paint.measureText("Hello Image")) / 2, background.getHeight() - 30, paint);
imageView.setImageBitmap(background);
I think this post really helps you Link is here...
You should make use of FrameLayout and then add your imageView to it in this post given example how to set image just add textview with background transparent after it , FrameLayout displays it upon image and you can also position it(just write your textview in RelativeLayout )
Hope this explanation works for you...
I have this image that comes back from an API, which represents the users avatar:
However, my graphics department has designed the app to mask the image to make it look like this at runtime (to match our existing design of sharp edges, etc):
Notice the small edge cutout on the bottom left?
I'd love to be able to create a custom ImageView that handled this for me. Unfortunately I'm not sure how to go about doing that. How can I create the bottom image in a custom ImageView. Is this possible? Do I mask it? If so, how?
Thanks!
Using Path and xfer modes to draw on canvas can do the trick. Check this answer how to draw pic to Closed curve area
I think the easiest way to do is to use 2 ImageViews, one with the photo and other above it with a mask for the photo, in your case it would be all transparent except the bottom left to create the cutout with the background color.
You may be able to use android.graphics.Path to draw the complex shape you want. I found this very helpful for a simple custom View, but it seems like you can do a lot with it:
http://developer.android.com/reference/android/graphics/Path.html
Simple code sample for a shaded rectangle:
private Path mRectanglePath;
...
// draw the path
mRectanglePath = new Path();
mRectanglePath.addRect(mLeft, mTop, mRight, mBottom, Path.Direction.CW);
// draw the fill
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setAlpha(64);
canvas.drawPath(mRectanglePath, paint);