I have been writing and rewriting this adapter many times in different ways, but only thing I get is a blank screen when I launch the app.
I want to load images into a gridview using picasso and a string of urls as base data, but I'm not really going forward with it.
https://github.com/Valakias/PortFolioMovieApp
The problem is that the imageView width is 0 (check it using: Poster.getWidth();), that's why you're not seeing the images, the Custom Adapter is fine.
You can fix this, using setLayoutParams to set a width/height to the imageView as the example below:
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.gridview_item, null);
//Get screen size, and divide it by the number of columns of your grid view.
int width = getContext().getResources().getDisplayMetrics().widthPixels / 2;
((ImageView) convertView.findViewById(R.id.gridview_item_image)).setLayoutParams(new GridView.LayoutParams(width, width));
}
ImageView Poster = (ImageView) convertView.findViewById(R.id.gridview_item_image);
Picasso.with(getContext())
.load(getItem(position))
.fit()
.centerCrop()
.into(Poster);
Or a different approach using Picasso.resize
//Get screen size, and divide it by the number of columns of your grid view.
int width = getContext().getResources().getDisplayMetrics().widthPixels / 2;
ImageView Poster = (ImageView) convertView.findViewById(R.id.gridview_item_image);
Picasso.with(getContext())
.load(getItem(position))
.resize(width, width)
.centerCrop()
.into(Poster);
Instead of .centerCrop() you can use .centerInside()
Using .centerCrop()
Using '.centerInside()' (This will respect image aspect ratio).
Related
I have a listivew that display a bunch of images. am using Universal Image Loader to load this images from files to imageviews.
This images have different dimensions and i want all of them to have same width but different height in respect to each image aspect ratio.
To achieve this, i have tried setting the following to my imageview
<ImageView
android:layout_width = "400dp"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"/>
The issue with this method is that there is a lot of flickering when one scrolls the listview since imageview height is not known in advance and images have to be scaled first using my width to calculate each image height in respect to it's aspect ratio.
How can i calculate each image height in advance instead of letting imageview handle it?
if i have an image which is 400 X 700, and i want the imageview to be 300px wide, how can i calculate imageview's height using my image dimension and maintain image aspect ratio? this can help avoid flickering wnen one scroll the listview.
The reason for this flicker is that, in listview list items are reused. When re-used, the imageviews in the list item retains the old image reference which is displayed first. Later on once new image is downloaded, it starts to show. this causes the flickering behavior.
To avoid this flickering issue, always clear the old image reference from the imageview when it is getting reused.
In your case, add holder.image.setImageBitmap(null); after holder = (ViewHolder) convertView.getTag();
So, your getView() method will look like:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
...
if (convertView == null) {
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(viewResourceId, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
holder.image.setImageBitmap(null)
}
...
return convertView;
}
After hours of research, i was able to know the method that i can use to calculate new imageview height while maintaining image aspect ratio.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
//Returns null, sizes are in the options variable
BitmapFactory.decodeFile("/sdcard/image.png", options);
int width = options.outWidth;
int height = options.outHeight;
//calculating image aspect ratio
float ratio =(float) height/(float) width;
//calculating my image height since i want it to be 360px wide
int newHeight = Math.round(ratio*360);
//setting the new dimentions
imageview.getLayoutParams().width = 360;
imageview.getLayoutParams().height = newHeight;
//i'm using universal image loader to display image
imaheview.post(new Runnable(){
ImageLoader.getInstance().displayImage(imageuri,imageview,displayoptions);
});
You can do something like this :
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
//Returns null, sizes are in the options variable
BitmapFactory.decodeFile("/sdcard/image.png", options);
int width = options.outWidth;
int height = options.outHeight;
//If you want, the MIME type will also be decoded (if possible)
String type = options.outMimeType;
How I solved it was by creating a Bitmap[] array variable to store images, then in adapter's getView(), I used position to check if image in Bitmap[] array is null or has value. If it is has value, then I use the value instead of calling the new DownloadImageTask() construct again.
For example:
YourCustomArrayAdapter.java
public class MyCustomArrayAdapter extends ArrayAdapter {
private static Bitmap[] myListViewImageViewsArray = new Bitmap[listViewItemsArray.length];
private String[] myListViewImageURLsArray = new String[listViewItemsArray.length]{
"image_url_1",
"image_url_2",
...
...
};
#Override
public View getView(int position, View view, ViewGroup parent){
CustomViewHolder vHolder;
if(view == null){
view = inflater.inflate(R.layout.movies_coming_soon_content_template, null, true);
vHolder = new CustomViewHolder();
vHolder.imageView = (AppCompatImageView) view.findViewById(R.id.my_cutom_image);
vHolder.imageUrl = "";
view.setTag(vHolder);
}
else{
vHolder = (CustomViewHolder)view.getTag();
// -- Set imageview src to null or some predefined placeholder (this is not really necessary but it might help just to flush any conflicting data hanging around)
vHolder.imageView.setImageResource(null);
}
// ...
// -- THIS IS THE MAIN PART THAT STOPPED THE FLICKERING FOR ME
if(myListViewImageViewsArray[position] != null){
vHolder.imageView.setImageBitmap(myListViewImageViewsArray[position]);
}else{
new DownloadImageTask(position, vHolder.imageView).execute(vHolder.imageUrl);
}
// -- END OF THE FLICKERING CONTROL
}
}
Then, in your image downloader construct, after downloading the image, make an insertion into the Bitmap[] image array for that position. For example:
YourImageDownloaderClass.java
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
AppCompatImageView imageView;
int position;
public DownloadImageTask(int position, AppCompatImageView imageView){
this.imageView = imageView;
this.position = position;
}
#Override
protected Bitmap doInBackground(String...urls) {
String urlOfImage = urls[0];
Bitmap logo = null;
try{
logo = BitmapFactory.decodeStream((InputStream) new URL(urlOfImage).getContent());
}catch(Exception e){
e.printStackTrace();
}
return logo;
}
#Override
protected void onPostExecute(Bitmap result){
if(result != null) {
YourCustomArrayAdapter.myListViewImageViewsArray [position] = result;
imageView.setImageBitmap(result);
}else{
YourCustomArrayAdapter.myListViewImageViewsArray [position] = null;
imageView.setImageResource(null);
}
}
}
my suggestion is to use grid view to avoid flickering of images it will load at first time if it is same url , it will load from cache
Glide.with(mContext)
.load(item.getImageUrl())
.into(holder.mIVGridPic);
I have this issue with loading my Bitmap image. I created a custom ImageAdapter and ImageItem for my GridLayout view. The application is supposed to take a picture, save it to the external storage, and retrieve the image from the external storage, showing it as a small thumbnail in a grid. When that image is clicked from the grid, a new Activity is started, showing the full-sized image. Everything has worked fine so far (The images get saved, and I can actually view them on the file system, the supplied path is correct), but the problem I am facing is that when I load the image, it shows up blank in the GridView, and when it is clicked upon, the new Activity also shows a blank image. I would greatly appreciate any help I can get! Here's the code:
The getView for the custom Adapter:
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
final ImageItem imageItem = (ImageItem) getItem(position);
LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout itemLayout = (LinearLayout) li.inflate(R.layout.image_item, parent, false);
final TextView nameView = (TextView) itemLayout.findViewById(R.id.nameView);
nameView.setText(imageItem.getName());
final ImageView imageView = (ImageView) itemLayout.findViewById(R.id.imageView);
String url = imageItem.getUrl();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
//Bitmap bitmap = BitmapFactory.decodeFile(url);
Drawable d = Drawable.createFromPath(url);
imageView.setImageDrawable(d);
imageView.setLayoutParams(new LinearLayout.LayoutParams(WIDTH, HEIGHT));
imageView.setPadding(PADDING, PADDING, PADDING, PADDING);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
return itemLayout;
} else {
return convertView;
}
}
I created an imageview image, but I want to scale it to be no greater than a fifth of the width and a sixth of the height of the android device's screen size.
Here I get the width and height of the screen:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
here I create my imageview:
ImageView imageView = new ImageView(this);
imageView.setImageResource(icons[0]);
and here I try to scale my imageview:
imageView.getDrawable().setBounds(0, 0, width/5, height/6);
This last part is the stickler. After typing this in I get no errors and my program runs normally - but the image is not scaled. Basically that last line of code seems to have no effect and I have no idea why, any pointers?
Rest of my code:
imageView.setAdjustViewBounds(true);
int mh = imageView.getDrawable().getIntrinsicHeight();
int mw = imageView.getDrawable().getIntrinsicWidth();
imageView.setX(width/2 - Math.round(width/5));
imageView.setY(height/2 - Math.round(height/6) - mActionBarSize);
relativeLayout.addView(imageView);
setContentView(relativeLayout);
do this :
imageView.getLayoutParams().height = (int) Math.round(height/6);
imageView.getLayoutParams().width = (int) Math.round(width/5);
imageView.requestLayout(); // If you're setting the height/width after the layout has already been 'laid out'
see this link to learn more about requestLayout():
Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree.
But ususally the view is calling it automatically, you don't have to care about that... (So for me I use it to force the view to do what I want to do)
Use this
ImageView imgView = (ImageView) findViewById(R.id.pageImage);
imgView.getLayoutParams().height = 100;
imgView.getLayoutParams().width = 100;
The problem with properly handling multiple screen sizes on Android has been talked all over thousands of times. However I couldn't find a solution to m problem. In a nutshell I need to align my custom progress bar over an imageView. I've got 3 set of drawables for the imageView - ldpi(240x400), mdpi(320x480), hdpi(480x800). I align my custom view in Java with the following code:
//get screen density
float density = getBaseContext().getResources().getDisplayMetrics().density;
//set the progress bar position according to screen density
if ( density == 1.0f)
{
ImageView micImage = ((ImageView) findViewById(R.id.imageViewClk));
Drawable drawing = micImage.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
// Get current dimensions
int width = bitmap.getWidth();
int height = bitmap.getHeight();
LayoutParams params = new LayoutParams((int)(height/13.94), (int)(height/13.94));
params.setMargins((int)(width/2.30), 0, 0, (int)(height/2.75));
params.addRule(RelativeLayout.ALIGN_LEFT,R.id.imageViewClk);
params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.imageViewClk);
myCustomTwistedProgressBar.setLayoutParams(params);
}else if ( density == 1.5f ){
ImageView micImage = ((ImageView) findViewById(R.id.imageViewClk));
Drawable drawing = micImage.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
int width = bitmap.getWidth();
int height = bitmap.getHeight();
LayoutParams params = new LayoutParams((int)Math.round(height/14.13), (int)Math.round(height/14.13));
params.setMargins((int)Math.round( width/2.27), 0, 0, (int)Math.round(height/2.91));
params.addRule(RelativeLayout.ALIGN_LEFT,R.id.imageViewClk);
params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.imageViewClk);
myCustomTwistedProgressBar.setLayoutParams(params);
}else if ( density == 0.75f ){
ImageView micImage = ((ImageView) findViewById(R.id.imageViewClk));
Drawable drawing = micImage.getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
// Get current dimensions
int width = bitmap.getWidth();
int height = bitmap.getHeight();
LayoutParams params = new LayoutParams((int)(height/14.88), (int)(height/14.88));
params.setMargins((int)(width/2.27), 0, 0, (int)(height/2.69));
params.addRule(RelativeLayout.ALIGN_LEFT,R.id.imageViewClk);
params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.imageViewClk);
myCustomTwistedProgressBar.setLayoutParams(params);
}
Everything worked fined on different screen sizes however when I tried to check on 480x854 resolution the vertical alignment of the custom view was incorrect. Checked with 480x800 on the same screen size and it again works. I than went for a big jump and checked in GalaxyTab and the horizontal and vertical alignments were wrong. Now my first though was that the bitmap width and height were the one of the image not the actual resized imageview. So I spent a lot of time on trying to get the real size of the imageview and even went for viewTreeObserver but the results were all the same - the correct, unchanged (unscaled?) bitmap size. So being positive that the problem is not here I couldn't get through further. Does anyone have an idea why the alignment is not working correctly?
PS: as for the image view in layout xml file I have 2 configurations for long and notlong but this image has the same description in both:
<ImageView
android:src="#drawable/cloking"
android:id="#+id/imageViewClk"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#+id/imageViewProcess"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:layout_marginTop="60dp"
android:scaleType="fitXY">
</ImageView>
Android will scale the image but it will maintain aspect ratio for the image. You can't control aspect ratio with layout settings (as far as I know). I would solve that problem by choosing few screen ratios that I want to support and making few more resources (images that would have aspect ratios that you support). Code would look like this:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
ImageView image = (ImageView) findViewById(R.id.imageViewClk);
if(width/height == aspectRatio1)
{
image.setImageResource(R.id.imageAspect1);
} else if( width/height == aspectRatio2...
this is my code to put some image doodads in my GridView
public View getView(int position, View convertView, ViewGroup parent) {
ImageView v;
if(convertView == null) {
v = new ImageView(c);
v.setLayoutParams(new GridView.LayoutParams(90,90));
v.setScaleType(ImageView.ScaleType.CENTER_CROP);
v.setPadding(2, 2, 2, 2);
} else {
v = (ImageView) convertView;
}
v.setImageDrawable(c.getResources().getDrawable(ops[position]));
return v;
}
but for smaller machines the layoutParams are too big, does anyone know how i can do like a (90dp, 90dp) for the width and height on the java side?
Create a dimension in XML :
<dimen name="image_size">90dp</dimen>
Then get it from your code :
int size = (int) getResources().getDimension(R.dimen.image_size);
v.setLayoutParams(new GridView.LayoutParams(size, size));
You can also set different dimensions depending on the screen sizes, by creating different values folders.
For example, put a large dimension in a folder named values-w600dp (which will be used if the width of the screen is greater or equal to 600dp) and a smaller one in the simple values folder (you will have two dimens.xml files).
You will find more details in the documentation.