Can an View play its animation out of its parent? - java

I have some ImageViews in a TableLayout's TableRow. When I start an animation of one ImageView using the following code:
ImageView image = (ImageView)findViewById(id);
TranslateAnimation a = new TranslateAnimation(0, 0, 0, 80);
a.setDuration(500);
image.startAnimation(a);
the image is expected to move downwards. But unfortunately it's in a TableRow, so I can see the image moves "in the backstage of the row". Any ideas? I can't use other layouts because of a lot of existing codes.

The answer is no. Animations change properties of an view, but can't take it out of its parent. At last I gave up and use another layout.

Related

ImageView onClick animation doesn't work after change in resource

I'm new to android, and I am currently working on a simple connect three game, I have nine ImageViews which contain transparent images, when the ImageView is clicked, the resource is changed to x or o.
I've tried adding more animation, and setting resource to null rather than a transparent image, but it didn't work, only restarting the activity seems to fix it.
public void oneClick (View view)
{
//image view onClick
ImageView one = (ImageView) findViewById(R.id.one);
one.setImageResource(R.drawable.x); //setting x or y
one.animate().rotation(180).setDuration(500); //animation
}
Here's how I reset the images,
one.setImageResource(R.drawable.transp)
After this, if the onClick activity is called again, the image is set.
However the animation doesn't seem to work.
What am I doing wrong?
My image was rotated 180, so it wasn't rotating again, the simple solution was to reset the orentation of the image then run the rotate animation again, which seems to have fixed it!
clicked.setImageResource(R.drawable.x);
clicked.animate().rotation(180).setDuration(500);
after this
clicked.animate().rotation(0);
now if I use the rotation animation again, it seems to work!
This is a simple fix. Simply add .start() to your animation. It should look like this afterward:
one.animate().rotation(180).setDuration(500).start();

How to define a clickable area in an image used in ImageView

Is there a way to define a clickable area in an image? Something equivalent or similar to map-area tags in HTML? Or is there a standard way of doing this sort of thing?
A possible method
1) get the width, and height of the image view using (in pixels)
2) get the coordinates of the image view (which is the top left corner of the image view)
3) create a touch listener, and record the coordinates of the users touch
4) if the user's touch coordinates fall on the image do whatever you want to
If you only want to do certain things based on where the user touches you will have to calculate the coordinates of those regions using the coordinates of the image view, and the image views width and height.
I think there might be no standard way for this, but you can achieve it by placing a transparent VIEW over your imageview. Set click listener to that view and make imageview clickable false.
When i have to do something similar, i use an include view.
As for the layout the include view will be accessing, i would use FrameLayout as root.
Its children would be the ImageView that i want to depict, along with a LinearLayout infront of it containing Buttons at the positions i want the user to choose from.
Then in code, i set listeners to these buttons.
Lastly, i would set the buttons so that there background is transparent and text is not available.
For example in a project i am working on, i have an ImageView with an Image of Ocean as background.
In front of the view i have a LinearLayout with smaller ImageViews with images of islands.
I place listener on the islands to see when they are clicked to open a new activity.

Display the modified image of an ImageView object

I've been using an ImageView object to try and modify an Image and then draw it. However I could only make the two following outcomes happen:
1) After ImageView has been modified, I tried to grab the image out of it for display with getImage() but that only displayed the original Image I had in the first place.
example code:
sheet = new Image("file:sprite_sheet.png");
ImageView iv = new ImageView();
iv.setImage(sheet);
iv.setViewport(new Rectangle2D(0, 0, 32, 32));
g.drawImage(iv.getImage(), 100, 100);//'g' is GraphicsContext2D
2) I managed to get the modified image to show by adding it to the Group obj like so:
root.getChildren().add(iv);
But then it only displayed the image in the top left corner of the screen and I couldn't move it or do anything with it.
I prefer (if possible) to approach this issue with a method more like the first outcome has presented. And just saying this in advance, I'm aware of that that there's another post similar to this atm, but I couldn't get anything useful out of it.
ImageView, as the name implies, is just a view of the data (image in this case). All modifications to the view only affect the view and not the data. There are two general methods for what you want to achieve.
Use scene graph and ImageView object. Perform all visual operations on the view. This is the preferred way as the data stays the same, meaning you can easily create a new ImageView with the original image and modify it in a different way should you wish to. If the parent is Pane, then you can easily move the object by calling setTranslateX/Y.
If you absolutely need to use Canvas, then you will need to modify the data itself or create a copy of the data and modify it instead. You can do this by using a combination of the following: PixelWriter and PixelReader
Example for 1.:
ImageView view = new ImageView(image);
// create root with dimension 800x600
Pane root = new Pane();
root.setPrefSize(800, 600);
// attach view to root so it can be displayed
root.getChildren().add(view);
// translate view to 100, 100
// on the screen this means the image will move 100 in X and 100 in Y
view.setTranslateX(100);
view.setTranslateY(100);

Having trouble with moving a drawable in Android

I'm having trouble with some code I've set up that allows me to move an imageView's drawable to another imageView simply by clicking on the two imageViews (one with source image and then destination imageView) in succession.
The goal is to simulate a chessboard.
My starting display is the following:
And after I click the leftmost pawn then click the square directly in front of it, the result is the following. You'll see that the target imageView (first column 3rd row) is losing its horizontal boundary definition and expanding across the entire tableRow:
Now, I'm also resetting tags along with the drawables in each square but I'd imagine that the issue stems from the way in which I'm getting and setting the drawables.
My code is as follows:
int selectedId = getResources().getIdentifier(selectedSquare, "id", getPackageName());
ImageView sourceSquare = (ImageView) findViewById(selectedId);
Drawable sourceDrawable = sourceSquare.getDrawable();
ImageView targetSquare = (ImageView) findViewById(R.id.s20);
targetSquare.setImageDrawable(sourceDrawable);
My guess is that the sourceDrawable variable I'm creating somehow grabs too much or too little information and when I move that drawable into the new imageView targetSquare this destroys my formatting.
You can give the new ImageView the source LayoutParams of the origin:
targetSquare.setLayoutParams(sourceSquare.getLayoutParams);

Android: Is it possible to draw a view on top of the mapview as an overlay

Actually, the subject is a question.
I want to draw on top of the map a view as overlay, simply my view consist of linear layout with 9patch background and two textview inside. I need to draw those layouts in runtime and they will be linked to geo location
There is no problems to define such view, but it is problematic to create an overlay that will handle such draw...
Yes - this is what a FrameLayout is for! Children in a FrameLayout appear on top of each other, with the last item displayed on top and the first item displayed on the bottom.
Put your MapView as the first child of a FrameLayout and your LinearLayout as the second.
Actually there is no way to do what I want, I've found a way to convert any layout to bitmap, but firstly you need to invoke layout on the root view and define it boundaries, so in my case it is the same to simple drawing on canvas which I've used for my solution. Converting layouts to bitmap is a good thing when your layout is already drawen on the screen, but when you need to draw layout on the canvas from scratch there is no benefits due to simple drawing on canvas.
You could create a drawable out of your view by converting it to a bitmap.
Or you can try to use this. Has the same effect: https://github.com/jgilfelt/android-mapviewballoons#readme
And you can modify it like you want to show whatever view you like

Categories

Resources