myc_100 2012-12-14 09:59 采纳率: 10%
浏览 4523
已采纳

当试图被设置成动画形式时,为什么setVisibility不能执行?

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);
  • 写回答

2条回答 默认 最新

  • myq_26 2012-12-17 02:11
    关注

    另外一个解决方案是,在另一个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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝