dynamic ImageView TranslateAnimation - android

I have images that need to appear from one location a user clicks, and move to another location the user clicks. I think the issue I am having is how to initially place the ImageView at a given absolute X,Y (my main view root layout is the default LinearLayout). This is what I'm currently trying, the animation runs but is not visible (after 1000ms the end function is indeed called).
ImageView iv = new ImageView(mainActivity);
String imgname = "drawable/animImg";
int imgId = act.getResources().getIdentifier(imgname, "drawable", act.getPackageName());
iv.setImageDrawable(act.getResources().getDrawable(imgId));
/*didn't seem to help
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
((ViewGroup)mainActivity.findViewById(R.id.LinearLayout)).addView(iv,lp);
*/
int[] srcxy = new int[2];
src.getLocationInWindow(srcxy);
int[] destxy = new int[2];
dest.getLocationInWindow(destxy);
TranslateAnimation translateAnimation = new TranslateAnimation( Animation.ABSOLUTE, srcxy[0], Animation.ABSOLUTE, destxy[0], Animation.ABSOLUTE, srcxy[1], Animation.ABSOLUTE, destxy[1]);
translateAnimation.setDuration(1000);
translateAnimation.setInterpolator(new AccelerateInterpolator());
translateAnimation.setAnimationListener( new MyAnimationListenener( c, src, dest, this ) );
iv.setVisibility(View.VISIBLE);
iv.startAnimation(translateAnimation);
So is there a way for me to specify that I want to first position the ImageView at srcxy[0],srcxy[1] before I animate? Or am I even going about this in the right way?

You can't place an ImageView to a x,y position if it's contained in a LinearLayout: as the name indicates, LinearLayout is used to display elements sequencely in horizontal or vertical order. I think you have to use FrameLayout. If you look at this example you can find a the piece of code that reaches your goal.

Related

Any way to avoid overlapping of 2 images on android?

For an android app, Im doing infinite Translate Animation.
imageview starts first and after a delay of 1 second, imageview2 starts.
Animation animation = new TranslateAnimation(0, 0, -500, 500);
animation.setDuration(4000);
imageview.startAnimation(animation);
animation.setRepeatCount(Animation.INFINITE);
Animation animation2 = new TranslateAnimation(0, 0, -500, 500);
animation2.setDuration(4000);
imageview2.startAnimation(animation2);
imageview2.setStartOffset(1000);
animation2.setRepeatCount(Animation.INFINITE);
The problem is that, after sometime imageview overlaps imageview2.
What Can I do to avoid the overlapping of 2 images ?
Any pointer would be appreciated.
You might want to try something like this:
private boolean isViewOverlapping(View firstView, View secondView) {
int[] firstPosition = new int[2];
int[] secondPosition = new int[2];
firstView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
firstView.getLocationOnScreen(firstPosition);
secondView.getLocationOnScreen(secondPosition);
int r = firstView.getMeasuredWidth() + firstPosition[0];
int l = secondPosition[0];
return r >= l && (r != 0 && l != 0);
}
This is a method that will take in two views and check whether they are overlapping. If they are, it will return a boolean of true. I would presume that once you get this boolean returned as true, you would then want to carry out another action to ensure they are not overlapped anymore, such as moving one view down 400 pixels. This would then result in the images not overlapping and you would have avoided the worrying situation. Hope this helps!

Android - Layout Not Centering

I'm confused, this should be centering, yet it's not.
public void addTableImage(String imageName, LinearLayout L){
ImageView newImage = new ImageView(c);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 150);
newImage.setLayoutParams(layoutParams);
int id = c.getResources().getIdentifier(imageName, "drawable", c.getPackageName());
RelativeLayout l1 = new RelativeLayout(c);
l1.setLayoutParams(new RelativeLayout.LayoutParams(400, 160));
l1.setBackgroundColor(Color.WHITE);
l1.setGravity(Gravity.CENTER);
newImage.setImageResource(id);
l1.addView(newImage);
L.addView(l1);
}
And I'm getting this as a result:
Image shows up fine (yes, it's just a placeholder green box), the background is set, and everything is set to be Gravity.CENTER, yet it's still left oriented.
Bonus points: Anyone know why "Ask a Question" now autocompletes your last question's info? It's a bit confusing, making me think I'm actually editing an old question.
Change your code to:
public void addTableImage(String imageName, LinearLayout L){
ImageView newImage = new ImageView(c);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 150);
newImage.setLayoutParams(layoutParams);
int id = c.getResources().getIdentifier(imageName, "drawable", c.getPackageName());
RelativeLayout l1 = new RelativeLayout(c);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(400, 160);
lp.gravity=Gravity.CENTER;
l1.setLayoutParams(lp);
l1.setBackgroundColor(Color.WHITE);
l1.setGravity(Gravity.CENTER);
newImage.setImageResource(id);
l1.addView(newImage);
L.addView(l1);
}
Your image is centered inside relativelayout by setting gravity of relative layout to center.
But to set relative layout itself to the center of the Linearlayout it is in you need to set layout gravity to center of relative layout to center or gravity of the LinearLayout to center.
You can try above code it should work.

Android negative margin does not work

I have encountered a problem when i try to give a negative left margin to a LinearLayout.
The negative margin does not appear.
Here is my code
HorizontalScrollView hview = new HorizontalScrollView(context); // HorizontalScrollView is the outer view
RelativeLayout.LayoutParams hs_lot_params = new RelativeLayout.LayoutParams(164, 164);
hs_lot_params.setMargins(100, 100, 0, 0); // set the positions
ImageView image = new ImageView(context);
image.setBackgroundResource(R.drawable.leder);
LinearLayout.LayoutParams img_lot_params = new LinearLayout.LayoutParams(164, 164);
img_lot_params.setMargins(0, 0, 0, 0);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(164, 164);
layoutParams.setMargins(-132, 0, 0, 0);
ll.addView(image, img_lot_params);
hview.addView(ll, layoutParams);
Note: my plan is to scroll the image from left to right.
First, the left part of the image is hidden and can scroll to right to see the full image
ViewGroup.MarginLayoutParams params =
(ViewGroup.MarginLayoutParams)view.getLayoutParams(); params.topMargin = -100;
Negative margins should work in LinearLayout and RelativeLayout. What you probably need, is to scroll the HorizontalScrollView with scrollBy(int x, int y) or scrollTo(int x, int y) to achieve the "peek and scroll" effect you described.
Also keep in mind that using raw pixel units is generally a bad idea as the actual size will depend on the pixel density of the screen. Prefer dp measurements instead.

AnimationSet plays in reverse order

I'm using an AnimationSet to do many Translate animations. In this example I want to move an ImageView from 0 to 700 (offscreen) and from -700 to 0 (the view move to the extreme right and re-enter from the extreme left). I use this code:
AnimationSet set = new AnimationSet(true);
set.setInterpolator(new AccelerateDecelerateInterpolator());
set.setFillEnabled(true);
if(traject!=null){
TranslateAnimation tmpAnim = null;
set.setDuration(duration);
int d = traject.size()!=0 ? duration/traject.size() : duration;
for(int i =0; i<traject.size();i++){
tmpAnim = new TranslateAnimation( traject.get(i).getStartX(),
traject.get(i).getEndX(),
traject.get(i).getStartY(),
traject.get(i).getEndY() );
tmpAnim.setDuration(d);
tmpAnim.setFillAfter(true);
tmpAnim.setStartOffset(d*i);
set.addAnimation(tmpAnim);
}
}
traject is an arraylist holding each TranslateAnimation start and end points
But in my test example the animation goes from -700 to 700. I tried switching the order of the TranslateAnimations, using setStartTime() instead of setStartOffset(), skipping the call to setStartOffset(), the result is the same. Do you have any idea on what I am missing ?

How to animate the translation of a view in Android

I would like to know if somebody knows how to animate the translation of the views programatically, not XML, I have the coordinates where these views should go.
This is the creation of the view:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.leftMargin = x;
params.topMargin = y;
my_view.setLayoutParams(params);
holder.addView(my_view);
I need to move and animate this view to other x1,y1 position. Thanks!
TranslateAnimation animation = new TranslateAnimation(fromX,ToX,FromY,ToY);
layout.startAnimation(animation);
This should work.

Categories

Resources