动态 ImageView TranslateAnimation

发布于 2024-10-28 08:24:26 字数 1414 浏览 0 评论 0原文

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

        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] 处?或者我是否以正确的方式处理这件事?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三月梨花 2024-11-04 08:24:26

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文