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.
Related
i use this code to change the position of my view
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)myview.getLayoutParams();
params.topMargin = topmargin;
params.leftMargin = leftmargin;
myview.setLayoutParams(params);
and nothing change.but when i use this code
myview.setTop(topmargin);
myview.setLeft(leftmargin);
it will work.now i want to know what is wrong with my first code??
because in the internet so many people suggest first code to change the position of views.
EDIT
also this code doesn't work
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)myview.getLayoutParams();
params.setMargins(100,100,0,0);
myview.setLayoutParams(params);
myview.requestLayout();
thanks in advance
Try calling myview.requestLayout(); after myview.setLayoutParams(params);
UPDATE
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)myview.getLayoutParams();
params.setMargins(left,top, right,bottom);
myview.setLayoutParams(params);
myview.requestLayout();
You need to call requestLayout() after updating the layout params
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)myview.getLayoutParams();
params.topMargin = topmargin;
params.leftMargin = leftmargin;
myview.setLayoutParams(params);
myview.requestLayout();
And this is a brief explanation from official documentation:
.setTop() ( same with .setLeft() )
Sets the top position of this view relative to its parent. This method
is meant to be called by the layout system and should not generally be
called otherwise, because the property may be changed at any time by
the layout.
topMargin ( same with leftMargin )
The top margin in pixels of the child. Margin values should be
positive. Call setLayoutParams(LayoutParams) after reassigning a new
value to this field.
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.
i want to use a unknown (at begin) number of Views in my CustomView, so I have to set the position of each of them programatically. The problem is that I cannot use setX()/... or seTop()/... , because Im developing for froyo. But how can I set the position else?
With RelativeLayout you can do the following:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)child.getLayoutParams();
layoutParams.leftMargin = x;
layoutParams.topMargin = y;
child.setLayoutParams(layoutParams);
How to place TextViews on layout depends on TextView's center from code?By default we can create it depends on its top left angle. Method setGravity(Gravity.CENTER_HORIZONTAL) doesn't help.
EDIT
I want to place TextViews in circle. For what I use RelativeLayout. Within layot TextViews place in for-loop with some angle step. Here some code
for (int i = 0; i < 62; i++) {
TextView word = new TextView(context);
word.setTextSize(13);
// word.setGravity(Gravity.CENTER_HORIZONTAL);
word.setTextColor(Color.BLACK);
RotateAnimation rotateAnimation = new RotateAnimation(0,
angle.get(i), 0, (float) (textHeight * 0.9 + rotateCircle));
rotateAnimation.setFillAfter(true);
rotateAnimation.setDuration(100);
rotateAnimation.setStartOffset(100);
word.setAnimation(rotateAnimation);
word.setGravity(Gravity.CENTER);
word.setPadding(10, 10, 10, 10);
RelativeLayout.LayoutParams layoutparams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutparams.leftMargin = Math.round(x_coords.get(i));
layoutparams.topMargin = (int) (Math.round(y_coords.get(i)
- textHeight * 0.9 - rotateCircle));
textviews.add(word);
words_layout.addView(word, layoutparams);
}
The gravity of a TextView controls how its content is aligned (the text), not how the view itself is aligned in the layout. To control how the View is aligned in the layout use the android::layout_gravity attribute instead, or the gravity field in the LayoutParams.
Use the android:layout_gravity = CENTER_HORIZONTAL to make text view in center.
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.