ImageView will not resize properly - java

I'm attempting to get an imageview (classical_up_btn) to fill the width of the screen correctly however it appears to be cut off by the next image it is supposed to load when swiped
Any suggestions as to how this can be resolved?
Thanks!
SCREENSHOT:
JAVA:
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] { R.drawable.classical_up_btn,
R.drawable.country_up_btn, R.drawable.dance_up_btn,
R.drawable.hiphop_up_btn };
public int getCount() {
return mImages.length;
}
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
public Object instantiateItem(ViewGroup container, int position) {
Context context = Home.this;
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}

imageView.setScaleType(ImageView.ScaleType.CENTER);
should be:
imageView.setScaleType(ImageView.ScaleType.FIT_XY);

Related

int array of Images implementing Picasso

This is my code
public class ImageAdapter extends BaseAdapter {
ImageView imageView;
private Context context;
public int Images[] = {Integer.parseInt(("http://www.fashionlady.in/wp-content/uploads/2016/03/creative-punjabi-mehndi-design-2016.jpg")),
Integer.parseInt("https://lumiere-a.akamaihd.net/v1/images/uk_toystory_chi_woody_n_5b5a006f.png?region=0,0,300,300"),
Integer.parseInt("https://lumiere-a.akamaihd.net/v1/images/open-uri20150422-20810-10n7ovy_9b42e613.jpeg"),
Integer.parseInt("http://www.wetpaint.com/wp-content/uploads/2015/11/toy-story-20th-anniversary.jpg")};
public ImageAdapter (Context c){
context= c;
}
#Override
public int getCount() {
return Images.length;
}
#Override
public Object getItem(int position) {
return Images[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageview = new ImageView(context);
imageview.setImageResource(Images[position]);
imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageview.setLayoutParams(new GridView.LayoutParams(240,240));
Picasso.with(context).load(Images[position]).into(imageview);
return imageview;
}
}
I am trying to implement Picasso in my project the problem i am facing is
I have an int array of Images .and i am getting error java.lang.NumberFormatException ,i guess its because there is no pars Int in array of Images.And if i make it String i.e
public String Images[] = {("URL"),("URL"),("URL"),("URL"),("URL")};
But setimageResourcewants an int value please help me i am stuck with this .Any help would really be appreciated .
Thanks.
Syntax from the picasso site:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Use a String[] instead of a Integer[] would be the correct solution
public String[] Images = { "http://www.fashionlady.in/wp-content/uploads/2016/03/creative-punjabi-mehndi-design-2016.jpg",
"https://lumiere-a.akamaihd.net/v1/images/uk_toystory_chi_woody_n_5b5a006f.png?region=0,0,300,300",
"https://lumiere-a.akamaihd.net/v1/images/open-uri20150422-20810-10n7ovy_9b42e613.jpeg",
"http://www.wetpaint.com/wp-content/uploads/2015/11/toy-story-20th-anniversary.jpg" };
You don't need imageview.setImageResource(Images[position]);when you load the image with Picasso, so just remove that line

parent.getMeasuredWidth() and parent.getMeasuredHeight() return 0 in ImageAdapter for GridView

I have 6 drawable images. I put this images in a GridView in 2x3 if it's portraite and 3x2 if it's landscape, but i have a problem: if i try to put measures based on parent.getMeasuredWidth() and parent.getMeasuredHeight();
first drawable have always 0 as width and hight;
From LOG:
w: 0 h:0
Code ImageAdapter:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {mContext = c;}
public int getCount() {return mThumbIds.length;}
public Object getItem(int position) {return null;}
public long getItemId(int position) {return 0;}
public View getView(int position, View convertView, final ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
int width = parent.getMeasuredWidth();
int height = parent.getMeasuredHeight();
int orientation = parent.getResources().getConfiguration().orientation;
int vh,vo;
if(orientation == 1){
vh = width/2;
vo = height/3;
} else {
vh = (width/3);
vo = (height/2);
}
Log.e(TAG,"w: "+vh+" h:"+vo);
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(vh, vo));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.gradient_bg_t1,
R.drawable.gradient_bg_t2,
R.drawable.gradient_bg_t4,
R.drawable.gradient_bg_t5,
R.drawable.gradient_bg_t6,
R.drawable.gradient_bg_t3
};
}
Code MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
}
});
}
Thank you.
it returns zero because the parent view isnt drawn yet. you can post your code to the parent view so it runs after parent view was drawn.
use below code:
parent.post(new Runnable() {
#Override
public void run() {
int w = parent.getMeasuredWidth();
int h = parent.getMeasuredHeight();
...
}
});
I already tried the code of #Amir Ziarati in ImageAdapter (found it in StackOverflow), but i can't get the values out of scope.
After i drink a little coffee i changed the code a litle:
I created in ImageAdapter 2 new int variable w,h for width and height:
private Context mContext;
private int w,h;
public ImageAdapter(Context c,int width,int height) {
mContext = c;
w = width;
h = height;
}
and in MainActivity:
gridview = (GridView) findViewById(R.id.gridview);
gridview.post(new Runnable() {
#Override
public void run() {
int w = gridview.getMeasuredWidth();
int h = gridview.getMeasuredHeight();
gridview.setAdapter(new ImageAdapter(MainActivity.this,w,h));
}
});
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
}
});
And it's working.

android - viewpager OutOfMemoryError

I have a problem. I want to get image URL from JSON and setting to viewpager. I tried it with drawable folder, but large images are causing problems. If the image would be small, then I would not have a problem. How can I get large images? I benefited from this article : enter link description here
GalleryActivity
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
ViewPager viewPager = (ViewPager)findViewById(R.id.viewpa);
GalleryAdapter ga = new GalleryAdapter(this);
viewPager.setAdapter(ga);
}
Gallery Adapter (extending PagerAdapter)
Context context;
private int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three
};
GalleryAdapter(Context context){
this.context=context;
}
#Override
public int getCount() {
return GalImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);// error on this line when large image
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
And logcat outputs
java.lang.OutOfMemoryError at android.graphics.BitmapFactory
...

how to setup onClickListeners for each slide in a viewpager?

The ImageAdapter Class
public class ImageAdapter extends PagerAdapter implements AdapterView.OnItemClickListener {
Context context;
private int[] GalImages = new int[] {//array storing all images used in the 'slideshow' in the main menu
R.drawable.savedpageex,
R.drawable.ic_cdi_icon,
R.drawable.savedpageex,
R.drawable.ic_cdi_icon
};
ImageAdapter(Context context){
this.context=context;
}
#Override
public int getCount() {
return GalImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
// int padding = context.getResources().getDimensionPixelSize(R.dimen.abc_panel_menu_list_width);
imageView.setPadding(1, 1, 1, 1);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final int clickedImage = GalImages[position];
}
}
In the MainActivity
ViewPager viewPager = (ViewPager)findViewById(R.id.viewpager);
ImageAdapter adapter = new ImageAdapter(this);
wrappedAdapter = new InfinitePagerAdapter(adapter);
viewPager.setAdapter(wrappedAdapter);
viewPager.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "clicked", Toast.LENGTH_LONG).show();
}
});
Right now, the pictures show up and they loop, I do need help getting the pictures to 'slide' at set time intervals, but the main issue is with setting up onClickListeners for each 'page'. Any help/advice?
You could add a switch statement inside of the onClickListener like this
switch(viewPager.getCurrentItem()){
case 0:
//stuff for first page
break;
case 1:
//stuff for second page
break;
}
...... etc.
or add a state to the viewpager to describe which "page" it is showing
and using that information do different things in the onClickListener

How to swap images in a GridView (Android)

I'm working on a slider puzzle Android app, so I'm using a GridView to display 16 images representing tiles. One of the tiles will consist of a plain white image (representing the background). The idea is that if the user clicks on any tile neighboring the white tile, the selected tile and the white tile will switch positions.
I'm trying to implement this by setting the white tile to the tile selected by the user, and vice versa. I saved the position of the white tile (it starts off at position 15) in the variable masterTilePos, and using the ImageAdapter Integer array referring to the images in my R.drawable file, set the image at masterValPos to the image at the selected index, and the selected image to the white tile. However, when I run the program, the tiles only successfully switch the first time: after that, it doesn't work properly and the order of the tiles within the GridView is ruined. I think it may because the array is simply referring to the actual image objects, but I'm not sure how to what to do instead; it has already been three days since I ran into this problem. Here is my code:
//GRID VIEW
public class MainActivity extends ActionBarActivity {
int masterTilePos = 15;
GridView gridview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
//These methods check if a neighboring tile is white, if there is one, swap() is called
if(checkUp(position) || checkDown(position) || checkLeft(position) || checkRight(position)) {
swap(position);
}
}
});
}
public void swap(int pos) {
ImageAdapter updater = new ImageAdapter(this);
//This is where I try to switch the images, and seems to be the source of the problem
int val = updater.mThumbIds[masterTilePos];
updater.mThumbIds[masterTilePos] = updater.mThumbIds[pos];
updater.mThumbIds[pos] = val;
updater.notifyDataSetChanged();
gridview.setAdapter(updater);
gridview.invalidateViews();
masterTilePos = pos;
}
}
//IMAGE ADAPTER
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// 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
DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
int width = metrics.widthPixels / 4;
int height = metrics.heightPixels / 4;
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(width, height));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(1, 1, 1, 1);
}
else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// the array containing references to the images
public Integer[] mThumbIds = {
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
R.drawable.pic6,
R.drawable.pic7,
R.drawable.pic8,
R.drawable.pic9,
R.drawable.pic10,
R.drawable.pic11,
R.drawable.pic12,
R.drawable.pic13,
R.drawable.pic14,
R.drawable.pic15,
R.drawable.background
};
}
Can anyone please help me solve this so I can successfully switch the images within the grid? Thanks.
In onCreate you are doing:
gridview.setAdapter(new ImageAdapter(this));
So, you created adapter, without assign it to any variable. This is wrong!
Then, every swap you create new Adapter:
ImageAdapter updater = new ImageAdapter(this);
And you are setting it as current adapter:
gridview.setAdapter(updater);
This is wrong as well.
You must do in such a way:
OnCreate -> create adapter, assign it to objects's variable (property)
Then you need to work only with that variable.
And then, if you have a problem, debug your logic in SWAP method.
//GRID VIEW
public class MainActivity extends ActionBarActivity {
int masterTilePos = 15;
GridView gridview;
ImageAdapter imageAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview = (GridView) findViewById(R.id.gridview);
imageAdapter= new ImageAdapter(this);
gridview.setAdapter(imageAdapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
//These methods check if a neighboring tile is white, if there is one, swap() is called
if(checkUp(position) || checkDown(position) || checkLeft(position) || checkRight(position)) {
swap(position);
}
}
});
}
public void swap(int pos) {
//This is where I try to switch the images, and seems to be the source of the problem
int val = imageAdaptor.mThumbIds[masterTilePos];
imageAdapter.mThumbIds[masterTilePos] = imageAdapter.mThumbIds[pos];
imageAdapter.mThumbIds[pos] = val;
imageAdapter.notifyDataSetChanged();
gridview.invalidateViews();
masterTilePos = pos;
}
}
//IMAGE ADAPTER
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// 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
DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
int width = metrics.widthPixels / 4;
int height = metrics.heightPixels / 4;
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(width, height));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(1, 1, 1, 1);
}
else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// the array containing references to the images
public Integer[] mThumbIds = {
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
R.drawable.pic6,
R.drawable.pic7,
R.drawable.pic8,
R.drawable.pic9,
R.drawable.pic10,
R.drawable.pic11,
R.drawable.pic12,
R.drawable.pic13,
R.drawable.pic14,
R.drawable.pic15,
R.drawable.background
};
}

Categories