Android表格行的淡出动画

6

我有一个包含TextViewTableRow。这是它的XML代码。

<TableRow
    android:layout_height="fill_parent" 
    android:layout_gravity="bottom"
    android:layout_width="fill_parent"
    android:background="#BF000000">

    <TextView
        android:id="@+id/topText"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:textSize="19sp"
        android:background="#BF000000"
        android:layout_gravity="center_horizontal"
        android:text="@string/text_searchword"
        android:layout_width="fill_parent">
    </TextView>

</TableRow>

我想在按钮触摸时使用淡出效果使表格行看不见,反之亦然。 我该怎么做?


这是一个例子 http://thegeekyland.blogspot.com/2015/12/android-animations-explained.html - Arlind Hajredinaj
1个回答

17

任何 View(包括 TableRow)都可以附加淡入淡出动画,但您需要能够在代码中引用视图,因此该行需要一个id:

<TableRow
  android:id="@+id/my_row"
  android:layout_height="fill_parent" 
  android:layout_gravity="bottom"
  android:layout_width="fill_parent"
  android:background="#BF000000">
  <TextView
    android:id="@+id/topText"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"
    android:textSize="19sp"
    android:background="#BF000000"
    android:layout_gravity="center_horizontal"
    android:text="@string/text_searchword"
    android:layout_width="fill_parent">
  </TextView>
</TableRow>

现在你可以在Java代码的某个地方(比如onCreate())中引用该行本身,例如:

View row = findViewById(R.id.my_row);

请注意,我没有将它转换为 TableRow。如果您需要执行其他操作,可以这样做,但仅仅为了设置可见性,将其保留为 View 就可以了。然后只需构造一个类似于以下的按钮点击方法:

public void onClick(View v) {
    View row = findViewById(R.id.myrow);
    if(row.getVisibility() == View.VISIBLE) {
        row.startAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
        row.setVisibility(View.INVISIBLE);
    } else {
        row.startAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        row.setVisibility(View.VISIBLE);
    }
}

Fade in和Fade out是Android包中定义的标准动画,您不需要自己创建它们,只需使用AnimationUtils.loadAnimation()加载即可。 在此示例中,单击相同的按钮会根据视图是否已可见,在淡入和淡出之间切换。

希望能帮到您!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接