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);
Related
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
Guys my program takes really long to open my gallery activity. I have gone through my code and I think I have found whats making the the activity slow down. I think its the part where the image gets decoded. It slows down when there are many images in my gallery and it looks like it takes time to decode them all.
This is my code
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(_activity);
} else {
imageView = (ImageView) convertView;
}
// THIS BELOW IN SEPARATE THREAD
Bitmap image = decodeFile(_filePaths.get(position), imageWidth, imageWidth);
// THIS ABOVE IN SEPARATE THREAD
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;
}
I think putting the image decoding line in a separate thread might speed up te program. Can anyone tell me how to do it?
You can not use a thread like that. Your whole function will still have to wait for the image to get decoded before returning, because you are returning imageView variable.
First of all, why are you casting convertView to an ImageView, I highly doubt its a good idea to do that. You might have to change your xml and add an imageView, if its missing. Then get a reference to that imageView in this function by using something like
final ImageView listImage = (ImageView) convertView.findViewById(R.id.listImage);
Finally, fire off your thread and set the image into this listImage variable, while returning convertView. Use an ExecuterService.
Here is a simple example.
private final ExecutorService executorService = Executors.newCachedThreadPool();
private final int layoutResourceId; //set this
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater = (_activity).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
}
final ImageView listImage = (ImageView) convertView.findViewById(R.id.listImage);
// THIS BELOW IN SEPARATE THREAD
executorService.submit(new Runnable() {
#Override
public void run() {
Bitmap image = decodeFile(_filePaths.get(position), imageWidth, imageWidth);
listImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
listImage.setLayoutParams(new GridView.LayoutParams(imageWidth, imageWidth));
listImage.setImageBitmap(image);
listImage.setOnClickListener(new OnImageClickListener(position));
}
});
return convertView;
}
I hope you got the general idea.
I highly suggest you use Picasso. Google it. You can do all this without having to implement your own threading, get the advantage of caching and do it in far lesser lines of code. You will run out of memory on low end devices because you are not recycling your bitmap.
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;
}
I have a GridView with 4 columns and n-rows. Each cell is simply an ImageView. My custom adapter's getView() code looks like this:
#Override
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);
} else {
imageView = (ImageView) convertView;
}
imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(1, 1, 1, 1);
//imageView.setAdjustViewBounds(true);
if(imageLoader != null) {
String url = randomImage();
imageView.setContentDescription(url);
imageLoader.DisplayImage(url, imageView);
}
return imageView;
}
The imageLoader will pick a random URL and in the background, go fetch it and update the ImageView accordingly. That works fine and dandy. The problem is that the resulting images seem to be using a scale type of FIT_START instead of FIT_XY. I'm explicitly setting this to FIT_XY and even set it again inside of the code that sets the imageView's drawable... still its not FIT_XY and i'm stumped. Ideas?
Edited to remove the call to setAdjustViewBounds().
So I just found the issue. I was using Romain Guy's code for creating a RoundedDrawable from a bitmap -- to get rounded corners, ala iOS, as shown here: curious-creature.org/2012/12/11/… ... when i skip the RoundedDrawable conversion, it works fine. Something in the RoundedDrawable code is what is throwing this whole thing off. I'll just need to find another method to round the corners of the imageview. I am using CENTER_CROP now thanks to #kcoppock ! :)
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.