【问题标题】:In the deltaX of an Android animation, should I use pixels or percentage?在 Android 动画的 deltaX 中,我应该使用像素还是百分比?
【发布时间】:2017-10-19 18:16:55
【问题描述】:

在设计视图中,我在 android 设备屏幕的右边界之外放置了一个视图。我想播放动画并将其从屏幕外移到屏幕中以制作入口效果,没什么花哨的。

然而,从那以后我一直在努力使用 deltaX 参数。什么是正确的数字?我应该选择像素还是百分比?

当我提供正确的输入时,会触发正确的事件,但无法找到视图,它根本不会显示在屏幕上。

这是我有问题的代码:

动画:

<?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="100%"
    android:toXDelta="0%"
    android:duration="300" />
</set>

有问题的观点:

<GridView
    android:id="@+id/android_gridview_menu"
    android:layout_width="80dp"
    android:layout_height="0dp"
    android:background="#ffffff"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    app:layout_constraintLeft_toRightOf="@+id/MainUI"
    android:layout_marginLeft="0dp">

【问题讨论】:

    标签: android android-animation


    【解决方案1】:

    首先将网格视图翻译出屏幕,然后播放动画

    // Translating grid view out of the screen
    GridView gridView = findViewById(R.id.android_gridview_menu);
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point(); 
    display.getSize(size);
    gridView.setTranslationX(size.x);
    
    
    // Call this method to bring grid view from right by animation
    private void playEnterAnimation(){
        GridView gridView = findViewById(R.id.android_gridview_menu);
        gridView.animate().translationX(0).setDuration(200).setInterpolator(new AccelerateDecelerateInterpolator()).start();
    }
    

    【讨论】:

    • 真的不行,我相信用动漫资源文件比较合理。
    • 此 playEnterAnimation() 方法仅在您第一次使用代码将视图移出屏幕时才起作用 // 将网格视图移出屏幕 GridView gridView = findViewById(R.id.android_gridview_menu) ;显示显示 = getWindowManager().getDefaultDisplay();点大小 = new Point(); display.getSize(大小); gridView.setTranslationX(size.x);
    【解决方案2】:

    要播放动画,请使用 AnimationAnimationUtils

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), 
                R.anim.animation); // R.anim.animation is your animation xml.
    
        GridView layout = (GridView) findViewById(R.id.android_gridview_menu);
        layout.startAnimation(animation);
    }
    

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多