Versuchen Sie den folgenden Code. Der Code verwendet Listview, aber die gleiche Logik kann für RecyclerView angewendet werden.
Der Ansatz besteht darin, eine neue Bildansicht im übergeordneten Layout der Listenansicht zu erstellen, die das angeklickte Bild überlappt. Übersetzen Sie dann die neu erstellte Bildansicht in die Mitte des Bildschirms.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
root = (RelativeLayout) findViewById(R.id.main_root);
listView = (ListView) findViewById(R.id.list);
MyAdapter adapter = new MyAdapter(MainActivity.this, web, imageId);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ImageView imgView = (ImageView) view.findViewById(R.id.grid_image);
// Get location of window with respect to window.
int location[] = new int[2];
imgView.getLocationInWindow(location);
// Create a new image view overlapping
// the image view that was clicked.
ImageView imgView2 = new ImageView(MainActivity.this);
imgView2.setImageDrawable(imgView.getDrawable());
// To make it overlap, use the location values of
// the clicked image as left and top margin for the
// new image.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
imgView.getWidth(), imgView.getHeight());
params.leftMargin = location[0];
params.topMargin = location[1] - getStatusBarHeight();
// Add the new image view to the root view of the activity.
root.addView(imgView2, params);
translateToCenter(imgView2, location);
}
});
}
/**
* To translate the new image view to the center of the screen.
* @param view
* @param originalLoc
*/
private void translateToCenter(View view , int originalLoc[])
{
int xMove = root.getWidth()/2 - view.getWidth()/2 - originalLoc[0];
int yMove = root.getHeight()/2 - view.getHeight()/2 - originalLoc[1];
TranslateAnimation anim = new TranslateAnimation(0, xMove , 0, yMove);
anim.setDuration(1000);
anim.setFillAfter(true);
view.startAnimation(anim);
}
/**
* To get the status bar height.
* @return
*/
private int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier(
"status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
Bitte zeigen Sie uns, was Sie bisher versucht haben. Vielen Dank. –