我想在它的父级中从左到右移动一个框
然后将其留在那里直到用户单击它,然后将其移回左侧并留在那里直到完成另一次单击。
但是我只能从左向右移动。不要把它放在右边
这是我的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;
}
});
}
}