当我通过listview项目点击打开新活动时,我怎样才能实现幻灯片等动画转换?
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = null;
// global string to class
selectedValue = String.valueOf(parent.getItemAtPosition(position));
if (selectedValue.equals("item1")) {
// ^^^ use any item value here you want
Intent myIntent = new Intent(view.getContext(), activity1.class);
startActivityForResult(myIntent,0);
}
else if (selectedValue.equals("item2")) {
Intent myIntent = new Intent(view.getContext(), aactivity4.class);
startActivityForResult(myIntent,0);
}
}
});
答案 0 :(得分:2)
santoash kumar回答是正确的。如果您在R.anim.layout资源上挣扎,那么当您单击listview项时,动画代码就像其他聊天应用程序的工作方式(幻灯片动画)一样。
将这些添加到您的动画资源
R.anim.push_left_in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%" android:toXDelta="0" android:duration="@android:integer/config_mediumAnimTime"/>
</set>
R.anim.push_left_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-20%" android:duration="@android:integer/config_mediumAnimTime"/>
</set>
R.anim.push_right_in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-20%" android:toXDelta="0" android:duration="@android:integer/config_mediumAnimTime"/>
</set>
R.anim.push_right_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="100%" android:duration="@android:integer/config_mediumAnimTime"/>
</set>
在您要在启动时执行此动画的activity1或任何活动中,请在设置setContentView()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
//rest of your code include setContentView();
}
现在,当您尝试导航回listView活动时,您会发现问题,只需按下后退按钮或单击代表后退按钮的视图,动画似乎仍然是默认动画,所以当您尝试时执行此操作结束你目前的活动。
finish();
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
对于按下的后退按钮,请使用此代码。
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.push_right_in,R.anim.push_right_out);
}
如果我提供的动画并不适合您想要的动画,您可以尝试使用此link制作自己的自定义动画。
答案 1 :(得分:0)
startActivityForResult(myIntent,0);
overridePendingTransition(R.anim.hold, R.anim.fade_in);
hold和fade in将是动画xmls