ImageGallery and ImageGrid in android - android

I have created an image gallery in android , now whenever i will click on image on a gallery then that particular image will get added into Grid View.
my layout has image gallery on top and image grid in bottom (so i have used LinerLayout Vertical )
so can anyone tell me how to achieve this in android ?

Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new Gallerydapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
ImageView imageView = new ImageView(PhotoGallery.this);
imageView.setLayoutParams(new GridView.LayoutParams(45, 45));
imageView.setAdjustViewBounds(false);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
imageView.setImageResource(mImageIds[position]);
mGrid.addView(imageView);
}
I have used the above code to add images in grid view , is this the efficient code ? i mean is this code gonna generate running out of memory error

Related

Cannot resolve method decodeFile in android studio

I am newbie to native android development. I am working on android studio. I have 3 tabs. One is for map second is for camera to take pictures and 3rd is for pictures in which all taken were to be shown. I have implemented map and camera, for viewing image i am following this tutorial. Here in a class i added Bitmap image = Bitmap image = decodeFile(_filePaths.get(position), imageWidth, imageWidth);
But it's showing me error Cannot Resolve method decodeFile. I have searched for it and couldn't find any solution for it.
Below is my code
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(_activity);
} else {
imageView = (ImageView) convertView;
}
// get screen dimensions
Bitmap image = decodeFile(_filePaths.get(position), imageWidth, imageWidth);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(imageWidth,
imageWidth));
imageView.setImageBitmap(image);
// image view click listener
imageView.setOnClickListener(new OnImageClickListener(position));
return imageView;
}
Any help would be highly appreciated.
it is custom method defined in the class (in the tutorial you should have missed to copy a method named decodeFile() ) check again ..
Use this
Bitmap image = BitmapFactory.decodeFile(_filePaths.get(position), imageWidth, imageWidth);

how programmatically add imageview under other imageview

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

how to find the last Image of Gallery in android

I am working on Gallery View in android. I am sliding the Images in Gallery View , I need to check the last Image of the Gallery images. After reaching at the end of the Gallery Images if user swipe more than I have to move to another activity . I am confused about how to detect the End of the Gallery Images.
public View getView(int index, View view, ViewGroup viewGroup) {
ImageView img = new ImageView(mContext);
img.setImageResource(CopyRightPagesActivity.typedImgsCopyRight.getResourceId(index, 1));
img.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
img.setScaleType(ImageView.ScaleType.FIT_XY);
return img;
}

Rotating image in gridview

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.

Gallery View not filling complete screen when get created

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;
}

Categories

Resources