android中的按钮如何淡入淡出

时间:2016-04-15 15:49:08

标签: java android

我有4个按钮的布局活动。活动开始后按钮应该淡入。活动开始时如何淡入4个按钮。我在xml部分很清楚,但我在java方面面临问题

2 个答案:

答案 0 :(得分:1)

很简单:

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayList<View> viewsToFadeIn = new ArrayList<View>();

    viewsToFadeIn.add(findViewById(R.id.b1));
    viewsToFadeIn.add(findViewById(R.id.b2));
    viewsToFadeIn.add(findViewById(R.id.b3));
    viewsToFadeIn.add(findViewById(R.id.b4));

    for (View v : viewsToFadeIn)
    {
        v.setAlpha(0); // make invisible to start
    }

    for (View v : viewsToFadeIn)
    {
        // 3 second fade in time
        v.animate().alpha(1.0f).setDuration(3000).start();
    }
}

答案 1 :(得分:0)

使用第三方库来执行此操作。你可以使用这个:

**compile 'com.nineoldandroids:library:2.4.0'**

现在你可以这样写:

Button button = (Button) findViewById(R.id.idButon);
ObjectAnimator.ofFloat(button, "alpha", 1f).setDuration(100).start();