PathMenu 中 将ViewGroup 的子类setVisibility无效。

不知道什么原因,在处理那个pathview的时候,将viewsetVisibility(View.GONE)一直无效,具体原因还未找出,但找到了一个解决方案,先记录一下。

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;

public class PathAnimations{
	private  final String TAG = "MYAnimation";
	
	private  float maxOffset = 160;
	
	private  double maxAngle= Math.PI/2;
	public  void initOffset(Context context) {//初始化
		maxOffset = context.getResources().getDimension(R.dimen.set_fly_dimens);
	}
	
	public PathAnimations(long duration ,float maxOffset){
		this.duration = duration;
		this.maxOffset = maxOffset;
	}
	
	private long duration = 500;
	public  void setDuration(long time){
		duration = time;
	}
	public  void setOffset(float offset){
		maxOffset =offset;
	}
	
	public static Animation  getRotateAnimation(float fromDegrees,
			float toDegrees, int durationMillis) {
		RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		rotate.setDuration(durationMillis);
		rotate.setFillAfter(true);
		return rotate;
	}
	
	/**
	 * 围绕90度旋转
	 * @param viewgroup
	 * @param durationMillis
	 */
	public  void startAnimationsOut(ViewGroup viewgroup) {
		viewgroup.setVisibility(View.VISIBLE);
		int counts = viewgroup.getChildCount();
		Log.i(TAG,counts+",out");
		double singleAngle;
		if(counts>1)
			singleAngle = maxAngle/(counts-1);
		else{
			singleAngle = maxAngle;
		}
		float xOffset,yOffset;
		for (int i = 0; i < counts; i++) {
			xOffset = (float) Math.cos(singleAngle*i)*maxOffset;
			yOffset = (float) Math.sin(singleAngle*i)*maxOffset;
			Log.i(TAG,xOffset+",yOffset:"+yOffset);
			View inoutimagebutton = (View) viewgroup
					.getChildAt(i);
			inoutimagebutton.setVisibility(View.VISIBLE);
			MarginLayoutParams mlp = (MarginLayoutParams) inoutimagebutton
					.getLayoutParams();
			Animation animation = new TranslateAnimation(
					 xOffset, 0F, yOffset, 0F);

			animation.setFillAfter(true);
			animation.setDuration(duration);
			animation.setStartOffset((i * 100)
					/ (-1 + viewgroup.getChildCount()));
			animation.setInterpolator(new OvershootInterpolator(2F));
			inoutimagebutton.startAnimation(animation);

		}
	}
	
	/**
	 * 飞入
	 * @param viewgroup
	 * @param durationMillis
	 */
	public  void startAnimationsIn(final ViewGroup viewgroup ) {
		final int counts = viewgroup.getChildCount();
		double singleAngle;
		if(counts>1)
			singleAngle = maxAngle/(counts-1);
		else{
			singleAngle = maxAngle;
		}
		float xOffset,yOffset;
		for (int i = 0; i < counts; i++) {
			xOffset = (float) Math.cos(singleAngle*i)*maxOffset;
			yOffset = (float) Math.sin(singleAngle*i)*maxOffset;
			final View inoutimagebutton =  viewgroup
					.getChildAt(i);
			Animation animation = new TranslateAnimation(0F, xOffset, 
					0F, yOffset);

			animation.setFillAfter(true);
			animation.setDuration(duration);
			animation.setStartOffset(((counts- i) * 100)
					/ (-1 + viewgroup.getChildCount()));// 顺序倒一下比较舒服
			animation.setInterpolator(new AnticipateInterpolator(2F));
			animation.setAnimationListener(new Animation.AnimationListener() {
				@Override
				public void onAnimationStart(Animation arg0) {
				}

				@Override
				public void onAnimationRepeat(Animation arg0) {
				}

				@Override
				public void onAnimationEnd(Animation arg0) {
					Log.i(TAG,"onAnimationEnd");
					if(viewgroup.getChildAt(0)==inoutimagebutton){
						Log.i(TAG,"0 child 相等");
					}
					if(viewgroup.getChildAt(1)==inoutimagebutton){
						Log.i(TAG,"1 child 相等");
					}
					if(viewgroup.getChildAt(counts-1)==inoutimagebutton){
						inoutimagebutton.setVisibility(View.GONE);
						Log.i(TAG,inoutimagebutton.getVisibility()+"'");
//						viewgroup.setVisibility(View.GONE);			//无奈之举。。。当所有子都设置完后,将布局文件也设置为GONE.
						return ;
					}
					inoutimagebutton.setVisibility(View.GONE);//单独设置这个压根没用
					Log.i(TAG,inoutimagebutton.getVisibility()+"'");
				}
			});
			inoutimagebutton.startAnimation(animation);
			
//			inoutimagebutton.setVisibility(View.GONE);//即使在这里设置也毫无效果。
		}

	}

}


下面是我的布局文件。

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

    <RelativeLayout
        android:id="@+id/hide_fly_relative"
        android:layout_width="match_parent"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/trans_type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="@dimen/set_fly_dimens"
            android:src="@drawable/trans_logo_youdao"
            android:visibility="gone" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="@dimen/set_fly_dimens"
            android:src="@drawable/ic_launcher"
            android:visibility="gone" />

    </RelativeLayout>

    <EditText
        android:id="@+id/search_content"
        android:layout_width="match_parent"
        android:layout_height="@dimen/right_bar_width"
        android:ems="10"
        android:visibility="gone" >

        <requestFocus />
    </EditText>

 <RelativeLayout
        android:id="@+id/select_bar"
        android:layout_width="@dimen/right_bar_width"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:layout_alignParentRight="true"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/search"
            android:layout_width="match_parent"
            android:layout_height="@dimen/right_bar_width"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/ic_action_search" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_below="@id/search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/set"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/right_bar_width"
            android:layout_alignParentBottom="true"
            android:src="@drawable/set" />

    </RelativeLayout>

</RelativeLayout>


PathAnimation中的传入的ViewGroup 是hide_fly_relative。我试过其他的,PathMenu,其实他也没处理好这个问题。有时间得去看看源码。找找原因。

额,前几天找到原因了,其实是因为设置了Animation的setFillAfter(true)。所以在动画结束的时候,那个被施加动画的View 还在继续被动画占用。如果想要取消占用,自然考虑取消动画咯。view.clearAnimation()。结构,就ok了咯

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值