如何在Android中顺畅地从左向右滑动ImageView?

时间:2015-07-22 12:14:32

标签: java android android-animation

我需要使用平滑动画从屏幕的左侧到右侧制作ImageView幻灯片(我希望在转换期间ImageView可见)我尝试使用以下代码:

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移动,但是动画是滞后的,并且不像我想的那样平滑。 我在代码上做错了什么?

7 个答案:

答案 0 :(得分:34)

创建这种补间动画很简单。只需按照步骤进行操作即可

第1步

@Transactional public UserResponse createCategory(SubmitReviewRequest createCategoryRequest) throws SQLException, ClassNotFoundException, IOException { Category category = new Category(); // read parent Category parentCategory = adminServiceDao.find(Integer.parseInt(createCategoryRequest.getMainCategory())); UserResponse userResponse = new UserResponse(); if (createCategoryRequest != null) { category.setCategoryName(createCategoryRequest.getSubCategory()); // category.setParenCategoryId(Integer.parseInt(createCategoryRequest.getMainCategory())); //set the child relationship. category.setParentCategory(parentCategory); int id = adminServiceDao.saveCategory(parentCategory); userResponse.setCode(WeekenterConstants.SUCCESS_CODE); userResponse.setMessage("Success"); userResponse.setId(id); } return userResponse; } 目录中创建目录anim,并将其设为res

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> fromXDelta来明确自定义动画。 %p 指的是尊重父,这意味着它会将图片相对于父图片移动75%。

第2步

toXDelta

如果您愿意,还可以// 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()。我没有在这里展示它们以保持简单。如果您需要,请告诉我。

注意

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

如果您仍然有延迟运行此代码,那么测试设备必须是原因。代码很完美。

<强>更新

我刚刚检查了在Lollipop上运行的Moto G,动画运行顺畅。这是一个非常小巧轻巧的动画,永远不会迟钝。如果您仍然遇到延迟,那么它必须是您正在测试的设备,或者该活动上的其他一些代码会导致UI缓慢或无响应

尝试检查哪一个适合您,

  • 我已经测试了总共6台设备,现在没有任何延迟。所以,你可以放心,你的制作应用程序不会有任何延迟,它可能是你的设备很慢
  • 如果您正在执行一些繁重的操作,例如访问文件系统,数据库或任何其他繁重的操作,那么它必须减慢UI线程的速度并且您正在丢失帧。尝试使用{{ 1}}用于这些繁重的操作

答案 1 :(得分:4)

TranslateAnimation animate = new TranslateAnimation(0, -view.getWidth(), 0, 0);
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);

答案 2 :(得分:3)

试试这个。它将为图像视图设置动画。

        ImageView img_animation = (ImageView) findViewById(R.id.img_animation);
        Display display = getWindowManager().getDefaultDisplay();
        float width = display.getWidth();
        TranslateAnimation animation = new TranslateAnimation(0, width - 50, 0, 0); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
        animation.setDuration(1000); // animation duration
        animation.setRepeatCount(5); // animation repeat count
        animation.setRepeatMode(2); // repeat animation (left to right, right to
                                    // left )
        // animation.setFillAfter(true);

        img_animation.startAnimation(animation); // start animation

答案 3 :(得分:3)

尝试以下代码

TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 1500.0f); // new TranslateAnimation (float fromXDelta,float toXDelta, float fromYDelta, float toYDelta)
animation.setDuration(2000); // animation duration
animation.setRepeatCount(1); // animation repeat count if u repeat only once set to 1 if u don't repeat set to 0
animation.setFillAfter(false);
your_view .startAnimation(animation);//your_view for mine is imageView  

答案 4 :(得分:0)

public class MainActivity extends Activity {

       int windowwidth;
       int windowheight;

       private LayoutParams layoutParams;

       @Override
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);

              windowwidth = getWindowManager().getDefaultDisplay().getWidth();
              windowheight = getWindowManager().getDefaultDisplay().getHeight();
              final ImageView img = (ImageView) findViewById(R.id.imageView1);

              img.setOnTouchListener(new View.OnTouchListener() {

                     @Override
                     public boolean onTouch(View v, MotionEvent event) {
                           LayoutParams layoutParams = (LayoutParams) img
                                         .getLayoutParams();
                           switch (event.getAction()) {
                           case MotionEvent.ACTION_DOWN:
                                  break;
                           case MotionEvent.ACTION_MOVE:
                                  int x_cord = (int) event.getRawX();
                                  int y_cord = (int) event.getRawY();

                                  if (x_cord > windowwidth) {
                                         x_cord = windowwidth;
                                  }
                                  if (y_cord > windowheight) {
                                         y_cord = windowheight;
                                  }

                                  layoutParams.leftMargin = x_cord - 25;
                                  layoutParams.topMargin = y_cord - 75;

                                  img.setLayoutParams(layoutParams);
                                  break;
                           default:
                                  break;
                           }
                           return true;
                     }
              });
       }
}

答案 5 :(得分:0)

希望这适用于你

<强> animation.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Animation" >

    <ImageView
        android:id="@+id/img_animation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="42dp"
        android:src="@drawable/emo_im_laughing" />

</RelativeLayout>

<强> Animation.java

import android.os.Bundle;
import android.app.Activity;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class Animation extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.);

  ImageView img_animation = (ImageView) findViewById(R.id.img_animation);

  TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,
    0.0f, 0.0f);
  animation.setDuration(5000);
  animation.setRepeatCount(5);
  animation.setRepeatMode(2);
  animation.setFillAfter(true);
  img_animation.startAnimation(animation);

 }
}

答案 6 :(得分:0)

要移动imageview

从右到左使用@Aritra Roy答案。

使用此代码

<?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="75%p"
        android:toXDelta="0%p"
        android:duration="100000" />
</set>