Getting alpha/opacity of a View in Android - java

How can I get the alpha/opacity of a View after I've animated it?
My fadeIn() is below - fadeOut() is the same with the endpoints switched.
public void fadeIn(View view) {
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(1000);
animation.setFillAfter(true);
view.startAnimation(animation);
}

I would highly recommend you to define the animations in an XML-file. The API doesn't have support for getting a specific alpha.
You can still have in mind that the opacity that your view has, is the last one you've defined; in this case, (your fadeIn() method), your latest opacity value is the value you passed into the AlphaAnimation constructor.

Related

Animating custom view jumping when being shown

I have a custom view which is a constraint layout and a couple of TextView and EditViews.
On my activity I want to show and hide the custom view based on a Switch being on/off. The same custom view is used on multiple Switches for different parts of capturing some data.
The logic in the switch to make the relevant custom VISIBLE or GONE view works without an issue when I just use the CustomViewName.setVisibility(View.VISIBLE) and CustomViewName.setVisibility(View.GONE) - instead of the ShowOrHideView routine below.
I'm trying to make it look better so instead of the custom view just appearing or disappearing it appears by sliding up/down from the switch (height from 0 to it's expected height), or disappears (expected height to 0).
Replacing the CustomViewName.setVisibility in the switch with a call to the function below to show or hide depending on the Switch being on/off:
Switch On to show the custom view (ignore the hard coded sizes for the example here)
ShowOrHideView(CustomViewName,0,100);
Switch Off to hide the custom view:
ShowOrHideView(CustomViewName,100,0);
private void ShowOrHideView(View parView, int parCurrentHeight,int parNewHeight) {
ValueAnimator mySlideAnimator = ValueAnimator
.ofInt(parCurrentHeight, parNewHeight)
.setDuration(2000); /// 2000 for debug only
mySlideAnimator.addUpdateListener(animation1 -> {
Integer value = (Integer) animation1.getAnimatedValue();
parView.getLayoutParams().height = value.intValue();
parView.requestLayout();
});
mySlideAnimator.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(#NonNull Animator parAnimator) {
}
#Override
public void onAnimationEnd(#NonNull Animator parAnimator) {
// If zero at the end of the animation then the view is being hidden
if (parView.getLayoutParams().height == 0) {
parView.setVisibility(View.GONE);
}
}
#Override
public void onAnimationCancel(#NonNull Animator parAnimator) {}
#Override
public void onAnimationRepeat(#NonNull Animator parAnimator) {}
});
if (parCurrentHeight == 0){
parView.setVisibility(View.VISIBLE);
}
AnimatorSet myAnimationSet = new AnimatorSet();
myAnimationSet.setInterpolator(new AccelerateDecelerateInterpolator());
myAnimationSet.play(mySlideAnimator);
myAnimationSet.setStartDelay(1000); // Delayed start for debug only
myAnimationSet.start();
}
The hiding of the custom view works OK with it correctly being set to GONE so the space is no longer there.
But when showing the custom view, the height correctly gets set in the animation, but there's an initial "blip" where the whole custom view gets shown before it then appears as height with 0 up to height of 100.
Any ideas how to make the custom view appear with the height getting larger (to make it appear nicely) and avoid the "blip" where it briefly shows the whole view before the animation? In my example I've temporarily put the setStartDelay(1000) to prove to myself the custom view appears fully when view becomes VISIBLE, then when the animation starts the custom view appears as if the height is 0 to 100
Thanks

How to set ImageView permanently at a position after TranslateAnimation

I referenced the other questions but couldn't find a solution, also I am fairly new to programming.
So I implemented a TranslateAnimation on my ImageView but once the animation ends it returns to its original position. I used Override onAnimationEnd but that doesn't seem to work. Can someone figure out what should I be doing?
public class PackAnimation extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pack_animation);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
String s = getIntent().getStringExtra("CHOICE");
final ImageView pandya = (ImageView) findViewById(R.id.pandya);
final int amountToMoveRight = 600;
final int amountToMoveDown = 0;
TranslateAnimation anim = new TranslateAnimation(0, amountToMoveRight, 0, amountToMoveDown);
anim.setDuration(100);
anim.setAnimationListener(new TranslateAnimation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationRepeat(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation)
{
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)pandya.getLayoutParams();
params.topMargin += amountToMoveDown;
params.leftMargin += amountToMoveRight;
pandya.setLayoutParams(params);
}
});
pandya.startAnimation(anim);
}
}
You can use anim.setFillAfter(true) to the TranslateAnimation and get the button stay at the new position, but the button clicks will not work at the new position.
So use ViewPropertyAnimator or ObjectAnimator. These are property animation and will change the position of the View, whereas the TranslateAnimation is View animation and will not change the actual property of the View.
For example, using ViewPropertyAnimator:
pandya.animate()
.translationX(amountToMoveRight)
.translationY(amountToMoveDown);
Refer the blog for more info:
Finally, the previous animations changed the visual appearance of the
target objects... but they didn't actually change the objects
themselves. You may have run into this problem. Let's say you want to
move a Button from one side of the screen to the other. You can use a
TranslateAnimation to do so, and the button will happily glide along
to the other side of the screen. And when the animation is done, it
will gladly snap back into its original location. So you find the
setFillAfter(true) method on Animation and try it again. This time the
button stays in place at the location to which it was animated. And
you can verify that by clicking on it - Hey! How come the button isn't
clicking? The problem is that the animation changes where the button
is drawn, but not where the button physically exists within the
container. If you want to click on the button, you'll have to click
the location that it used to live in. Or, as a more effective solution
(and one just a tad more useful to your users), you'll have to write
your code to actually change the location of the button in the layout
when the animation finishes.
Use anim.setFillAfter(true); and it will remain on the final position after the end of animation.

The java backend code to the xml code

I have a basic android application that has several pages accessible from the home page, anyways I change the layout by doing
public void main(View view) {
setContentView(R.layout.main);
}
and changing the layout to the main game page. Now I want to add characters to it and so I added protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint green = new Paint();
green.setColor(Color.GREEN);
Rect theRect = new Rect();
theRect.set(0,0,canvas.getWidth()/2,canvas.getHeight()/2);
canvas.drawRect(theRect, green);
}
but I only want to call it for the "main" or the scene/layout that the game is played in. It appears to not be called at all and that may be due to the fact that I do not completely understand the relationship between the xml and multiple java files and functions. I'm new to android so this may be a stupid question, I just couldn't find anything with multiple web searches, with no luck.
You can add your game view into activity class:
Class GameView extends View/SurfaceView
{
//-- contains onDraw() method
}
To add this game view into your activity, you have to do
get reference of layout in whioch you want to add this gameview.
eg: LinearLayout layout=(LinearLayout) findViewById(R.id.linearlayout);
Add GameView into this layout
eg: layout.addView(new GameView());
For more detail, you can get reference from http://www.edu4java.com/en/androidgame/androidgame3.html
Hope this helps you

Android TextView: onClick after Moving TextView

I'm trying to get my textView to react to a user's tap.
I found out that I actually have to set android:clickable="true", so my function is actually called now.
But my problem is, that I'm using an animation to move my textView within my activity (from bottom to top). So now the onClick function is only called when you tap where my textView used to be, not where it actually is.
How do you fix this?
EDIT:
Unfortunately, I wasn't able to move the actual TextView programmatically (I'm using a RelativeLayout) like the first answer suggests.
However, I could overlay the position of my TextView with another TextView (transparent) that's clickable only when the animation comes to an end.
Works perfectly.
When you are doing animations the TextView is only visualy moved (the graphic rendering representation is only moved on the screen) but the real TextView object stays on the old position. You have to manually (programatically) move the TextView to the new position.
Take a look at this presentation from Romain Guy and Chet Haase.
Chet Haase is taking about this issue at the 42:00 min. Take a look at the whole presentation if you have time. They are talking about some really usefull things.
I recently had to do this. You didn't provide any code, so this will have to do. The key is setting bottomMargins to compensate.
Animation yourAnimation = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.your_anim_file);
slideOutBottom.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
animatedButton.clearAnimation();
Resources r = view.getResources();
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, -68, r.getDisplayMetrics());
lp = (LinearLayout.LayoutParams) animatedButton.getLayoutParams();
lp.bottomMargin = -50;
cartFunctionButtons.setLayoutParams(lp);
}
});
yourAnimation.setFillAfter(true);
animatedButton.startAnimation(yourAnimation);

When in the Activity life cycle are UI elements available to be measured?

I am defining some animations based on the inflated dimensions of some UI controls. What is the earliest point in the Activity life cycle I can tap into to know when the UI elements have been sized and I can query them for their dimensions?
Right after you set the Content of you Activity via the setContentView() method is the earliest I've been able to grab information from my widgets (size, text and others).
Per Rich's request:
You can determine when the width and height by using the GlobalLayoutListener like so:
final View myView = findViewById(R.id.id_of_view);
ViewTreeObserver vto = myView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int viewHeight = myView.getHeight();
int viewWidth = myView.getWidth();
// Do what you want with the width and height
ViewTreeObserver obs = myView.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});
Full (better) answer: How to retrieve the dimensions of a view?
If you want to drill down to the point that widget have JUST been placed you have to extend each widget you want to monitor.
Then override onDraw method and capture if that view has been drawn one time
private boolean imVisible=false;
public boolean imVisible() {
return imVisible;
}
#Override
protected void onDraw(Canvas canvas) {
if(!imVisible){
imVisible=true;
}
super.onDraw(canvas);
}
Then you can do a for loop to the widgets of interest and you know they are drawn at their position with dimentions.
A far better solution is when the onDraw gets called the first time fire a listener that is drawn. You have to set an array of listeners that watch the progress of the widgets. That way the exact moment that the last widget is on the screen... you know it.

Categories

Resources