package com.naman14.playanimations;
import android.app.SharedElementCallback;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.naman14.playanimations.adapter.GamesData;
import com.naman14.playanimations.adapter.RecentGamesAdapter;
import java.util.ArrayList;
import java.util.List;
/**
* Created by naman on 27/05/15.
*/
public class RecentGamesFragment extends Fragment {
private RecyclerView mRecyclerView;
private RecentGamesAdapter mAdapter;
private List<GamesData> gamesList = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_games, null);
mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing_card);
mRecyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));
mRecyclerView.setHasFixedSize(true);
setUpGameList();
getActivity().setExitSharedElementCallback(new SharedElementCallback() {
@Override
public Parcelable onCaptureSharedElementSnapshot(View sharedElement, Matrix viewToGlobalMatrix, RectF screenBounds) {
int bitmapWidth = Math.round(screenBounds.width());
int bitmapHeight = Math.round(screenBounds.height());
Bitmap bitmap = null;
if (bitmapWidth > 0 && bitmapHeight > 0) {
Matrix matrix = new Matrix();
matrix.set(viewToGlobalMatrix);
matrix.postTranslate(-screenBounds.left, -screenBounds.top);
bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.concat(matrix);
sharedElement.draw(canvas);
}
return bitmap;
}
});
return v;
}
private void setUpGameList() {
if (gamesList == null) {
gamesList = new ArrayList<GamesData>();
}
for (int i = 0; i < 9; i++) {
GamesData game = new GamesData();
game.setgameName("Game Name" + i);
game.setPublisher("Publisher" + i);
game.setIcon(R.drawable.zombie_icon);
game.setCover(R.drawable.zombie_cover);
gamesList.add(game);
}
mAdapter = new RecentGamesAdapter(getActivity(), gamesList);
mRecyclerView.setAdapter(mAdapter);
}
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public SpacesItemDecoration(int space) {
this.space = space;
}
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
outRect.left = space;
outRect.right = space;
outRect.bottom = space;
}
}
}