Hi I have custom adapter which add ti gridview image
adapter code
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams((tools.getWidth(mContext) - 30) / 3, (tools.getWidth(mContext) - 30) / 3));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageBitmap(static_file[position]);
imageView.setTag(p[position]);
return imageView;
}
In minimap I have image
Help me please how I can programmatically add 2-rd image in 1-st
There is no way simply because your "transparent background" isn't transparent at all. It's just a part of the image, you should remove it as if it was white color, i've made it for, download this image and use it
Use a custom layout,
<RelativeLayout>
<ImageView> containing the image you want
<ImageView> the 2nd image having centerInParent=true
</RelativeLayout>
Refer to this
Custom Adapter for List View
Related
I have a GridView with 4 columns and n-rows. Each cell is simply an ImageView. My custom adapter's getView() code looks like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
} else {
imageView = (ImageView) convertView;
}
imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(1, 1, 1, 1);
//imageView.setAdjustViewBounds(true);
if(imageLoader != null) {
String url = randomImage();
imageView.setContentDescription(url);
imageLoader.DisplayImage(url, imageView);
}
return imageView;
}
The imageLoader will pick a random URL and in the background, go fetch it and update the ImageView accordingly. That works fine and dandy. The problem is that the resulting images seem to be using a scale type of FIT_START instead of FIT_XY. I'm explicitly setting this to FIT_XY and even set it again inside of the code that sets the imageView's drawable... still its not FIT_XY and i'm stumped. Ideas?
Edited to remove the call to setAdjustViewBounds().
So I just found the issue. I was using Romain Guy's code for creating a RoundedDrawable from a bitmap -- to get rounded corners, ala iOS, as shown here: curious-creature.org/2012/12/11/… ... when i skip the RoundedDrawable conversion, it works fine. Something in the RoundedDrawable code is what is throwing this whole thing off. I'll just need to find another method to round the corners of the imageview. I am using CENTER_CROP now thanks to #kcoppock ! :)
I'm using this example of gridview http://developer.android.com/resources/tutorials/views/hello-gridview.html just with less images and a rotate button in one corner. I want to click a image, then click rotate button to rotate that image, but I can't find how to do it.
Anyone have any idea?
Rotating the image can be done using setImageMatrix() within getView if you keep track of which images you want rotated. Something like this:
HashMap<Integer, Matrix> mImageTransforms = new HashMap<Integer,Matrix>();
Matrix mIdentityMatrix = new Matrix();
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
Matrix m = mImageTransforms.get(position);
if ( null == m ) {
m = mIdentityMatrix;
}
imageView.setImageMatrix(m);
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
void setImageRotation(int position, int degrees) {
Matrix m = mImageTransforms.remove(position);
if ( degrees != 0 ) {
if ( null == m ) m = new Matrix();
m.setRotate(degrees);
mImageTransforms.put(position, m);
}
notifyDataSetChanged();
}
One thing to note. In touch mode lists and grids do not have a selection per-se, however they can be checked. It's a bit tricky to get a grid view to show the checked state, that being said the Honeypad Tutorial shows how to use checked views within a list view which may be helpful.
I have created horizontal gallery view using this Tutorial
But when gallery is created it starts from half of the screen, below screen shot
I want that when gallery view starts it should start from left of screen not center.
Thanks
Better way to escape, mark the focus from second item.
Try to use in xml--
android:gravity="left"
in your getView function you can set the image that should be centered. So you can set the last image as below
imageView.setImageResource(mImageIds[position]);
Something I did
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
I want my TextView to appear below my ImageView in a RelativeLayout that is going into a GridView. I've tried:
public override View GetView(int position, View convertView, ViewGroup parent)
{
RelativeLayout rl = new RelativeLayout(context);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.FillParent);
ImageView imageView;
TextView tv;
imageView = new ImageView(context);
imageView.LayoutParameters = new GridView.LayoutParams(250, 250);
imageView.SetScaleType(ImageView.ScaleType.CenterCrop);
imageView.SetPadding(8, 8, 8, 8);
imageView.SetImageResource(thumbIds[position]);
imageView.Id = position;
lp.AddRule(LayoutRules.Below, position);
lp.AddRule(LayoutRules.CenterHorizontal);
tv = new TextView(context);
tv.Text = stringIds[position].ToString();
tv.SetTextSize(Android.Util.ComplexUnitType.Dip, 20);
tv.SetTextColor(Android.Graphics.Color.WhiteSmoke);
tv.LayoutParameters = lp;
rl.AddView(imageView);
rl.AddView(tv);
return rl;
}
But the TextView always shows up on top of the ImageView.
Check out this question.
When you call rl.AddView(tv), you should include the LayoutParams rl.AddView(tv, lp).
You have to use
imageView.setLayoutParams(lp) in order to assign the params to your view.
How do you setLayoutParams() for an ImageView?
Since my content is not dynamic, I worked around the issue by simply editing my image in Paint and placing text below the image. Then I made that the background for the ImageView.
I'm trying to build a gallery with text below the image but although I've followed every response founded in here I've yet to accomplish my objective.
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(mContext);
ll.setOrientation(LinearLayout.VERTICAL);
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(150, 150));
TextView tv = new TextView(ll.getContext());
tv.setTag(mText[position]);
tv.setText(mText[position]);
tv.setLayoutParams(new Gallery.LayoutParams(48, 48));
ll.addView(tv);
// The preferred Gallery item background
//i.setBackgroundResource(mGalleryItemBackground);
return ll;
//return i;
}
I don't know why (and maybe it's the dumbest thing) but my images don't appear:)
I believe you forgot to add the image view to the linear layout, simply add:
ll.addView(i);
Also you are not recycling the convertView which could cause problems unless you have very few images and little to no scrolling of the grid.
You should check if convertView is null and if it is not simply change the text and image of the existing convertView.
Here is a good example for a custom grid view:
http://vikaskanani.wordpress.com/2011/07/20/android-custom-image-gallery-with-checkbox-in-grid-to-select-multiple/