TextView為什麼變得不可見?
layout xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvRotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate Me"
/>
</LinearLayout>
Activity:
public class RotateMeActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tvRotate = (TextView) findViewById(R.id.tvRotate);
RotateAnimation r = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
r.setDuration(0);
r.setFillAfter(true);
tvRotate.startAnimation(r);
tvRotate.setVisibility(View.INVISIBLE);
}
}
我想實現的功能是能讓試圖能旋轉,然後可以隱藏,通過設置setVisibility可以在代碼中顯示出來。在API Level 11中setRotation是可以實現的,但是在API Level 10中就不行。什麼原因呢?
tvRotate.setRotation(180);//instead of the RotateAnimation, only works in API Level 11
tvRotate.setVisibility(View.INVISIBLE);
另外一個解決方案是,在另一個view中隱藏動畫視圖,設置隱藏了的視圖的可見性。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/animationHoldingFrame"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvRotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate Me"
/>
</FrameLayout>
</LinearLayout>
代碼然後變成這樣:
TextView tvRotate = (TextView) findViewById(R.id.tvRotate);
FrameLayout animationContainer = (FrameLayout)findViewById(R.id.animationHoldingFrame)
RotateAnimation r = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
r.setDuration(0);
r.setFillAfter(true);
tvRotate.startAnimation(r);
animationContainer.setVisibility(View.INVISIBLE);