android - How to stop a GridView from cropping my images? -
i have gridview have filled 64 60x60px png's. want gridview display them close perfect square can have set numcolumns in xml 8 have 8x8 gird.
here looks like:
my images have small border @ edge though being cropped off. here drew on top left image should when displayed:
here xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/textfieldfu" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <gridview android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="600dp" android:numcolumns="8" android:verticalspacing="10dp" android:horizontalspacing="0dp" android:stretchmode="columnwidth" android:gravity="center" /> </relativelayout>
when using 40x40px , 50x50px size png's worked fine, small see little symbols. have changed in xml think of no matter how spacing give or give it, images stay cropped when there ample room.
how can make gridview display full, un-cropped images?
for love of holy, i'm dumb. had forgotten in "imageadapter" class had set had used imageview setlayoutparams method , set them (50, 50). sorry wasting peoples time.
@override public view getview(int position, view convertview, viewgroup parent) { imageview iv; if (convertview != null) { iv = (imageview) convertview; } else { iv = new imageview(context); ******iv.setlayoutparams(new gridview.layoutparams(50, 50));****** iv.setscaletype(scaletype.center); iv.setpadding(0, 0, 0, 0); } iv.setimageresource(images[position]); return iv; }
to use flowlayout make java class called flowlayout
custom control in android.
.../src/flowlayout.java:
import android.content.context; import android.content.res.typedarray; import android.graphics.canvas; import android.util.attributeset; import android.view.view; import android.view.viewgroup; public class flowlayout extends viewgroup { public static final int horizontal = 0; public static final int vertical = 1; private int horizontalspacing = 20; private int verticalspacing = 20; private int orientation = 0; private int innerpadding = 12; public flowlayout(context context) { super(context); } public flowlayout(context context, attributeset attributeset) { super(context, attributeset); } public flowlayout(context context, attributeset attributeset, int defstyle) { super(context, attributeset, defstyle); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { int sizewidth = measurespec.getsize(widthmeasurespec) - this.getpaddingright() - this.getpaddingleft(); int sizeheight = measurespec.getsize(heightmeasurespec) - this.getpaddingright() - this.getpaddingleft()+innerpadding; int modewidth = measurespec.getmode(widthmeasurespec); int modeheight = measurespec.getmode(heightmeasurespec); int size; int mode; if (orientation == horizontal) { size = sizewidth; mode = modewidth; } else { size = sizeheight; mode = modeheight; } int linethicknesswithspacing = 0; int linethickness = 0; int linelengthwithspacing = 0; int linelength; int prevlineposition = 0; int controlmaxlength = 0; int controlmaxthickness = 0; final int count = getchildcount(); (int = 0; < count; i++) { final view child = getchildat(i); if (child.getvisibility() == gone) continue; child.measure ( measurespec.makemeasurespec(sizewidth, modewidth == measurespec.exactly ? measurespec.at_most : modewidth), measurespec.makemeasurespec(sizeheight, modeheight == measurespec.exactly ? measurespec.at_most : modeheight) ); layoutparams lp = (layoutparams) child.getlayoutparams(); int hspacing = this.gethorizontalspacing(lp); int vspacing = this.getverticalspacing(lp); int childwidth = child.getmeasuredwidth(); int childheight = child.getmeasuredheight(); int childlength; int childthickness; int spacinglength; int spacingthickness; if (orientation == horizontal) { childlength = childwidth; childthickness = childheight; spacinglength = hspacing; spacingthickness = vspacing; } else { childlength = childheight; childthickness = childwidth; spacinglength = vspacing; spacingthickness = hspacing; } linelength = linelengthwithspacing + childlength; linelengthwithspacing = linelength + spacinglength; boolean newline = lp.newline || (mode != measurespec.unspecified && linelength > size); if (newline) { prevlineposition = prevlineposition + linethicknesswithspacing; linethickness = childthickness; linelength = childlength; linethicknesswithspacing = childthickness + spacingthickness; linelengthwithspacing = linelength + spacinglength; } linethicknesswithspacing = math.max(linethicknesswithspacing, childthickness + spacingthickness); linethickness = math.max(linethickness, childthickness); int posx; int posy; if (orientation == horizontal) { posx = innerpadding + getpaddingleft() + linelength - childlength; posy = getpaddingtop() + prevlineposition; } else { posx = getpaddingleft() + prevlineposition; posy = innerpadding + getpaddingtop() + linelength - childheight; } lp.setposition(posx, posy); controlmaxlength = math.max(controlmaxlength, linelength); controlmaxthickness = prevlineposition + linethickness; } if (orientation == horizontal) this.setmeasureddimension(resolvesize(controlmaxlength, widthmeasurespec), resolvesize(controlmaxthickness, heightmeasurespec)); else this.setmeasureddimension(resolvesize(controlmaxthickness, widthmeasurespec), resolvesize(controlmaxlength, heightmeasurespec)); } private int getverticalspacing(layoutparams lp) { int vspacing; if (lp.verticalspacingspecified()) vspacing = lp.verticalspacing; else vspacing = this.verticalspacing; return vspacing; } private int gethorizontalspacing(layoutparams lp) { int hspacing; if (lp.horizontalspacingspecified()) hspacing = lp.horizontalspacing; else hspacing = this.horizontalspacing; return hspacing; } @override protected void onlayout(boolean changed, int l, int t, int r, int b) { final int count = getchildcount(); (int = 0; < count; i++) { view child = getchildat(i); layoutparams lp = (layoutparams) child.getlayoutparams(); child.layout(lp.x, lp.y, lp.x + child.getmeasuredwidth(), lp.y + child.getmeasuredheight()); } } @override protected boolean drawchild(canvas canvas, view child, long drawingtime) { return super.drawchild(canvas, child, drawingtime); } @override protected boolean checklayoutparams(viewgroup.layoutparams p) { return p instanceof layoutparams; } @override protected layoutparams generatedefaultlayoutparams() { return new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); } @override public layoutparams generatelayoutparams(attributeset attributeset) { return new layoutparams(getcontext(), attributeset); } @override protected layoutparams generatelayoutparams(viewgroup.layoutparams p) { return new layoutparams(p); } public static class layoutparams extends viewgroup.layoutparams { private static int no_spacing = -1; private int x; private int y; private int horizontalspacing = no_spacing; private int verticalspacing = no_spacing; private boolean newline = false; public layoutparams(context context, attributeset attributeset) { super(context, attributeset); this.readstyleparameters(context, attributeset); } public layoutparams(int width, int height) { super(width, height); } public layoutparams(viewgroup.layoutparams layoutparams) { super(layoutparams); } public boolean horizontalspacingspecified() { return horizontalspacing != no_spacing; } public boolean verticalspacingspecified() { return verticalspacing != no_spacing; } public void setposition(int x, int y) { this.x = x; this.y = y; } private void readstyleparameters(context context, attributeset attributeset) { typedarray = context.obtainstyledattributes(attributeset, r.styleable.flowlayout_layoutparams); try { horizontalspacing = a.getdimensionpixelsize(r.styleable.flowlayout_layoutparams_layout_horizontalspacing, no_spacing); verticalspacing = a.getdimensionpixelsize(r.styleable.flowlayout_layoutparams_layout_verticalspacing, no_spacing); newline = a.getboolean(r.styleable.flowlayout_layoutparams_layout_newline, false); } { a.recycle(); } } } }
then create custom attributes views going inside flow layout view.
.../res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="flowlayout_layoutparams"> <attr name="layout_newline" format="boolean"/> <attr name="layout_horizontalspacing" format="dimension"/> <attr name="layout_verticalspacing" format="dimension"/> </declare-styleable> </resources>
then in xml layout add:
<[path_to_class].flowlayout xmlns:flowlayout="http://schemas.android.com/apk/res/za.co.lawdata.searchworks" android:id="@+id/flow_layout" android:layout_width="fill_parent" android:layout_height="wrap_content"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" flowlayout:layout_verticalspacing="50dp"/> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" flowlayout:layout_newline="true" flowlayout:layout_horizontalspacing="50dp"/> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button"/> </[path_to_class].flowlayout>
and replace [path_to_class]
package path eg: com.example.appname
flowlayout:layout_verticalspacing="50dp"
set vertical space between item. default set in java class.
flowlayout:layout_horizontalspacing="50dp"
set horizontal space between item. default set in java class.
flowlayout:layout_newline="true"
put item on new line.
this edit git: https://github.com/apmem/android-flowlayout