Android: How do I make a non-square area of an image clickable? Additionally, some questions about image scaling - android-imageview

My problem is three-fold. I'm going to try to explain as detailed as I can, because I've asked this question before and haven't gotten any clear answers.
What I'm Trying to Do:
I have diagonally shaped images, that I'm trying to make clickable, so I separated them out of the background image. They were originally one image, but I've cut out several pieces of the image and I want to align them identically to how they were in the original image. This might not the best method for what I am trying to achieve, and if there is a better way to make non-square areas of an image clickable, that would solve all of these problems. If not, here is my problem. The problem is, as soon as I align them in XML, I boot them up on an Android emulator or my phone, and they are aligned differently on both, differently than the preview and from each other.
Below is my example, and I'll try to explain what I mean.
The black and white grid is my background. The brown, red, and yellow lines are separate images. I don't want to just combine them with the background, because I need those lines to be clickable. I'd set a button behind them, but they are diagonally shaped and I need the entire shape to be clickable. However, they have to be EXACTLY where they are in the background, aligned specifically as they are to the background. The problem is, as soon as I load it on another device, it scales, and everything misaligns.
It also misaligns depending the device, so I presume it's because it's scaling the different parts differently.
I need a way to align it the way I want it, and have each image scale together.
How I've Been Trying This So Far:
Initially, I tried just using XML. And herein was my first problem. My background image is 1920x1080. It loads on the emulator without a problem. However, if I try to load it on my phone, I get the following error:
OpenGLRenderer: Bitmap too large to be uploaded into a texture (5760x3240, max=4096x4096)
So that leads to my first question. Why is my 1920x1080 trying to load as 5760x3240?!
I bypassed this, by using Picasso to scale the image to 1920x1080. As a sidenote, in addition to programmatically loading the image, I also am removing the title bar. Here is my OnCreate:
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main_layout);
Picasso.with(this).load(R.drawable.backgroundimage).resize(1920,1080).into((ImageView)findViewById(R.id.background));
On the XML side, I've tried all sorts of different things. Relative layouts, linear layouts, frame layouts...None seem to scale the separate imageviews the same.
Which leads to my second question.
How do I scale two differently sized imageviews together, so that they maintain their alignment on different sized devices?
My second imageview is 198x547(this would be the equivalent of one of the squiggly lines in the example images) and even setting that in Picasso does not maintain my alignment. In fact, that doesn't seem to work at all, since I have to scale it differently to even match the alignment I have in the original image.
Summary Questions
I need a diagonally shaped portion of an image to be clickable. Is cutting the shape out as a separate image the appropriate method for doing so? If not, how do I make such a specific, non-square area of an image clickable?
I need specific alignments to scale together. Why aren't they?
Why does my png file upscale itself?

Related

Positioning items dynamically for different screensizes

Good morning!
I'm currently trying to create a view similar to this mockup.
I have put down 3 different screen sizes so you can see the issue.
I have a header background image (grey box) with an angled bottom. On the right I want to display an image, which obviously needs to be positioned.
Positioning it horizontally is no issue but how can I position the image vertically? I have it positioned fixed for one screen size but obviously need to make it flexible.
Any ideas? Help would be much appreciated!
David
You can definitely use measure as #rajesh pointed out, or you can use Dimensions. As far as getting the layout consistent across devices, using position absolute and measuring the device height should allow you to get consistency across these devices.
Check out this example I set up, it should be a good starting point at least.
https://rnplay.org/apps/pSzCKg

Better way of defining several clickable area in a single large image in Corona SDK

I have a large image background with several areas or objects in the images which can be clicked and an event is triggered. The method I'm currently using to accomplish this is by slicing the images of objects and area and positioning them over the background image and assigning a click handler to them.
This works for the moment but I feel that there should be a better way of doing this. One way I thought and tried is to fill the sliced images with black or white, position them over the background image, make their opacity 0, make them hit testable and assign a click handler.
Does this method have advantage over the previous one? Does making an image object transparent use less texture memory or is it the same?
And are there other better ways to do so? My main objective is about making the game use less texture memory and cutting the overall project file size by using less of those sliced images.
Use display.newRect/newCircle to mark down area, make them transparent and hit testable.
This should be more efficient than using images.

Is there a way to take an image file and make its background transparent via VB .NET?

We have a system where people are being taken a face shot via a DSLR camera. We need the people's images with transparent background. What we're currently doing is taking the image and editing and cropping it in Photoshop, removing the background image with the Magic Eraser tool.
What I am looking for is a way to parse the image and automatically erase the semi-white background we have, along with the resizing and cropping. Is there some kind of library or code sample that does this without requiring manual intervention?
This is a real complex problem. Like the answer below suggested you'll need to do a fuzzy match on each pixel and set it to be transparent but you also need to detected other nearby pixels to make sure they are not close in color. A white tag on the shirt, white eyelids, hair, pale skin reflecting the flash. All are candidates to be removed by any greedy fuzzy logic.
Think about the Magic Wand tool in Photoshop. How good is it at detecting the edges of the person in the picture? Yeah, and that's the top standard of image editing software with thousands of engineering hours behind it.
This is not a feasible request for a Q&A format, and this is one of those things that humans just do better than machine. BUT, that doesn't mean it's not possible, and who knows, you might be the one to do it. Just don't do it in VB.NET please :)
Some pseudo-code to get an idea of what you need to do:
Bitmap faceShot = Bitmap.FromFile(filepath)
foreach pixel in faceShot
//the following line is where the magic happens, you can do any fuzzy match on the color that suits you
//figure out your color range and do a fuzzy match percentage wise
if (pixel between RGB(255,255,255) and RGB(250,235,215)) //white and antique white
pixel.setAlpha=0
endif
end foreach
You could start with this as a starting point for processing a single image,
http://www.java2s.com/Code/VB/2D/ProcessanImageinvertPixel.htm
Basically, if you have a constant background color (like the TV green-screen), it's just a matter of selecting pixels close to the color you are erasing and setting their Alpha level to 0 (transparent). Treating the RGB values like XYZ coordinates, you can do a 3d distance from your background color, and make everything within a certain threshold transparent.
As an improvement, you could also make everything within another threshold semi-transparent so the edges right around hair and stuff like that look softer and less harsh.
Alternatively, you could probably do the same exact thing with good results in Photoshop, as it should support batch processing.
Edit, thinking about it some more, you may want to use a green screen type background as well instead of an off-white one like you stated, as you may make people's eyes transparent. I would definitely try to batch it in Photoshop/Gimp/etc.

How do i set the borders of an mplot3d, and also make the grid lines lie behind the plot?

I have this plot as part of a PySide program;
And there are two problems I have with it. The first is the ugly grey border. I know I can can get rid of it using the toolbar option, but I can't find a way to it programatically, or make it default to that when it plots...
The second issue, is that it is drawing the grid lines on top of the surface, which I would rather it didn't do... How do I get the grid lines to be drawn underneath the surface?
EDIT:
i'm using version 1.1.1;
this doesn't happen for all plot types - i.e.
that is fine.
If i try and plot multiple objects, then it can be a problem;
but i understand that's a limitation of mplot3d not being a try 3D engine, just a set of 3D images with a Z-Ordering, so the order of objects drawn becomes orientation dependant. (same graph - different angle: enter link description here).
The grid lines should surely always be at the bottom of the drawing though, no? Is there a way to force them to be?
Will.

How to add a shadow to an UIImageView which fits the shape of the image content but with some rotation and shift effect

I have been looking for the solution on the web for a long time. Most tutorials are fairly simple about adding shadow to a UIView. I also noticed that if we add a shadow to an UIImageView. The shadow shape could perfectly fit the shape of the content image if the image itself has alpha channel in it. Say for example, if the image is an animal with transparent background, the shadow shape is also the same as that animal (not a rectangle shadow as same as UIImageView frame).
But these are not enough. What I need to do is to add some changes to the shadow so it may have some rotation angle and compressed (squeezed or shift) effect so that looks like the sunlight comes from a certain spot.
To demonstrate what I need, I upload 2 images below, which I captured from the Google Map App created by Apple. You can imagine the Annotation Pin is an image which has the Pin shape, so the shadow is also "pin shaped", but it is not simply "offset" with a CGSize, you can see the top of the shadow is shifted right about 35 degrees and slightly squeezed the height.
When we tap and hold and pin, the shadow is also animated away from the pin, so I believe that such shadow can be made programmably.
The best shadow tutorial I can found so far is http://nachbaur.com/blog/fun-shadow-effects-using-custom-calayer-shadowpaths But unfortunately, that cannot make this effect.
If anyone know the answer or know any better words to search for, please let me know. Thank you.
(Please note that the shape of the image is dynamic in the App, so using any tool like Photoshop to pre-render the shadow is not an option.)
In order to create dynamic effects like this, you have to use Core Graphics. It's incredibly powerful once you know how to use it. Basically you need to set a skew transform on the context, set up a shadow and draw the image. You will probably have to use transparency layers as well.
It doesn't sound like you can use CALayer shadows, since that is meant to solve a specific use-case. The approach Apple takes with the pin marks on the map is to have two separate images that are created ahead of time (e.g. in Photoshop) and they position them within the map relative to a reference point.
If you really do need to do this at run-time, it should still be possible by using either Core Graphics or ImageKit. To get a blurred shadow appearance, you can use the kCICategoryBlur CIFilter. You can then convert the image to grayscale. And to get that compressed look you just need to resize and skew the image.
Once you have two separate images, you can either take the CGImageRef for the shadow image and can set that as the content of another sublayer, or you can add it as a separate view.
If you know what all the shapes are, you could just render a shadow image in Photoshop or something.