Android: Picasso enlarging Image on Click? - java

I was using Picasso for a while now and I'm very satisfied with it. However I'm in need of enlarging an imageview's image which was loaded by Picasso. As in, I simply want to create a zoomImageFromThumb of the image. However the traditional zoomImageFromThumb requires a resource to be present in the project. In my case, Picasso loads it up.. so how do I enlarge an image laoded by Picasso on click of the image view?
My code so far:
ImageBanner= (ImageView) findViewById(R.id.lb_single_image);
Picasso.with(ctx)
.load("www.flickr.com/randomimage.jpg")
.placeholder(R.drawable.loading_placeholder)
.error(R.drawable.error_placeholder)
.into(ImageBanner);
ImageBanner.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//how do I enlarge the picasso image?
}
});

Instead of setOnClickListener() you can use PhotoViewAttacher:
ImageBanner= (ImageView) findViewById(R.id.lb_single_image);
Picasso.with(ctx)
.load("www.flickr.com/randomimage.jpg")
.placeholder(R.drawable.loading_placeholder)
.error(R.drawable.error_placeholder)
.into(ImageBanner);
PhotoViewAttacher mAttacher = new PhotoViewAttacher(ImageBanner);
Only one line, and it will works.

Related

Change the color of the icon in the ImageView in the java class

I showed the icons in the program via ImageView. The yellow icons should turn red when the button is pressed. I tried all the methods, but none of them worked. ...Backgroundcolor... He turned ImageView red in full square shape. I just want the icon to change color. This is very difficult‚ Please help. Thank you.
My code:
ImageView Test = (ImageView) findViewById(R.id.TestImageIcon);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Here
Test.setcolor(R.id.red);
}
});
you change the tint of drawable like that:
imageView.setColorFilter(context.getResources().getColor(R.color.yourColor), PorterDuff.Mode.SRC_IN);
It can be done using setColorFilter(color).
Try to change color of icon programatically with
Test.setColorFilter(ContextCompat.getColor(YourActivity.this, R.color.red));

how to set up a wallpaper app using firebase recyclerview?

I created a wallpaper app ,I'm using Firebase to upload images to a database and shown it to RecyclerView.
I can see the images that I uploaded in Firebase through the RecyclerView and I can also pass that image to a another activity.
but my problem is, I can't set the image as wallpaper, when I set the button the image is disappearing.
my code to set wallpaper:
img = (ImageView) findViewById(R.id.images);
Intent intent = getIntent();
String webUrl = intent.getStringExtra("URL");
Picasso.get().load(webUrl).into(img);
fab1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
WallpaperManager wallpaper = WallpaperManager.getInstance(getApplicationContext());
try {
wallpaper.setResource(+ R.drawable.pug); //by using this code i can set a image in directory a wallpaper
//wallpaper.setResource(+ R.id.images); //i tried this one it doesn't work it just crashes the app
}catch (IOException e){
e.printStackTrace();
}
}
});
what I want is to set any image shown in the ImageView
is there any way
I'm assuming you already have the permission in place for this:
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
I'm not sure what you are trying to achieve, but I am assuming you want the user to choose an image from the list and set it as wallpaper.
You can only pass a Bitmap or Resource ID to WallpaperManager. In this case, fetch the image as a Bitmap first, and then load into the ImageView.
Since you are already using Picasso, you can do this:
Picasso.with(this).load(webUrl).centerCrop().into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
wallpaper.setBitmap(bitmap);
}
}

Unable to enter the if statement | Android Studio

I am trying to fade two images back and forth:
Image1 with an id of imageone
Image2 with an id of imagetwo
For both the images, I have linked onClick in UI properties to the onClick method in the below Java code.
After the launching the app, when I click on image1, it is supposed to fade out and image2 should fade IN.
After analysis, I determined that the flow is not entering the if statement and is always getting directed to else no matter what.
How can I solve this?
Why is the if statement if (view.getId() == R.id.imageone) not letting the flow inside it even though the entry condition view.getId() == R.id.imageone is true?
public class MainActivity extends AppCompatActivity {
public void onClick(View view) {
ImageView imageone = (ImageView) findViewById(R.id.imageone);
ImageView imagetwo = (ImageView) findViewById(R.id.imagetwo);
if (view.getId() == R.id.imageone) {
imageone.animate().alpha(0f).setDuration(2000);//image1 fade OUT
imagetwo.animate().alpha(1f).setDuration(2000);//image2 fade IN
} else {
imagetwo.animate().alpha(0f).setDuration(2000);//image2 fade OUT
imageone.animate().alpha(1f).setDuration(2000);//image1 fade IN
}
}
}
#Narendra Jadon : You are right ,The problem was with the layout. The image two was on top of image one. And i solved it by setting the image one on top of image 2. Thank you everyone for your help.

How to check image resource of an imageview programmatically?

I want to check background of my imageview and do sth if it equals to some drawable in my drawable folder. How can I do that? I have try this code but no answer:
layoutRoot.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (view.getBackground() == R.drawable.grid_single_bg_selected) {
//Do sth
}
}
});
When you set the background drawable also do set a tag to the imageview
imageView.setTag(R.drawable.demo);
And then compare
if (((Integer) imageView.getTag()) == R.drawable.demo){
// Do stg
}
If you are setting the imageView programmatically, while setting, also set a tag to this imageView. Now, when you want to find out the drawable, you can do it by retrieving the tag of this imageView.

Using Async task for gridview image loading

Ive been searching for a solution for hours now, hoping someone can help?
Ive got a swipe tab pages ui and each page has a gridview that loads images, works as expected but its very slow, even on a high end device. Can i set image resources using Async task? My adapter for my gridview is below:
public class PcAdapter extends BaseAdapter {
private Context context;
private Integer[] imageIds = {
R.drawable.pcserioussam, R.drawable.pc_trinetwo,
R.drawable.pc_leftfordead, R.drawable.pc_dungeondefenders,
R.drawable.pc_portaltwo, R.drawable.pc_spaz,
R.drawable.pc_laracroftattoo, R.drawable.pc_goatsim,
R.drawable.pc_deadblock
};
public PcAdapter(Context c) {
context = c;
}
public int getCount() {
return imageIds.length;
}
public Object getItem(int position) {
return imageIds[position];
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View view, ViewGroup parent) {
ImageView iview;
if (view == null) {
iview = new ImageView(context);
iview.setLayoutParams(new GridView.LayoutParams(230,300));
// iview.setScaleType(ImageView.ScaleType.FIT_CENTER);
iview.setPadding(5, 5, 5, 5);
} else {
iview = (ImageView) view;
}
iview.setImageResource(imageIds[position]);
return iview;
}
}
#mmlooloo's answer is totally relevant and I totally agree with him.
In complement, and to give a pist of solution, I would suggest you to use the library Picasso which is really easy to use and really powerful.
In your Adapter you can load your Images into the ImageView like this:
// Trigger the download of the URL asynchronously into the image view.
Picasso.with(context)
.load(url) // url of your image
.placeholder(R.drawable.placeholder) // drawable to display while downloading the image
.error(R.drawable.error) // drawable in case of failure
.into(imageView); // ImageView where you want to load the picture.
Picasso will take care of the caching and memory issues for you.
To learn more about Picasso and start using it, take a look here.
Your problem arises because of these several thing:
you are using async task with asyncTask.execute that is run one async task at a time so I recommend you this link:
Running multiple AsyncTasks at the same time -- not possible?
you are using viewpager and if your gridview is inside fragment viewpager when loads your current tab page also loads two tabs beside it to make user experience well when clicking other tabs and not wait to view inflated and so on ... so you must call async task inside tab select not inside page select and cancel it on onTabOnselected (you can not make viewpager to set.limitOffScreenPage(0); that wont work and the 0 is ignored. I highly recommend you use libraries to download images because this task requires lot of tips and tricks to have a smooth user experience and by itself it is an another project(do not reinvent wheel, if others invents for you)
you are decoding images on UI thread and decoding one at a time
your internet connection is slow
your image sizes are large
hope help you !

Categories