How to create a custom ImageView with nonrectangular shape? - java

how can I make custom ImageViews in Android like in the picture which all have non-rectangular shapes? I want to be able to load images into these ImageViews and have the images clipped accordingly so that each image stays in its specified area (1,2,3 4). I would preferrably like to specify the shapes of the ImageViews in the layout if that is at all possible.
I also want touches to be recognized only in the exact areas where the ImageViews are.
If not, is there a way to do it with a canvas where I define these different areas and then clip the images loaded into them programatically?

Related

LIBGDX drawables on devices with different densities/resolutions

I'm new to LIBGDX game development, and I have faced my first problem. I've created 9.patch drawables (buttons) using texture packer. This drawables can be used on low density and also extra high density screens and quality is the same.
If I run my project with that drawable on desktop project the image shown is okay and perfect size. If I run project on low density android device, drawable becomes huge (almost half of the screen). And also If I run project on extra high density android device the button becomes really small.
So my question is, how to handle drawables in LIBGDX, so the ratio (screen:image size), stays the same no matter resolution/density..?
If your button is a text button. Change the font of your text.
If you are using image button, this might help you
It kind of depends on what you're drawing...If you are just drawing an image, I've found it easier to specify a float width and height in the last 2 parameters of the draw method. If you are using a camera with a fixed viewport size, you can simply use a fixed percentage of your camera viewport so it will always be the same dimensions and draw like this:
batch.draw(drawable,x,y,screenwidth * 0.5f,screenheight*0.5f);
However, if you are using buttons or some other widget inside of a table, you should specify the cell size and it should automatically be resized based on the size of the table cell. Something like:
myTable.add(myWidget).width(300).height(200);
Post up exactly what it is that you need to draw if you get a chance and it will be easier to figure out what needs to happen.

How to create a custom Android view? Using XML objects or a drawing API?

I'm trying to create a new view that would:
load an image
allow the user to zoom and rotate the image with two fingers while a "cropping box" stays translucent over the top while the image extends beyond the "cropping box"
with the end goal of being able to mark the rotation and position of the main image (so that later i could crop out or show the area within the cropping box)
I'm wondering if I can do this with an ImageView holding the image in the background and another ImageView holding the crop box on top of it, then using TouchEvents to move the image, would this work?
Or do I need to use some drawing API on a 2D surface?
I'm just really new to creating custom objects in Android that aren't just customized out-of-the-box Views.
What's the standard way of creating something like this?
Thanks!
Create a Compound View by extending RelativeLayout with two overlapping ImageViews in it. Then override onTouch event or override onGestureDetector interface in that newly created View.
Official Documentation: http://developer.android.com/guide/topics/ui/custom-components.html
Another Tutorial: http://www.vogella.com/tutorials/AndroidCustomViews/article.html

Options to put corners in Android ImageView

I'm trying to put corners im my ImageView and I find these "workarounds":
1 - Take the Bitmap soure and paint then
2 - put a second ImageView on my layout and use shape+corners as its source
It's not possible take the image view and put a corner around then?
In my app i have a List with a lot of ImageView (the Bitmap used in this ImageView's is programmatically) and I want to put corners in every ImageView.
There is any other option?
You can create a selector that contains shape with rounded corners, and then apply it as the ImageView's background using xml.
Look at this answers here
You can also do it via code by using PorterDuffXfermode. PorterDuffXfermode uses alpha compositing that will allow you to create and intersection (things of mathematical sets here) between the Canvas you get from onDraw() and an off screen Canvas you will have to create. You will use one of the PorterDuff.Mode flags to tell the framework you want to only render the pixels that intersect from the two Bitmaps in each Canvas.
More on Alpha Compositing

Android UI Design Suggestion

This is the scenario:
I have one image background set on an activity. On this background, a rectangle has been drawn (in other words, one image depicting a rectangle). I need to display text WITHIN this rectangle.
Right now, I have one solution in mind: since I'm going to optimize the UI for most screens (incl. tablets), I'm going to customize the main .xml layout for each screen size (multiple .xml layouts). Thus, I can manually set the place where the text area goes within the rectangle (+ its size).
I am most certain that this solution is NOT good. I'd like to hear some suggestions from more advanced developers. What would an elegant way of placing text over a background image showing a rectangle, so that the text stays within the rectangle's borders be?
Because I need to set particular positions for other UI elements (centered buttons (vertically/horizontally), I am currently using a Relative Layout.
By default if you have used dp as dimensional measure, your app should in theory work fine for all resoultions. Android by default scales the screen to fit different screens. AndroidDeveloper You just have to make sure that you have different images for resources (Rectangle image). There is another post in SO which you might be intrested to look into link
Cheers
RIchie

Android, Imageview, a few questions

I'm wondering..
1)is it possible to set an imageview's resource to the URL location of an image?
2)is there a way of setting the x,y co-ords of where on the screen to draw the imageview?
3)How would I run a check to see if the space is taken up by another Imageview (must specifically be an imageview)
4) How would I make the Imageview "clickable" e.g if the user clicks the image it'll do something?
5) How would I dynamically create imageviews? E.g if a condition is true make another imageview
Perhaps I'm going about this wrong so I'll explain better what I'm looking to do.. basically I want to draw images on the screen which are located at URL's. I want to display N amount of images (There will be conditions which will decide how many images I'm displaying, so it'll have to be dynamically created) each image should take up approx 50x50 screen space. There will be other conditions to where the image should be displayed.. If an image exists at a certain co-ord it shouldn't draw over it, when the user clicks the image something else should happen.
Hopefully this makes things clearer.
Thanks.
Not directly. You can download image afer program was started, then convert it to Drawable and display on screen. But you cannot assume user always enables data transfer, so often image won't be downloaded at all.
Yes, but much better is to use Layouts - different handsets have different screen resolutions and desinty.
No simple answer - it depends, how you prepare and display screen contents.
((ImageView)view.findViewById(R.id.imageid)).setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//something
}
});
It sounds like you don't need ImageView (the graphics component used to place image in various layouts) itself, but rather some kind of canvas. Anyway, see:
How to add 3 images in a canvas in android
Drawing to the canvas

Categories