Android Image animation. Picture reveal from top to bottom - android

I want to display an image, but this image will reveal itself from top to bottom slowly. You can imagine that if this image is a text, then this text will be revealed line by line. And each line is revealed by the fade effect. Is there anyway to do this ? I don't even know which term should I search for.

There is one way I know to do that and it is just simple and it's by using the class called TransitionManager.
Here's a simple code you can put on your onCreate and test it.
final ImageView image = new ImageView(this);
image.setImageResource(R.mipmap.ic_launcher);
// Set the initial position of the image
RelativeLayout.LayoutParams imageDetails = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
imageDetails.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
imageDetails.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
final ViewGroup layout = (ViewGroup)findViewById(R.id.mainLayout);
layout.setOnTouchListener(new ViewGroup.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
// Use the default transition of TransitionManager
TransitionManager.beginDelayedTransition(layout);
// Set the final position of the image
RelativeLayout.LayoutParams imageDetails = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
imageDetails.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
imageDetails.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
image.setLayoutParams(imageDetails);
return true;
}
});
layout.addView(image, imageDetails);
There is one disadvantage that I know when using this approach. The minimum SDK version should be 19.
PS: If you want more control on the transitions or animations you make. Just read further on animations and transitions on Android.

OK, if you want to move the view from top to down, you could just use an animator to animate that. If you want to Reveal the image from top to bottom, then you can do that with a trick. Have an image view and load your image in it. Now have another view on top of it. All you have to is animate the height of that view until, the top coincides with the bottom. You will have your reveal animation.
If you want to create the reveal with fade effect, then you can mask the view on top and animate the mask. I used this in the past to create the circular reveal. For the mask view take a look at this : https://github.com/christophesmet/android_maskable_layout

Related

New layout sliding in from bottom pushing the entire layout upwards

I am trying to create a screen, on some operation it is required to slide a layout from bottom to the height to match it's length, with a translation animation kind of slide.
Have a look at the screen example:
As you can see the initial screen layout, the new view 5 slides in from bottom, pushing the entire layout upwards with an animation.
I have tried this: https://stackoverflow.com/a/19766034/1085742
Using the above link, the new view is visible with animation but screen is not sliding down to show the new view 5.
Any idea how to do this!!
Thanks in advance.
I also tested this. Apparently an animation with translateY or Y does not change the layout bounds. In other words, the other views do not move up. Neither when using an Animation nor an Animator.
One solution is to animate the margins, if the animated view is in a layout that supports margins. In Kotlin:
val collapse = ValueAnimator.ofInt(0, -someLayoutAndViews1.height)
collapse.addUpdateListener {
val params = someLayoutAndViews1.layoutParams as ViewGroup.MarginLayoutParams
params.topMargin = it.animatedValue as Int
someLayoutAndViews1.layoutParams = params
}
collapse.start()

Create and remove image at certain coordinates on android

I have an XML Relative layout, with just a few textviews and buttons on it. But I would like to place an image (or sometimes multiple copies of the image) at different x and Y coordinate which is worked out later on.
Unfortunately I can't seem to figure out how to create the ImageView in android (besides XML) and make it appear at the desired coordinate.
Also it would be great if I could help in making it disappear or removing it at a later stage as well.
You can create a ImageView programmatically with ImageView iv = new ImageView(context); Then you have to set the LayoutParameters for the view. If you plan to add the view to a RelativeLayout you must use RelayiveLayout.LayoutParams. So you can have to do the same as you know from xml: add layout rules. See the documentation.
Then overall something like this:
ImageView iv = new ImageView(context);
RelayiveLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// TODO set the params
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
iv.setLayoutParams(params);
relativeLayout.addView(iv);
To hide the imageView you can user imageView.setVisibility(View.GONE)

put a button on the Relative Layout and move it independently

I have a layout that always rotate to the north. I want to put a pointer on it, but pointer should not rotate. how can I do that?
this is my code of Layout:
rll = (RelativeLayout) findViewById(co.iman.R.id.relativeLayout1);
rlllp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlllp.setMargins(190, 200, 200, 0);
and in other method I rotate the layout with:
rll.setRotation(mCurrentDegree);
I want put a pic or button exactlly in center of this layout? and I want to move this button independently.
Try making another RelativeLayout inside your first one, and do something like this:
rll.setRotation(mCurrentDegree);
rll2.setRotation(-mCurrentDegree);
Now the inside layout should be set to -10 degrees when its parent is set to 10, so it will look like it is not moving at all. Hope that helps!

Android TranslateAnimation Overwrites Other Layout Views

I have a RelativeLayout (vertical) defined to include 3 Views. The top and bottom views are 16 pixels high, and display text. The middle view displays graphics for a game application.
At various points during game play, the app runs a TranslateAnimation that effectively "scrolls" the middle view up or down. This keeps the game character in the game "centered" in the middle view.
My problem is that when the animation runs, the translation causes the top or bottom views to be overwritten. This lasts only for the duration of the animation. When the animcation completes, everything is fine and the top/bottom views display their text as expected. What is surprizing to me is that the translation animation displays content outside of the (middle) view in which it is running. My desire is that when the translation slides the display up/down, that the animation only affects the middle view and does not obscure content in the top or bottom views.
The following is the code I am using to create the layout. The custom views derive directly from View and display content in their onDraw(Canvas) methods.
RelativeLayout rl = new RelativeLayout(this);
topView = new MyTextView(this);
topView.setId(1);
middleView = new MyGameView(this);
middleView.setId(2);
bottomView = new MyTextView(this);
bottomView.setId(3);
final int textSize = 16;
RelativeLayout.LayoutParams lpTop = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, textSize);
lpTop.addRule(RelativeLayout.ALIGN_PARENT_TOP);
RelativeLayout.LayoutParams lpBottom = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, textSize);
lpBottom.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
RelativeLayout.LayoutParams lpMiddle = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
lpMiddle.addRule(RelativeLayout.BELOW, 1);
lpMiddle.addRule(RelativeLayout.ABOVE, 3);
rl.addView(topView, lpTop);
rl.addView(middleView, lpMiddle);
rl.addView(bottomView, lpBottom);
setContentView(rl);
The code in middleView that launches the animation is as follows:
TranslateAnimation ta = new TranslateAnimation(0, 0, 0, yDistance);
ta.setDuration(500);
startAnimation(ta);
I have tried a number of things to get this animation to behave as desired, but have been unable to do so. Hopefully someone out there can help. Thanks in advance.

How to draw two images on the bottom right corner of the screen programatically? (without using XML files)

i need to draw two imageviews, (+) and (-) symbols, on the bottom right corner of the screen, similar of the zoom objects from googlemaps.
I need to do it programatically, with Java, and without using XML files.
I'm trying to do with relativelayout, but i dont know how to do it. They must to be on the bottom right corner of the screen, with 5 or 10 pixels of separation between them.
How to do it?
Also will be cool if someone can tell me how to detect when a user has pressed each image.
You can use gravity.
http://developer.android.com/reference/android/widget/RelativeLayout.html#setGravity(int)
this might do it:
image.setGravity(Gravity.RIGHT | Gravity.BOTTOM);
This should work
ImageView plusImage = new ImageView(this);
RelativeLayout.LayoutParams pp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
pp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
pp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
pp.leftMargin = 5;
plusImage.setId(501);
plusImage.setLayoutParams(pp);
plusImage.setImageResource(R.drawable.icon);
ImageView minusImage = new ImageView(this);
RelativeLayout.LayoutParams mp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
mp.addRule(RelativeLayout.LEFT_OF,plusImage.getId());
mp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
mp.rightMargin = 5;
minusImage.setId(502);
minusImage.setLayoutParams(mp);
minusImage.setImageResource(R.drawable.icon);
//Add the images to the outer layout
((RelativeLayout)findViewById(R.id.outerlayout)).addView(plusImage);
((RelativeLayout)findViewById(R.id.outerlayout)).addView(minusImage);

Categories

Resources