Skip to content

Commit

Permalink
fix(md-3858): cell gap and image position/scaling (#143)
Browse files Browse the repository at this point in the history
* fix: image scaling

* fix: cleanup
  • Loading branch information
gruskal committed Nov 17, 2022
1 parent 3e184f8 commit c73dc39
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 56 deletions.
Expand Up @@ -18,6 +18,7 @@
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

@SuppressLint("ViewConstructor")
public class CellView extends RelativeLayout implements SelectionsObserver {
Expand All @@ -40,7 +41,7 @@ public class CellView extends RelativeLayout implements SelectionsObserver {
this.isInFirstColumnRecyclerView = isInFirstColumnRecyclerView;
this.dragBoxEventHandler = tableView.dragBoxEventHandler;

RelativeLayout wrapper = null;
FrameLayout wrapper = null;
RelativeLayout.LayoutParams wrapperLayout = null;
switch (type) {
case "text":
Expand All @@ -49,7 +50,7 @@ public class CellView extends RelativeLayout implements SelectionsObserver {
content = textView;
break;
case "image":
wrapper = new RelativeLayout(context);
wrapper = new FrameLayout(context);
wrapperLayout = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
content = new ClickableImageView(context, selectionsEngine, tableView, this);
wrapper.addView((View) content);
Expand Down Expand Up @@ -196,7 +197,6 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
if(column == null) {
return;
}

layout.width = column.width;
setLayoutParams(layout);
}
Expand Down
Expand Up @@ -20,9 +20,13 @@
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

@SuppressLint("ViewConstructor")
public class ClickableImageView extends androidx.appcompat.widget.AppCompatImageView implements Content {
int imageHeight;
int imageWidth;

DataCell cell = null;
boolean selected = false;
String scaleType = null;
Expand All @@ -41,112 +45,112 @@ public class ClickableImageView extends androidx.appcompat.widget.AppCompatImage
}

private void alwaysFit() {
RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(tableView.rowHeight, tableView.rowHeight);
this.setLayoutParams(layout);

this.setScaleType(ScaleType.FIT_XY);

imageHeight = tableView.rowHeight;
imageWidth = imageHeight;

scaleType = "alwaysFit";
}

private void stretchToFit(DataColumn column) {
RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(tableView.rowHeight, column.width);
setLayoutParams(layout);

private void stretchToFit(DataColumn column) {
this.setScaleType(ScaleType.FIT_XY);

imageHeight = tableView.rowHeight;
imageWidth = column.width;

scaleType = "stretchToFit";
}

private void fitToHeight(Bitmap image) {
float height = image.getHeight();
float width = image.getWidth();
float aspectRatioMultiplier = width/height;

RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(Math.round(tableView.rowHeight * aspectRatioMultiplier), tableView.rowHeight);
setLayoutParams(layout);

private void fitToHeight(float aspectRatioMultiplier) {
this.setScaleType(ScaleType.FIT_XY);

imageHeight = tableView.rowHeight;
imageWidth = Math.round(tableView.rowHeight * aspectRatioMultiplier);

scaleType = "fitToHeight";
}

private void fitToWidth(DataColumn column, Bitmap image) {
float height = image.getHeight();
float width = image.getWidth();
float aspectRatioMultiplier = height/width;
private void fitToWidth(DataColumn column, float aspectRatioMultiplier) {
this.setScaleType(ScaleType.MATRIX);

RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
setLayoutParams(layout);
imageHeight = Math.round(column.width * aspectRatioMultiplier);
imageWidth = column.width;

RelativeLayout parent = (RelativeLayout) getParent();
if(parent == null) {
return;
}
RelativeLayout.LayoutParams wrapperLayout = (RelativeLayout.LayoutParams) parent.getLayoutParams();
wrapperLayout.width = column.width;
wrapperLayout.height = Math.round(column.width * aspectRatioMultiplier);
parent.setLayoutParams(wrapperLayout);
scaleType = "fitToWidth";
}

this.setScaleType(ScaleType.FIT_XY);
public void scaleAndPositionImage(DataColumn column, Bitmap image) {
setSizing(column, image);
setAlignment(column);
FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(imageWidth, imageHeight);
setLayoutParams(layout);
}

scaleType = "fitToWidth";
private void shrinkParentToBounds() {
FrameLayout wrapper = (FrameLayout) getParent();
RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(imageWidth, imageHeight);
wrapper.setLayoutParams(layout);
}

public void setSizing(DataColumn column, Bitmap image) {
private void setSizing(DataColumn column, Bitmap image) {
float height = image.getHeight();
float width = image.getWidth();

switch (column.representation.imageSize) {
case "fill":
stretchToFit(column);
shrinkParentToBounds();
break;
case "fitHeight":
fitToHeight(image);
fitToHeight(width/height);
shrinkParentToBounds();
break;
case "fitWidth":
fitToWidth(column, image);
fitToWidth(column, height/width);
FrameLayout wrapper = (FrameLayout) getParent();
RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
wrapper.setLayoutParams(layout);
break;
default:
case "alwaysFit":
alwaysFit();
shrinkParentToBounds();
break;
}
}

public void setAlignment(DataColumn column) {
RelativeLayout wrapper = (RelativeLayout) getParent();
private void setAlignment(DataColumn column) {
setTranslationY(0);

if(wrapper == null) {
return;
}

switch (column.representation.imagePosition) {
case "topCenter":
wrapper.setGravity(Gravity.LEFT);
cellView.setGravity(Gravity.LEFT);
break;
case "bottomCenter":
wrapper.setGravity(Gravity.RIGHT);
cellView.setGravity(Gravity.RIGHT);
break;
case "centerLeft":
if (scaleType.equals("fitToWidth")) {
wrapper.setGravity(Gravity.TOP);
cellView.setGravity(Gravity.TOP);
break;
}
wrapper.setGravity(Gravity.CENTER);
cellView.setGravity(Gravity.CENTER);
break;
case "centerRight":
if (scaleType.equals("fitToWidth")) {
wrapper.setGravity(Gravity.BOTTOM);
setTranslationY(tableView.rowHeight - cellView.getMinimumHeight());
cellView.setGravity(Gravity.BOTTOM);
setTranslationY(tableView.rowHeight - imageHeight);
break;
}
wrapper.setGravity(Gravity.CENTER);
cellView.setGravity(Gravity.CENTER);
break;
default:
case "centerCenter":
if (scaleType.equals("fitToWidth")) {
setTranslationY((float) (tableView.rowHeight / 2 - cellView.getMinimumHeight() / 2));
setTranslationY((float) (tableView.rowHeight - imageHeight) / 2);
}
wrapper.setGravity(Gravity.CENTER);
cellView.setGravity(Gravity.CENTER);
break;
}
}
Expand Down
Expand Up @@ -54,8 +54,7 @@ public void setData(DataRow dataRow, int rowHeight, CellContentStyle cellContent
}
ClickableImageView imageView = (ClickableImageView) cellView.content;
imageView.setImageBitmap(imageBitmap);
imageView.setSizing(column, imageBitmap);
imageView.setAlignment(column);
imageView.scaleAndPositionImage(column, imageBitmap);
} else if(column.representation.type.equals("miniChart")) {
CellView cellView = (CellView) row.getChildAt(columnIndex);
LinearLayout.LayoutParams cellViewLayoutParams = new LinearLayout.LayoutParams(column.width, ViewGroup.LayoutParams.MATCH_PARENT);
Expand Down Expand Up @@ -113,8 +112,7 @@ public void updateColumnRepresentation() {
if(imageBitmap == null) {
continue;
}
imageView.setSizing(column, imageBitmap);
imageView.setAlignment(column);
imageView.scaleAndPositionImage(column, imageBitmap);
}
}
}
Expand Down

0 comments on commit c73dc39

Please sign in to comment.