簡體   English   中英

如何在Android中從左向右平滑滑動一個ImageView?

[英]How to slide an ImageView from left to right smoothly in Android?

我需要使用平滑的ImageView從屏幕左側向右側滑動 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移動但 animation 像我想要的那樣滯后且不流暢。 我在代碼上做錯了什么?

創建這種補間動畫很簡單。 按照步驟,

步驟1

res目錄中創建一個目錄anim ,並將其作為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這兩個屬性fromXDelta定義動畫。 %p指的是父母 ,這意味着它將相對於父母移動圖像75%。

第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() 我沒有在這里展示它們以保持簡單。 如果您需要,請告訴我。

注意

正如您反復提到的那樣,您正在經歷一個滯后動畫 我已經在3個真實設備和2個模擬器上測試了這個動畫, 動畫在所有這些動畫上都很平滑 在Moto E等低端設備上測試Nexus 5和Galaxy S6等高端設備。

如果您仍然有延遲運行此代碼,那么測試設備必須是原因。 代碼很完美。

UPDATE

我剛剛檢查了在Lollipop上運行的Moto G,動畫運行順暢。 這是一個非常小巧輕巧的動畫,永遠不會遲鈍。 如果您仍然遇到滯后,那么它必須是您正在測試的設備,或者該Activity上的其他一些代碼使UI緩慢或無響應

試着檢查哪一個適用於你,

  • 我已經測試了總共6個設備,現在沒有任何延遲。 所以,你可以放心,你的制作應用程序不會有任何延遲,它可能是你的設備很慢
  • 如果您正在執行一些繁重的操作,如訪問文件系統,數據庫或任何其他繁重的操作,它必須減慢UI線程,並且您正在丟失幀。 嘗試使用AsyncTask進行這些繁重的操作

試試這個。 它將為imageview添加動畫效果。

        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
TranslateAnimation animate = new TranslateAnimation(0, -view.getWidth(), 0, 0);
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);

嘗試下面的代碼

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  

希望這對你有用

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

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

要移動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>

現在我們有 MotionLayout 嘗試使用它來實現非常干凈和平滑的滑動 animation。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM