安卓:在API翻译动画不必要的延迟19



我对Android和StackOverflow都很陌生。在API 19中播放动画之前,我在这里为程序创建的TextView设置了动画,但它显示了我的TextView的最终状态(即动画完成后的状态)的几分之一秒,这使它立即失去了视觉吸引力。然而,它在API 22中运行良好。任何帮助都将不胜感激。我还放入了动画资源文件、布局资源文件和Java类的代码。

我的动画资源文件的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<set android:interpolator="@android:anim/decelerate_interpolator" android:fillBefore="false">
    <alpha
           android:duration="1200"
           android:fromAlpha="0.0"
           android:toAlpha="1.0"
        />
    <translate
               android:duration="1200"
               android:fromYDelta="70%p"
               android:toYDelta="0%p"/>
</set>
</set>

我的布局资源文件的代码如下:

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/main_layout"
android:gravity="center_horizontal">

我的Java文件的代码如下:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout layout = (RelativeLayout)findViewById(R.id.main_layout);
        TextView tv = new TextView(this);
        tv.setTextColor(Color.BLACK);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 70);
        //tv.setGravity(Gravity.CENTER_VERTICAL);
        tv.setText("My Text");
        layout.addView(tv);
        Animation fadeUp = AnimationUtils.loadAnimation(this, R.anim.fade_up);
        tv.startAnimation(fadeUp);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

在xml:中将TextView初始设置为"不可见"

android:visibility="invisible"

然后在onCreate方法中,在动画之前添加:

tv.setVisibility(View.VISIBLE);

看看它是如何执行的-如果仍然有"轻弹",那么用替换上面的行

Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                tv.setVisibility(View.VISIBLE);
            }
        }, 20);

基本上,这将确保动画在TextView可见之前20毫秒开始。

尝试添加

android:fillEnabled="true"
android:fillBefore="true"
android:fillAfter="true"

去你的片场。还要注意,动画中有两个set元素,这毫无意义——去掉外部的一个。

fill属性使视图的布局位置固定为fromto声明。因此,在动画开始之前,您将停留在70%p,然后停留在0%p。如果这也不起作用,也许您应该考虑使用可见性。

最新更新