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.
Related
I want to achieve this screenshot like design with Horizontal scroll view.
I already done with the Horizontal scroll view with the help of Wheel view library
https://github.com/chemalarrea/Android-wheel
But I am not getting any idea about how to fill a color in this images without disturbing image square area using setBackground method for Imageview.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp">
<kankan.wheel.widget.WheelView
android:id="#+id/slotwheel_look_color"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Adapter
private class SlotMachineAdapter_Look extends AbstractWheelAdapter {
// Image size
final int IMAGE_WIDTH = 100;
final int IMAGE_HEIGHT = 100;
private LayoutInflater mInflater;
private LinearLayout mLn_parant;
private ImageView mImg_condition;
// Layout inflater
private Context context;
// Slot machine symbols
private final int items[] = new int[]{
R.drawable.image_1,
R.drawable.image_2,
R.drawable.image_3,
R.drawable.image_4,
R.drawable.image_5,
R.drawable.image_6,
R.drawable.image_7,
R.drawable.image_8,
R.drawable.image_9
};
// Cached images
private List<SoftReference<Bitmap>> images;
/**
* Constructor
*/
public SlotMachineAdapter_Look(Context context) {
this.context = context;
mInflater = LayoutInflater.from(context);
images = new ArrayList<SoftReference<Bitmap>>(items.length);
for (int id : items) {
images.add(new SoftReference<Bitmap>(loadImage(id)));
}
}
/**
* Loads image from resources
*/
private Bitmap loadImage(int id) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), id);
Bitmap scaled = Bitmap.createScaledBitmap(bitmap, IMAGE_WIDTH, IMAGE_HEIGHT, true);
bitmap.recycle();
return scaled;
}
#Override
public int getItemsCount() {
return items.length;
}
// Layout params for image view
LayoutParams params = new LayoutParams(IMAGE_WIDTH, IMAGE_HEIGHT);
#Override
public View getItem(int index, View cachedView, ViewGroup parent) {
ImageView img;
if (cachedView != null) {
img = (ImageView) cachedView;
} else {
img = new ImageView(context);
}
img.setLayoutParams(params);
SoftReference<Bitmap> bitmapRef = images.get(index);
Bitmap bitmap = bitmapRef.get();
if (bitmap == null) {
bitmap = loadImage(items[index]);
images.set(index, new SoftReference<Bitmap>(bitmap));
}
BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap);
img.setBackground(ob);
img.setImageBitmap(bitmap);
img.setColorFilter(Color.YELLOW);
return img;
// return view;
}
}
Please help me out for this issue.
Thanks in advance
Used the setColorFilter
Drawable myIcon = getResources().getDrawable( R.drawable.myicon);
ColorFilter filter = new LightingColorFilter( Color.BLUE, Color.BLUE );
myIcon.setColorFilter(filter);
Imageview.setImageDrawable(myIcon);
Or
Resources res = context.getResources();
final ImageView image = (ImageView) findViewById(R.id.image);
final int newColor = res.getColor(R.color.new_color);
image.setColorFilter(newColor, Mode.SRC_ATOP);
I am trying to set a 3:2 ratio for a gridview (IE 300 pixels by 200 per cell). Here is my 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
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.image1, R.drawable.image2, R.drawable.image3,
};
}
I've tried adjusting the imageView.setLayoutParams(new GridView.LayoutParams(200, 200)); width parameter to 300 but it results in overlapping. Here is my XML File:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
Weird overlapping:
Hi there you can try like this from your Activity :
ViewGroup.LayoutParams layoutParams = gridview.getLayoutParams();
layoutParams.height = 200;
layoutParams.width = 300;
gridview.setLayoutParams(layoutParams);
or
int final width = 300;
int final height = 200;
ViewGroup.LayoutParams lauoutParams = new ViewGroup.LayoutParams(width,height);
gridView.setLayoutParams(lp);
This might help.
Discovered this library: AsymetricGridView
I am newer in developing android app , can anybody tell me how is it possible to add an onClick listener to a drawable image? This is the code for putting the drawable images in the swapping mode but I want the user be able to go to another page by clicking on each of the drawable images. how can I add the onClick method to my code?
public class ViewAdapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] {
R.drawable.image_1,
R.drawable.image_2,
R.drawable.image_3,
R.drawable.image_4
};
ViewAdapter(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]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
Add an onClickListener to the ImageView displaying the drawable:
#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]);
((ViewPager) container).addView(imageView, 0);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// do something
}
});
return imageView;
}
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;
}
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;
}