Skip to content

Instantly share code, notes, and snippets.

@cbedoy
Created November 19, 2014 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbedoy/618ad6d5ba26b3e091dd to your computer and use it in GitHub Desktop.
Save cbedoy/618ad6d5ba26b3e091dd to your computer and use it in GitHub Desktop.
MosaicService
import java.util.ArrayList;
import java.util.Random;
/**
* --------------------------------------------------------
* Created by Carlos Bedoy on 18/11/14.
* MosaicTest
* Mobile Developer
* Aguascalientes Mexico
* Email: carlos.bedoy@gmail.com
* Facebook: https://www.facebook.com/carlos.bedoy
* ---------CODE && MUSIC ----------------------------------
*/
public class MosaicService
{
private Random random;
private ArrayList<MosaicView> dataModel;
public MosaicService(){
this.dataModel = new ArrayList<MosaicView>();
this.random = new Random(600);
this.fillData();
}
private void fillData(){
int[] w = new int[]{386,488,576,584};
int[] h = new int[]{475,374,380,483};
for(int i = 0; i<w.length; i++){
MosaicView mosaicView = new MosaicView();
Info info = new Info();
info.setFrame(new CGRect(0,0,w[i], h[i]));
mosaicView.setInfo(info);
dataModel.add(mosaicView);
}
}
public int fuckingInt(int min, int max){
int i = random.nextInt((max - min) + 1) + min;
return i;
}
public void doAny(){
ArrayList<Info> infoForCells = new ArrayList<Info>();
int numberOfCells = dataModel.size();
int CELL_PADDING = 3;
int CELL_MAX_HEIGHT = 168;
float lastCellRightX = CELL_PADDING;
float rowBottomY = 0;
float rowTopY = CELL_PADDING;
int rowFirstIndex = 0;
int rowLastIndex = 0;
float boundsHeight = 1024;
float boundsWidth = 720;
for(int i = 0 ; i<numberOfCells; i++){
CGSize size = dataModel.get(i).getInfo().getFrame().size;
float ratio = size.width / size.height;
CGSize actualSize = new CGSize(CELL_MAX_HEIGHT*ratio, CELL_MAX_HEIGHT);
if(actualSize.width>= boundsWidth - lastCellRightX - CELL_PADDING){
actualSize.width = boundsWidth - lastCellRightX - CELL_PADDING;
actualSize.height = actualSize.width/ratio;
rowLastIndex = i;
}
if(FLOAT_CHECK_EQUAL(lastCellRightX, CELL_PADDING)){
rowBottomY = actualSize.height;
rowFirstIndex = i;
}
Info info = new Info();
info.setFrame(new CGRect(lastCellRightX, rowTopY, actualSize.width, actualSize.height));
info.setIndex(i);
info.setRatio(ratio);
infoForCells.add(info);
lastCellRightX += actualSize.width + CELL_PADDING;
if(lastCellRightX > boundsWidth)
{
if(rowLastIndex >= rowFirstIndex)
{
float ratios = 0.f;
for(int jj = rowFirstIndex; jj<rowLastIndex; jj++)
{
Info __info = infoForCells.get(jj);
ratios += __info.getRatio();
}
rowBottomY = (boundsWidth - ((rowLastIndex - rowFirstIndex + 2) * CELL_PADDING))/ratios;
lastCellRightX = CELL_PADDING;
for(int jj= rowFirstIndex; jj<rowLastIndex; jj++)
{
Info __info = infoForCells.get(jj);
CGRect cellFrame = __info.getFrame();
cellFrame.origin.x = lastCellRightX;
cellFrame.size.height = rowBottomY;
cellFrame.size.width = rowBottomY * __info.getRatio();
__info.setFrame(cellFrame);
lastCellRightX +=CELL_PADDING + cellFrame.size.width;
}
}
lastCellRightX = CELL_PADDING;
if(i !=numberOfCells -1)
rowTopY += rowBottomY + CELL_PADDING;
else{
}
}
float maxHeight = rowBottomY+rowTopY+CELL_PADDING;
CGSize contentSize = new CGSize(0.0f, maxHeight);
}
for(int index = 0; index<dataModel.size(); index++){
Info info1 = infoForCells.get(index);
MosaicView mosaicView = dataModel.get(index);
mosaicView.setInfo(info1);
mosaicView.reload();
}
}
private boolean FLOAT_CHECK_EQUAL(float leftValue, float rightValue) {
return (Math.abs((leftValue)-(rightValue)) < 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment