Java 执行推送片段动画
如何制作一个动画,将当前片段按下一个片段移动 以下是我想要的动画: 我当前的动画代码只是将第一个片段与第二个片段重叠,它没有像图片中那样推动它 代码如下:Java 执行推送片段动画,java,android,animation,fragment,Java,Android,Animation,Fragment,如何制作一个动画,将当前片段按下一个片段移动 以下是我想要的动画: 我当前的动画代码只是将第一个片段与第二个片段重叠,它没有像图片中那样推动它 代码如下: result_list.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> av, final View view,
result_list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> av, final View view,
final int i, long i2) {
result_list.setEnabled(false);
view.animate().setDuration(300).translationX(widthListView).alpha(0).
withEndAction(new Runnable() {
@Override
public void run() {
//setResult(Activity.RESULT_OK,new Intent().putExtra("bussStopCode", data.get(i).getStopCode()).putExtra("bussStopName", data.get(i).getStopName()));
////int get 1
//data.remove(i);
int temporaryInteger = i;
listLastPostion = temporaryInteger;
//customAdapter.notifyDataSetChanged();
//view.setTranslationX(0);
Log.d("data",conreq.getCollectedData().getBusRouteSetData().get(temporaryInteger - 1).getRouteHeading());
Bundle bundle = new Bundle();
bundle.putString("busdestination", conreq.getCollectedData().getBusRouteSetData().get(temporaryInteger-1).getRouteHeading());
bundle.putString("busnumber", conreq.getCollectedData().getBusRouteSetData().get(temporaryInteger-1).getRouteNo());
Fragment fragment = new FragmentNextTripForStop();
fragment.setArguments(bundle);
FragmentTransaction fragmentManager = getFragmentManager().beginTransaction();
fragmentManager.setCustomAnimations(R.anim.right_left_anim_x_left,R.anim.right_left_anim_x_right,R.anim.left_right_anim_x_left,R.anim.left_right_anim_x_right);
fragmentManager.add(R.id.fragment_searched_data_xml, fragment).addToBackStack(null).commit();
// finish();
//overridePendingTransition(R.anim.right_left_anim_x_left,R.anim.right_left_anim_x_right);
}
});
}});
result\u list.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共虚空onItemClick(AdapterView av、最终视图、,
最终整数i,长整数i2){
结果_list.setEnabled(假);
view.animate().setDuration(300).translationX(widthListView).alpha(0)。
withEndAction(新的可运行(){
@凌驾
公开募捐{
//setResult(Activity.RESULT_OK,new Intent().putExtra(“BussTopCode”,data.get(i).getStopCode()).putExtra(“BussTopName”,data.get(i).getStopName());
////整数得到1
//数据删除(i);
int临时整数=i;
listLastPostion=临时整数;
//customAdapter.notifyDataSetChanged();
//view.setTranslationX(0);
Log.d(“数据”,conreq.getCollectedData().getBusRouteSetData().get(临时整数-1.getRouteHeading());
Bundle=新Bundle();
bundle.putString(“busdestination”,conreq.getCollectedData().getBusRouteSetData().get(临时整数-1.getRouteHeading());
bundle.putString(“busnumber”,conreq.getCollectedData().getBusRouteSetData().get(临时整数-1.getRouteNo());
Fragment Fragment=new FragmentNextTripForStop();
fragment.setArguments(bundle);
FragmentTransaction fragmentManager=getFragmentManager().beginTransaction();
fragmentManager.setCustomAnimations(R.anim.right_left_anim_x_left,R.anim.right_left_anim_x_right,R.anim.left_right_anim_x_left,R.anim.left_right_anim_x_right);
fragmentManager.add(R.id.fragment\u searched\u data\u xml,fragment).addToBackStack(null.commit();
//完成();
//重写转换(R.anim.right\u left\u anim\u x\u left,R.anim.right\u left\u anim\u x\u right);
}
});
}});
尽管这个问题是重复的,但这个问题提供的答案对许多观众来说并不适用,需要他们对此做出一些假设
这是一个很好的问题,考虑到许多应用程序中都存在此功能,但它需要一个非常详细的答案。我将尝试将答案分解为一系列划分的步骤,以确保它是可重复的
问题
阻碍我们大多数人轻易做到这一点的问题是一个复合性质的问题。为了充分理解这个问题,让我列出以下问题:
- 我们不应该使用典型的动画框架,因为
和FragmentManager
类提供了一种方便的方法,FragmentTransaction
,需要setCustomAnimations(int,int,int-int)
ObjectAnimators
- 此外,我们不能在
中使用百分比标记(传统方法)ObjectAnimators
- 最后,我们只能在运行时确定屏幕宽度,因此必须在我们自己的布局中具有自定义功能
片段定义一个自定义布局,因为我们必须具有对屏幕宽度的运行时访问权限,以及一个基于此宽度操纵视图(布局)x位置的自包含方法
FractionTranslateLineEarlayout.java
objectAnimator
.xml文件,用于在屏幕上转换视图。请注意,我们将需要其中的四个文件,因为我们需要一个用于每个进程(out和in),另一个用于每个进程(left和right)
slide_left_out.xml
活动
主类
- 此示例使用原始的
类,因此除非进行必要的修改,否则将无法使用支持包Fragment
- 本例要求您使用的是
或更高版本Android API 13.0
- 本示例的目的是提供信息,并希望解释此处涉及的问题。在您自己的编辑器中拼接代码是您应该自行决定的事情李>
希望这里有足够的细节来回答你的问题。如果你还需要什么,请告诉我
此时,应该有足够的代码来实现您自己的解决方案。我确实是在没有Eclipse编辑器支持的情况下输入的,因此如果有任何错误,请提前接受我的道歉 复制我猜:)惊讶地看到这是唯一的方式,即使现在!虽然这会变得更容易,但我有一个PreferenceFragment,它是从xml加载的,所以我不确定如何将它包含在我的自定义Fra中
public class FractionTranslateLinearLayout extends LinearLayout{
private int screenWidth;
private float fractionX;
protected void onSizeChanged(int w, int h, int oldW, int oldh){
// Assign the actual screen width to our class variable.
screenWidth = w;
super.onSizeChanged(w, h, oldW, oldH);
}
public float getFractionX(){
return fractionX;
}
public void setFractionX(float xFraction){
this.fractionX = xFraction;
// When we modify the xFraction, we want to adjust the x translation
// accordingly. Here, the scale is that if xFraction is -1, then
// the layout is off screen to the left, if xFraction is 0, then the
// layout is exactly on the screen, and if xFraction is 1, then the
// layout is completely offscreen to the right.
setX((screenWidth > 0) ? (xFraction * screenWidth) : 0);
}
}
<com.[your_package_here].FractionTranslateLinearLayout
// Omitted namespace.
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text_view_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment 1" />
</com.[your_package_here].FractionTranslateLinearLayout>
<com.[your_package_here].FractionTranslateLinearLayout
// Omitted namespace.
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text_view_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment 2" />
</com.[your_package_here].FractionTranslateLinearLayout>
public class Fragment1 extends Fragment {
public View onCreateView(LayoutInflater inf, ViewGroup vg, Bundle b){
// Simply inflate the View from the .xml file.
return inf.inflate(R.layout.fragment_1, vg, false);
}
}
public class Fragment2 extends Fragment {
public View onCreateView(LayoutInflater inf, ViewGroup vg, Bundle b){
// Simply inflate the View from the .xml file.
return inf.inflate(R.layout.fragment_2, vg, false);
}
public void onActivityCreated (Bundle savedInstanceState){
View v = getView();
FractionTranslateLinearLayout layout;
layout = (FractionTranslateLinearLayout) v.findViewById(R.id.layout);
// Move the entire View off to the right of the screen for now.
layout.setFractionX(1.0f);
}
}
<objectAnimator
// Omitted namespace.
android:valueFrom="0"
android:valueTo="-1"
// This String must be the exact name of the class variable.
android:propertyName="xFraction"
android:valueType="floatType"
// Duration in milliseconds.
android:duration="500"/>
<objectAnimator
// Omitted namespace.
android:valueFrom="0"
android:valueTo="1"
// This String must be the exact name of the class variable.
android:propertyName="xFraction"
android:valueType="floatType"
// Duration in milliseconds.
android:duration="500"/>
<objectAnimator
// Omitted namespace.
android:valueFrom="-1"
android:valueTo="0"
// This String must be the exact name of the class variable.
android:propertyName="xFraction"
android:valueType="floatType"
// Duration in milliseconds.
android:duration="500"/>
<objectAnimator
// Omitted namespace.
android:valueFrom="1"
android:valueTo="0"
// This String must be the exact name of the class variable.
android:propertyName="xFraction"
android:valueType="floatType"
// Duration in milliseconds.
android:duration="500"/>
<com.android.FrameLayout
// Omitted namespace.
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
public class Main extends Activity {
private boolean showingFirstFragment;
public void onCreate(Bundle savedInstanceState){
setContentView(R.layout.main);
FragmentManager manager = getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
// Keep track of which Fragment we are facing.
showingFirstFragment = true;
// Add the first Fragment to the container.
trans.add(R.id.main_container, new Fragment1(), "fragment_1");
trans.commit();
}
public void onBackPressed(){
// Override the back button functionality to simply switch
// Fragments. Note that this would normally be done in a click
// click listener.
switchFragments();
}
private void switchFragments(){
FragmentManager manager = getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
// Set the animations that will emulate the functionality you
// requested.
int rightIn = R.animator.slide_right_in;
int rightOut = R.animator.slide_right_out;
int leftIn = R.animator.slide_left_in;
int leftOut = R.animator.slide_left_out;
// Note that we pass in 4 animations here. Please see the
// documentation on this method as it is critical to the
// understanding of this solution.
trans.setCustomAnimations(rightIn, leftOut, leftIn, rightOut);
if(showingFirstFragment){
int container = R.id.main_container;
// Show the second Fragment.
trans.replace(container, new Fragment2(), "fragment_2");
trans.commit();
showingFirstFragment = false;
}
else{
// Show the first Fragment by popping the back stack!
manager.popBackStack(null);
showingFirstFragment = true;
}
}
}