【问题标题】:dynamic ImageView TranslateAnimation动态 ImageView 翻译动画
【发布时间】:2012-08-09 10:01:19
【问题描述】:

我的图像需要从用户单击的一个位置显示,然后移动到用户单击的另一个位置。我认为我遇到的问题是如何最初将 ImageView 放置在给定的绝对 X、Y 上(我的主视图根布局是默认的 LinearLayout)。这是我目前正在尝试的,动画运行但不可见(1000ms 后确实调用了 end 函数)。

        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);  

那么有没有办法让我指定我想在动画之前首先将 ImageView 定位在 srcxy[0],srcxy[1] 上?还是我什至以正确的方式处理这件事?

【问题讨论】:

    标签: android animation imageview


    【解决方案1】:

    如果 ImageView 包含在 LinearLayout 中,则不能将其放置到 x,y 位置:顾名思义,LinearLayout 用于按水平或垂直顺序顺序显示元素。我认为你必须使用 FrameLayout。如果您查看this example,您会发现一段代码可以达到您的目标。

    【讨论】: