MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView listView;
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView) findViewById(R.id.listview);
adapter=new MyAdapter(MainActivity.this);
listView.setAdapter(adapter);
}
}
MyAdapter.java
注意:点击图片会有转场动画出现,仔细看onClick的写法
public class MyAdapter extends BaseAdapter {
private Context context;
List list=new ArrayList();
MyAdapter(Context context) {
this.context = context;
list.add(R.drawable.anim1);
list.add(R.drawable.anim2);
list.add(R.drawable.anim3);
// list.add(R.drawable.anim4);
// list.add(R.drawable.anim5);
// list.add(R.drawable.anim6);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView.findViewById(R.id.img_avatar);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageView.setImageResource((int)list.get(position));
holder.imageView.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(View v) {
//动画在这里
ActivityOptions options =
ActivityOptions.makeSceneTransitionAnimation((Activity) context, v, context.getString(R.string.image_transition_name));
Intent intent=new Intent(context,DtailActivity.class);
intent.putExtra(DtailActivity.EXTRA_IMAGE,(int)getItem(position) );
context.startActivity(intent,options.toBundle());
}
});
return convertView;
}
private class ViewHolder {
ImageView imageView;
}
}
DtailActivity.java
public class DtailActivity extends AppCompatActivity {
public static final java.lang.String EXTRA_IMAGE = DtailActivity.class.getSimpleName() + ".IMAGE";
ImageView imgAvatar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dtail);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
imgAvatar= (ImageView) findViewById(R.id.img_avatar);
imgAvatar.setImageResource(getIntent().getExtras().getInt(EXTRA_IMAGE));
}
}
第二个页面布局文件
关键:注意imageView的 android:transitionName=”@string/image_transition_name”
"1.0" encoding="utf-8"?>
.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.qunar.yuzhiyun.animationtransition.DtailActivity">
.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
"@+id/img_avatar"
android:layout_width="match_parent"
android:layout_height="200dp"
android:fitsSystemWindows="true"
android:scaleType="fitXY"
android:transitionName="@string/image_transition_name"
app:layout_collapseMode="parallax"
android:src="@drawable/anim2"/>
.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"/>
.support.design.widget.CollapsingToolbarLayout>
.support.design.widget.AppBarLayout>
.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
.support.v7.widget.CardView
android:id="@+id/cardview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/activity_horizontal_margin">
"@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin"
android:text="@string/detailText"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
.support.v7.widget.CardView>
.support.v4.widget.NestedScrollView>
.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
.support.design.widget.CoordinatorLayout>