Android: Bottom Layout not Showing in Gallery - android

I'm trying to surround the top and bottom of my ImageView in a Gallery with a layout containing a tiled background. The top part seems to be working, but the bottom layout doesn't show. Any ideas why? Here's my getView():
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout container = new RelativeLayout(galleryContext);
RelativeLayout topReel = new RelativeLayout(galleryContext);
RelativeLayout bottomReel = new RelativeLayout(galleryContext);
topReel.setBackgroundResource(R.drawable.film_top);
bottomReel.setBackgroundResource(R.drawable.film_bottom);
ImageView imageView = null;
if (convertView != null) { //we can reuse the view!
imageView = (ImageView) convertView;
} else {
imageView = new ImageView(galleryContext); //boo we have to make a new view
}
//Get a scaled Bitmap so that it doesn't use up all our memory
Bitmap setBitmap = loadScaledBitmap(imageList[position].getAbsolutePath(), imageSizeInPixels);
//set up our ImageView inside the gallery to display our Bitmap...
imageView.setImageBitmap(setBitmap);
imageView.setLayoutParams(new Gallery.LayoutParams(imageSizeInPixels, imageSizeInPixels));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(galleryBackground);
RelativeLayout.LayoutParams topParams = new RelativeLayout.LayoutParams(imageSizeInPixels, 20);
topParams.addRule(RelativeLayout.ABOVE, imageView.getId());
topParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
RelativeLayout.LayoutParams bottomParams = new RelativeLayout.LayoutParams(imageSizeInPixels, 20);
bottomParams.addRule(RelativeLayout.BELOW, imageView.getId());
bottomParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(imageSizeInPixels, imageSizeInPixels);
imageParams.addRule(RelativeLayout.BELOW, topReel.getId());
imageParams.addRule(RelativeLayout.ABOVE, bottomReel.getId());
imageParams.addRule(RelativeLayout.CENTER_VERTICAL);
imageParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
container.addView(topReel, topParams);
container.addView(bottomReel, bottomParams);
container.addView(imageView, imageParams);
return container;
}

This solves the problem, hopefully someone can use this:
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout container = new RelativeLayout(galleryContext);
ImageView imageView = null;
if (convertView != null) { //we can reuse the view!
imageView = (ImageView) convertView;
} else {
imageView = new ImageView(galleryContext); //boo we have to make a new view
}
//Get a scaled Bitmap so that it doesn't use up all our memory
Bitmap setBitmap = loadScaledBitmap(imageList[position].getAbsolutePath(), ((int)(imageSizeInPixels * .75)));
//set up our ImageView inside the gallery to display our Bitmap...
imageView.setImageBitmap(setBitmap);
imageView.setLayoutParams(new Gallery.LayoutParams(imageSizeInPixels, imageSizeInPixels));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundColor(Color.BLACK);
RelativeLayout borderImg = new RelativeLayout(galleryContext);
borderImg.setPadding(10, 5,10, 5);
borderImg.setBackgroundColor(0xff000000);
borderImg.addView(imageView);
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(imageSizeInPixels, imageSizeInPixels);
imageParams.addRule(RelativeLayout.CENTER_VERTICAL);
imageParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
container.setBackgroundResource(R.drawable.repeat_reel);
container.setLayoutParams(new Gallery.LayoutParams(imageSizeInPixels, imageSizeInPixels+40));
container.addView(borderImg, imageParams);
return container;
}

Related

get image height from drawable at ArrayAdapter class

i try to make my image view position dynamic base on image height. but i stuck to get the image height from drawable.. help me to solve it thanks..
this is my code :
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_wall, null, true);
TextView txtProfileUser = (TextView) rowView.findViewById(R.id.txtWallProfile);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txtWallContentTitle);
TextView txtContent = (TextView) rowView.findViewById(R.id.txtWallContent);
ImageView imgWallProfile = (ImageView) rowView.findViewById(R.id.ivWallProfile);
ImageView imgWallContent = (ImageView) rowView.findViewById(R.id.ivWallContent);
txtTitle.setText(web2[position]);
txtContent.setText(web3[position]);
txtProfileUser.setText(web[position]);
imgWallProfile.setImageResource(imageId[position]);
imgWallContent.setImageResource(imageId2[position]);
GetSize gz = new GetSize();
Integer h = gz.getImageSize(imageId[position]);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(20,h, 0, 0);
imgWallProfile.setLayoutParams(lp);
return rowView;
}
and this my getImageSize method :
public Integer getImageSize(Integer d){
Drawable bd = getResources().getDrawable(d);
int height=bd.getIntrinsicHeight();
return height;}
If I understand correctly you want to get the width and height of your image view instead of the bitmaps width and height. Layout parameters allows you to access the imageView and view and modify it's parameters.
public int getImageSize(View myView) {
RelativeLayout.LayoutParams myParams = (RelativeLayout.LayoutParams) myView.getLayoutParams();
return myParams.height;
}

how to set size of imageview in viewpager programmatically?

i want to resizing imageview, but my code doesn't works. here's the code:
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {
R.drawable.kuhpps,
R.drawable.kuhpps,
R.drawable.kuhpps
};
part of error code:
#Override
public Object instantiateItem(ViewGroup container, int position){
Context context = MainActivity.this;
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.getLayoutParams().height = 30;
imageView.getLayoutParams().width = 30;
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
You need to set the Layout Params with the height and wight . You are updating the LayoutParams instance from getLayoutParams , but that won't change the view.
Do like this to change the ImageView Layout size.
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(300,300);
imageView.setLayoutParams(layoutParams);
In the viewpager's adapter:
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = ActivityGallery.this;
ImageView imageView = new ImageView(context);
imageView.setLayoutParams(new ViewGroup.LayoutParams(mScreenWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
Drawable drawable = Drawable.createFromPath(Environment.getExternalStorageDirectory() + "/Android/data/" + getPackageName() +
"/"+....+".jpg");
if(drawable != null)
imageView.setImageDrawable(drawable);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
mScreenWidth is the current screen width fetched earlier.

Android ViewHolder pattern with programmatically added views

I'm creating a FrameLayout and then I add two Views (an ImageView and a TextView ).
My code on getView() :
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
TextView textView1;
int new_width = wid/2;
int textview_id=0;
FrameLayout frame_layout = new FrameLayout(mContext); // Instantiate the parent
frame_layout.setLayoutParams(new GridView.LayoutParams(android.widget.FrameLayout.LayoutParams.MATCH_PARENT,
android.widget.FrameLayout.LayoutParams.MATCH_PARENT));
textView1 = new TextView(mContext); textView1.setId(position);
textView1.setText("Set Wallpaper");
textView1.setTextColor(mContext.getResources().getColor(R.color.set_wallpaper_colors));
textView1.setVisibility(View.INVISIBLE);
textView1.setTypeface(textView1.getTypeface(), Typeface.BOLD);
FrameLayout.LayoutParams textview_params = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
textview_params.gravity = Gravity.CENTER;
FrameLayout.LayoutParams imageview_params = new FrameLayout.LayoutParams(
new_width -5, new_width -5 );
imageView = new ImageView(mContext);
//imageView.setLayoutParams(new GridView.LayoutParams(new_width - 5, new_width -5));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
imageView.setImageResource(mThumbIds[position]);
frame_layout.addView(imageView, imageview_params);
frame_layout.addView(textView1, textview_params);
return frame_layout;
}
How should I change it to use the ViewHolder class. There isn't a layout to inflate.
With your code it should be something like this
static class ViewHolder {
TextView textView1;
ImageView imageView1;
}
#Override
public View getView(final int row, View convertView, final ViewGroup parent) {
ViewHolder holder = null;
FrameLayout frame_layout = (FrameLayout) convertView;
if (frame_layout == null) {
holder = new ViewHolder();
holder.textView1 = new TextView(mContext);
holder.imageView1 = new ImageView(mContext);
frame_layout.setTag(holder);
}
else {
holder = (ViewHolder) frame_layout.getTag();
}
holder.textView1.setText("Set Wallpaper");
holder.textView1.setTextColor(mContext.getResources().getColor(R.color.set_wallpaper_colors));
holder.textView1.setVisibility(View.INVISIBLE);
holder.textView1.setTypeface(holder.textView1.getTypeface(), Typeface.BOLD);
FrameLayout.LayoutParams textview_params = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
textview_params.gravity = Gravity.CENTER;
FrameLayout.LayoutParams imageview_params = new FrameLayout.LayoutParams(
new_width -5, new_width -5 );
holder.imageView1.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.imageView1.setPadding(0, 0, 0, 0);
holder.imageView1.setImageResource(mThumbIds[position]);
frame_layout.addView(holder.imageView1, imageview_params);
frame_layout.addView(holder.textView1, textview_params);
return frame_layout;
}

Gridview does not show all the picture, only a small part

I have the following GridView:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setScaleType(ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
if (mTemplates.get(position).getResource() != 0) {
LogService.log("", "zzzzzzzzzzzzzzzz======loaded from resource" + mTemplates.get(position).getResource() + "???????????" + mTemplates.size());
imageView.setImageResource(mTemplates.get(position).getResource());
} else {
imageView.setImageDrawable(mTemplates.get(position).getDrawableThumbnail());
// imageView.setImageResource(mTemplates.get(0).getResource());
LogService.log("", "zzzzzzzzzzzzzzzz======loaded from drawable");
LogService.log("", "=========SIZE: " + mTemplates.size());
}
return imageView;
}
Now I have some elements that do not have resources in the Drawables, but instead in a folder on the sdcard, so I create a drawable from the source of this pictures, and try to load that drawable on the gridview:
imageView.setImageDrawable(mTemplates.get(position).getDrawableThumbnail());
But its like the gridview does not recognise these, and if its on the same collumn with other pictures it is shown, but if its on another collumn (lower), it will only show 2-3mm of the picture (also doesn't recognise its position like its not therE).
If I hardcode and use the first picture from Resources like this:
imageView.setImageResource(mTemplates.get(0).getResource());
It all works, what could be the issue?
Because the actually size of the pic, is the small size, but when loading an imageView from Resources it strectches it to its parents size.
Resolved this issue by calculating the height needed, and then load all the pictures with that height:
public AdapterGridView(Context c) {
context = c;
DisplayMetrics metrics = new DisplayMetrics();
((Activity) c).getWindowManager().getDefaultDisplay().getMetrics(metrics);
if (Constants.IS_TABLET) {
height = metrics.widthPixels / 9 - (int) Util.pxFromDp(2, c);
} else {
height = metrics.widthPixels / 4 - (int) Util.pxFromDp(2, c);
}
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
} else {
imageView = (ImageView) convertView;
}
imageView.setScaleType(ScaleType.CENTER_CROP);
imageView.setAdjustViewBounds(true);
if (mTemplates.get(position).getResource() != 0) {
imageView.setImageResource(mTemplates.get(position).getResource());
} else {
imageView.setImageDrawable(mTemplates.get(position).getDrawableThumbnail());
}
imageView.setLayoutParams(new LayoutParams(height, height));
return imageView;
}

Grayscale image background

Hello is there a way to grayscale image background? I achieved grayscaling image itself, but not the background :/.
My GridView getView:
// 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(65, 65));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(13, 13, 13, 13);
} else {
imageView = (ImageView) convertView;
}
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
imageView.setImageResource(MainActivity.mThumbIds.get(position));
imageView.setBackgroundResource(R.drawable.check);
imageView.setColorFilter(filter);
return imageView;
}
Not directly, but you can render the resource to a bitmap, apply the filter to the bitmap, and set the background to the filtered bitmap using a BitmapDrawable and imageView.setBackground(Drawable).

Categories

Resources