如何在Android中从左向右平滑滑动ImageView? [英] How to slide an ImageView from left to right smoothly in Android?

查看:153
本文介绍了如何在Android中从左向右平滑滑动ImageView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用平滑的动画从屏幕的左向右滑动ImageView幻灯片(我希望ImageView在过渡期间可见),我尝试使用以下代码:

I need to make an ImageView slide from left to right of the screen with a smooth animation (I want the ImageView to be visible during the transition) I tried with the following code:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;

camion.animate()
    .translationX(width)
    .setDuration(2000)
    .setInterpolator(new LinearInterpolator())
    .setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            //camion.setVisibility(View.GONE);
        }
    });

ImageView移动了,但是动画像我想要的那样缓慢且不平滑. 我在代码上做错什么了?

The ImageView moves but the animation is laggy and not smooth like I want to. What am I doing wrong on the code?

推荐答案

创建这种补间动画非常简单.只需按照步骤操作,

Creating this kind of tween animation is simple. Just follow the steps,

第1步

res目录内创建目录anim,并将其放置为slide.xml

Create a directory anim inside the res directory and put this as slide.xml

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

   <translate
        android:fromXDelta="0%p"
        android:toXDelta="75%p"
        android:duration="800" />
</set>

您显然可以通过更改两个属性fromXDeltatoXDelta来自定义动画. %p 是指尊重父级,这只是意味着它将相对于父级将图像移动75%.

You can obviously customize the animation by changing the two attributes fromXDelta and toXDelta. The %p refers to with respect to the parent which simply means that it will move the image 75% with respect to the parent.

第2步

// Refer the ImageView like this
imageView = (ImageView) findViewById(R.id.img);

// Load the animation like this
animSlide = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.slide);

// Start the animation like this
imageView.startAnimation(animSlide);

如果需要,您也可以setInterpolator()setListeners().为了简单起见,这里没有显示它们.如果需要,请告诉我.

You can also setInterpolator() and setListeners() if you want to. I haven't shown them here to keep it simple. If you need, just let me know.

注意

正如您反复提到的那样,您遇到的是松散动画.我已经在 3个真实设备和2个仿真器上测试了此动画,并且在所有这些动画上都非常流畅.在Moto E等低端设备到Nexus 5和Galaxy S6等高端设备上进行了测试.

As you have mentioned repeatedly, that you are experiencing a laggy animation. I have tested this animation on 3 real devices and 2 emulators and the animation was buttery smooth on all of them. Tested on low end devices like Moto E to high end devices like Nexus 5 and Galaxy S6.

如果运行此代码仍然有滞后性,则必须是测试设备.该代码是完美的.

If you still have a lag running this code, then the test device must be the reason. The code is perfect.

更新

我也刚刚检查了在棒棒糖上运行的Moto G,动画运行顺利.这是一个非常小巧,轻巧的动画,并且永远不要太笨拙.如果仍然滞后,则它必须是您正在测试的设备,或者该Activity上的其他一些代码,会导致UI变慢或无响应.

I just checked on my Moto G running on Lollipop too, the animation is running smoothly. This is a very small and light-weight animation, and should never be laggy. If you are still getting a lag, then it must be the device you are testing, or some other piece of code on that Activity making the UI slow or unresponsive.

尝试检查哪一个适合您,

Try to check which one is applicable to you,

  • 我现在总共对6台设备进行了测试,完全没有延迟.因此,您可以放心,您的生产应用不会有任何滞后,这可能是您的设备运行缓慢
  • 如果您要执行一些繁重的操作,例如访问文件系统,数据库或任何其他繁重的操作,则一定是在减慢UI线程的速度,并且正在丢失框架.尝试使用用于这些繁重的操作

这篇关于如何在Android中从左向右平滑滑动ImageView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆