2013-10-29 11 views
0

我想從左至右移動一個框中的父母如何從左到右移動一個框並將其留在android中?

然後將它留在那裏,直到用戶點擊它,然後將其移回到左側並將其留在那裏直到另一次單擊完成。

但是我只能從左到右移動。它不會離開向右

這裏是我的xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

     <ImageView 
     android:id="@+id/switch_bg" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:background="@drawable/switch_bg" 
     android:layout_gravity= "right"/> 

    <RelativeLayout 
     android:id="@+id/handle_right" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/handle" 

     android:visibility="visible"> 

     <ImageView 
      android:id="@+id/switch_v_right" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:src="@drawable/switch_v" /> 
    </RelativeLayout> 

     <RelativeLayout 
     android:id="@+id/handle_left" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/handle" 

    android:visibility="gone"> 

     <ImageView 
      android:id="@+id/switch_v_left" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:src="@drawable/switch_v" /> 
    </RelativeLayout> 



</RelativeLayout> 

我的轉變XML:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<translate android:fromXDelta="0%p" android:toXDelta="80%p" android:duration="600"/> 
    <alpha android:fromAlpha="1.0" 
       android:toAlpha="0.1"/> 
</set> 

這裏是我的類:

public class SettingsSwitchView extends RelativeLayout { 

     private static final int TRANSITION_TIME = 1; 
     private LayoutInflater inflater; 


     private ImageView switch_bg; 
     private RelativeLayout boxImage_left; 
     private ImageView vImage_left; 
     private RelativeLayout boxImage_right; 
     private ImageView vImage_right; 

     private boolean isChecked; 

     public SettingsSwitchView(Context context, AttributeSet attrs) { 
      super(context, attrs); 
      inflater = LayoutInflater.from(context); 
      inflater.inflate(R.layout.settings_switch, this); 

      switch_bg = (ImageView) findViewById(R.id.switch_bg); 
      boxImage_left = (RelativeLayout) findViewById(R.id.handle_left); 
      vImage_left = (ImageView) findViewById(R.id.switch_v_left); 

      boxImage_right = (RelativeLayout) findViewById(R.id.handle_right); 
      vImage_right = (ImageView) findViewById(R.id.switch_v_right); 

      isChecked = false; //benda: read from config file 

      switch_bg.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 


        //    com.waze.view.anim.AnimationUtils.openNavigateScreenWithFadeIn(vImage); 

        //    
    //    image1.setAlpha(progress); 
    //    Image2.setAlpha(100-progress); 

        if (!isChecked) 
        { 

        Animation animation = android.view.animation.AnimationUtils.loadAnimation(AppService.getAppContext(), com.waze.R.anim.slide_from_left_with_fade_out); 
        animation.setInterpolator(new AccelerateInterpolator()); 
        boxImage_left.startAnimation(animation); 

        boxImage_right.setVisibility(View.VISIBLE); 

        boxImage_left.setVisibility(View.GONE); 

        } 
        else 
        { 
         Animation animation = android.view.animation.AnimationUtils.loadAnimation(AppService.getAppContext(), com.waze.R.anim.slide_from_right_with_fade_in); 
         animation.setInterpolator(new AccelerateInterpolator()); 
         boxImage_right.startAnimation(animation); 

         boxImage_left.setVisibility(View.VISIBLE); 

         boxImage_right.setVisibility(View.GONE); 


        } 

      TransitionDrawable transition = (TransitionDrawable) switch_bg.getBackground(); 
      transition.reverseTransition(TRANSITION_TIME); 

      isChecked = !isChecked; 

     } 
    }); 
} 

}

回答

0

你可以使用

  TranslateAnimation anim=new TranslateAnimation(0,50,0,0); 
      anim.setDuration(1000);   
      v.startAnimation(anim); 

此移動到right.for左改變參數。

相關問題