Animation hides the image android - android

i used below single line code for animation to my image view in android.
Coding
v.animate().translationXBy(1000).setDuration(5000).start();
but i hides the image. i don't want to hide my image using the above source. any helps are much welcome thanks.

Try this:
v.bringToFront();
Add this before animating.
Hope this helps

how do you want to hide your image?
you can also hide it by set 0 in alpha attribute
v.animate().alpha(0).setDuration(500).start();
EDIT
To move the view right to left without hide it at the end
You can try to use v.animate.x() instead
Set the min X Position, its 0 since you want to move it to left without goes out of screen
int minXPosition = 0;
Set the value how much the view move and calculate the view new X position
int moveXBy = 100;
int newXPosition = (int)view.getX()-moveXBy;
Animate the view, but check the position first -> if its past your minXPosition, use the minXPosition instead
view.animate()
.x(newXPosition<minXPosition?minXPosition:newXPosition)
.setDuration(500)
.start();

Related

How to make the screen fade

I would like the user to be able to click on the screen.
On click, I would like entire screen to turn opaque slowly.
Then I would like to display some buttons on the opaque screen.
What is the best way to achieve this effect?
Thanks.
You could fade in a dialog with an opaque background.
Then just set a custom view that contains your buttons on the dialog.
Here are some options I found
ViewSwticher
CrossFade
edit
I found this method to be ideal for cross-fading layouts.
You can simply change the alpha level of the activity's background in order to give it an opaque effect.
View backgroundimage = findViewById(R.id.my_activity);
Drawable background = backgroundimage.getBackground();
background.setAlpha(50);
To make it appear to "fade out", you could easily implement this into a for loop, allowing the thread to sleep for a short time between intervals to create an animated effect.
Here is a quick design of what this function might look like:
void fadeView(View v, int threshold, int speed) {
Drawable background = v.getBackground();
for (int i = 100; i < threshold; i--) {
background.setAlpha(i);
Thread.sleep(speed);
}
}

How to move a TextView to the bottom of the screen till it's gone?

I need to scroll a TextView to the bottom till it's gone by preforming an animation like translate animation in xml. I tried to create an animation file and used translate attributes in the xml, but what it will do is that it will go down by a specific distance that I set in the xml. I don't know how to let it translate to the bottom till it's gone and disappeared from the screen. One more thing is that I also want something like a listener that will perform some action when that TextView is gone and disappeared from the screen when the animation ends.
Any suggestions will be greatly appreciated. Thanks.
You can calculate the distance you want your view to travel, but if you don't want to be exact, you can just use the screen height:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenHeight = size.y;
And then just animate the view as needed, but add a listener to do what you want at the end:
textView.animate().translationY(screenHeight).setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
textView.setVisibility(View.GONE);
}
});
You still need to make the view visibility GONE at the end in the listener, just to make sure you don't see it anymore.
You can also try and use getBottom() for the distance.

Item getting bigger when sliding ListView

I would like to implement a list effect in android as the one displayed in the Ultravisual Iphone app :
The similar effect can be view on the Expo Milano 2015 app in android.
I would like the top item get bigger when sliding down the ListView.
I have no idea how this can be done... Is it an animation on the first item in the current view?
If someone has an example or a clue to achieve this, it will be great!
Thanks
Well I tried to achieve that effect and it looked like this:
First you need to start defining your max and min font size. I did this:
private final int MAX_FONTSIZE=50;
private final int MIN_FONTSIZE=12;
Next you need to save your screen total height. On your onCreate save it like this:
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
mScreenHeight = size.y;
Then override your listview onScroll event and do something like this:
#Override
public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
if(listview.getChildCount()>0)
for(int i=0;i<listview.getChildCount();i++)
{
float font = Math.min(Math.max(MAX_FONTSIZE - (float)(((MAX_FONTSIZE-MIN_FONTSIZE)/(mScreenHeight*0.3)))*Math.max(listview.getChildAt(i).getTop(),0),MIN_FONTSIZE),MAX_FONTSIZE);
((TextView)listview.getChildAt(i)).setTextSize(font);
}
}
The 0.3 means that at about 30% of your screen it will always be the minimum font size. You can tweak that value for whatever you want. Remember that listview.getChildAt(i) will return the view that you inflate on your adapter. In my case it's just a simple textview, that's why it's safe to cast it to TextView. You might need to call findViewById to get your textview.
You also might need to make the TextView centered so it looks prettier.
EDIT:
Since op want to change the view's size (which should not be used with this code) here is some hack you can do. Start by changing your min/max constants to what you want (100 & 250). Then proceed to create a flag that controls if the listview is scrolling or not. On your onScrollStateChanged add this line isScrolling= i == SCROLL_STATE_TOUCH_SCROLL || i == SCROLL_STATE_FLING; where i is the second parameter of the onScrollStateChanged. Next change the line to if(listview.getChildCount()>0 && isScrolling). The next step is to change the view height. To do this you have to change it's layoutParams.
listview.getChildAt(i).setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Math.round(font * getResources().getDisplayMetrics().density)));
Remember this is, like I said, a simple hack. This is not, by far, the best solution to this. But since it's a complex thing to do let's stick with the basics.

How I check an imageview ontop of another imageview or not

suppose i have two image view v1,v2.
imageview v1,v2;
v1=(Imageview)findviewbyId(R.id.view1);
v2=(Imageview)findviewbyId(R.id.view2);
How do I check that v1 ontop of v2 or not.
You can set onclick listener for both of it so that when the user clicks so that the top of the imageview only gets clicked. By this u can get which imageview is up top.
you may check by getting co-ordinates of the image by using this link
and can check which image is top and which is bottom with that co-ordinates
one suggestiton: get their location and height
minus them and configure if on top of
You can calculate it with the position and the width and height of the View.
Position: Use View.getLocationOnScreen() and/or getLocationInWindow().
Width/height: Use View.getWidth() and View.getHeight().
The view that is defined first in your xml layout will be drawn first. The next view will be drawn on top of the previous view if there is any.
By that principle, you can ask your frame/relative layout to check the ordering where the child views are ordered:
int indexV1 = layout.indexOfChild(v1);
int indexV2 = layout.indexOfChild(v2);
if(indexV1 < indexV2) {
// v1 is below v2
}

How do I get a view position relative to another view in Android?

I'm making a app for Android that shows a camera image (after going through some manipulation) on screen using an ImageView and I want to allow to move other ImageViews on the screen - but only inside the the parent ImageView.
Also, I need to get the coordinates of the child views relative to the coordinates of the parent ImageView. i.e - So that (0,0) will not be the whole screen, but the ImageView that shows the camera.
Is that possible at all? If so - How?
Help is much appreciated! Thanks
If you want to move the views by drag here's a lot of info around, e.g Moving image with touch events .
What about getting the top and left side of minorView relative to majorView it's also straightforward with some plain substraction minorViewRelativeTop = minorViewTop - majorViewTop. Use View.getLocationInWindow() for getting these absolute positions.
N.B. That View.getLocationInWindow() will usually not work just inside Activity.onCreate() so better call it after some delay. For testing it could be sth like:
majorView.postDelayed(new Runnable() {
#Override
public void run() {
int[] location = new int[2];
majorView.getLocationInWindow(location);
int majorViewLeft = location[0];
int majorViewTop = location[1];
}
}, 1000);

Categories

Resources