Really need some help on this...
Please take a look to this simple FadeIn animation of an ImageView, using full java code. Recreate it using API\'s 21, 18, 17, 16.. work
! SOLVED !
For some reason, API19 programmatically pure java animations doesn't play very well with absolute settings, like setX() or setY(). Experts may take a look on this. API19 prefers to work with margins on the layout parameter side.
The following procedure, apply's to android 4.4.2. Of course, it works on the others versions down and up, but you must change your way of thinking. You must place an imagen programmatically using layout parameters, so change:
iv.setX(50);
iv.setY(0);
to
lparam.setMargins(50, 0, 0, 0);
iv.setLayoutParams(lparam);
iv.requestLayout();
you can also use (for API >=17)
lparam.setMarginStart(50); // for x pos
lparam.setMarginEnd(0); // for y pos
You must call "requestLayout()" to ensure that the new changes on the layout, will be committed when adding the view to the parent (fl).
Finally, by mistake, I use "LayoutParams" instead of "FrameLayout.LayoutParams". This has nothing to do with the issue and was not affecting it, but this is the correct way to define it, so change:
LayoutParams lparam = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
to
FrameLayout.LayoutParams lparam = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
The complete working code for any API is:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Common common = new Common(this, this);
FrameLayout fl = new FrameLayout(this);
ImageView iv = new ImageView(this);
FrameLayout.LayoutParams flparam = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
FrameLayout.LayoutParams lparam = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
iv.setImageBitmap(common.newImage("100x100.png", ImageFormat.RGB565).getBitmap());
lparam.setMarginStart(50);
lparam.setMarginEnd(0);
iv.setLayoutParams(lparam);
iv.requestLayout();
fl.addView(iv);
this.setContentView(fl, flparam);
AlphaAnimation animation = new AlphaAnimation(0, 1);
animation.setDuration(3000);
iv.setAnimation(animation);
iv.startAnimation(animation);
}