I'm using Picasso to display many small ImageViews in a RecyclerView.
The ImageViews change their Image for 500 milliseconds when I click on them and change it back when the handler with 500 milliseconds end.
This is the code:
Picasso.get().load(imageResources[position]).into(holder1.itemImageView);
holder1.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
final Context context = v.getContext();
Picasso.get().load(pressedResource[position]).into(holder1.itemImageView);
new Handler().postDelayed(new Runnable() {
public void run() {
Picasso.get().load(imageResources[position]).into(holder1.itemImageView);
}
}, 500);
new Thread(new Runnable() {
#Override
public void run() {
EventHandlerClass.startMediaPlayer(v, soundID);
}
}).start();
}
});
I use Picasso to prevent my app from getting OutOfMemory crashes.
The Image Dissapears for like 10 milliseconds, then changes to the other image, after the 500 milliseconds it dissapears again and then it changes back to the default image.
It only dissapears for the first time I click on one of the Images, after that it works without dissapearance.
I think this happens because Picasso is loading the image too slow.
Is there a way to first load the image and display it when I click on the button?
Here is a GIF which shows how it looks like: https://media.giphy.com/media/STlGbpXvT8B9iBvnQS/giphy.gif
In this case it only dissapeared once but sometime it dissapeares twice with one click.
The Images are all 200x200 and around 8kb. The format is .WEBP
How can I prevent this?
The idea is to wait for the Callback and then set the image into the image view. This is achieved by using a CallbackListener.
Picasso.get().load(imageResources[position]).into(holder1.itemImageView);
holder1.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
final Context context = v.getContext();
Picasso.get().load(pressedResource[position]).into(holder1.itemImageViewnew com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
new Handler().postDelayed(new Runnable() {
public void run() {
Picasso.get().load(imageResources[position]).into(holder1.itemImageView);
}
}, 500);
new Thread(new Runnable() {
#Override
public void run() {
EventHandlerClass.startMediaPlayer(v, soundID);
}
}).start();
}
#Override
public void onError() {
}
}));
}
});
Related
I am making an application that generates a random image by a url when touching the button and presents it in the imageview and I use picasso and to change the image you have to clear the cache and I would like someone to tell me how I could go back to the previous image, like a back button
Any help is appreciated from the heart
My code to generate a new image:
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String quest1 = "https://proxxcraft.com/";
Picasso.get().load(quest1).memoryPolicy(MemoryPolicy.NO_CACHE).into(memeRandomView);
Picasso.get().isLoggingEnabled();
}
});
you should save the image as a bitmap, save it in an array or some other data structure. I know how to do it with glide but you should check for how to set callbacks to know when the image is loaded.
private void loadImage() {
Picasso.with(this)
.load("https://proxxcraft.com/")
.into(mContentTarget);
}
private Target mContentTarget = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
//save the bitmap in an array so you can use it later.
Imageview.setImageBitmap(bitmap);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
I am using a fragment and there is a button as soon as I click on it the background will become dim and textView will be visible.The appearance of the dimming effect and text will take place at once.For some reason I don't get these result.
Here is my code:-
activate_wifi_button = (Button)wifi_and_hotspot.findViewById(R.id.Activate_wifi);
activate_wifi_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
#Override
public void run() {
WindowManager.LayoutParams layoutParams=getActivity().getWindow()
.getAttributes();
layoutParams.dimAmount = 0.7f;
getActivity().getWindow().setAttributes(layoutParams);
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
text.setVisibility(View.VISIBLE);
text_animate_dots.setVisibility(View.VISIBLE);
timer.cancel();
}
};
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(runnable);
}
}, 2000, 2000);
}
});
I have used handler and runnable because I want to animate the textview for 2 sec but the animate part can come latter at first I need to do the above task.
For the desired output, you can take semi-transparent layout and textView in FrameLayout. In onclick method, set its visibility VISIBLE; and for better user experience, set Fade animation to textview.
Hi so I have the following code:
In the onCreate:
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
refreshContent();
}
});
And in another method:
private void refreshContent(){
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
db = new DatabaseHelper(getApplicationContext());
new RefreshScores().execute();
list.clear();
list.addAll(db.getTracked());
db.closeDB();
mSwipeRefreshLayout.setRefreshing(false);
adapter.notifyDataSetChanged();
}
},2000);
}
Currently, when I pull down near the top of my app, the refresh spinner shows up and starts spinning. It disappears after 2 seconds. However, I want it to remain visible until RefreshScores().execute() and list.clear(); list.addAll(db.getTracked()); db.closeDB(); have occured. The RefreshScores is an AsyncTask. Not really sure how to do this.
AsyncTask has a callback named onPostExecute. This callback will be invoked in the UI thread when doInBackground() returns. See http://developer.android.com/reference/android/os/AsyncTask.html
You may try using
mSwipeRefreshLayout.setRefreshing(false);
directly in this callback, and move other calls which require new data to the callback as well.
Edit
mSwipeRefreshLayout.post(new Runnable() {
#Override
public void run() {
mSwipeRefreshLayout.setRefreshing(false);
}
});
I have a class which generates a pdf file.
This class is extended by Activity that means is have onCreate, onPause (and so on) methods.
While a pdf file is generating it takes some time to be generated so I need to show a progress bar to notify the user that the file is generating at the moment.
Everything works fine expect that when I click on a generate button the progress bar freezes for a few seconds (according to Logcat: progress bar freezing while file is generating).
So my problem is that, that I don't know how to make the progress bar unstoppable while the file is generating.
Below is my code:
Log.i(TAG, "PDF generation: " + position);
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setCancelable(true);
dialog.setIndeterminate(true);
dialog.setMessage("Generating...");
dialog.show();
new Thread(new Runnable() {
#Override
public void run() {
startActivity(new Intent(getApplicationContext(),
PdfGenerator.class));
runOnUiThread(new Runnable() {
#Override
public void run() {
dialog.hide();
}
});
}
}).start();
Change your code as for starting an Activity from Thread using runOnUiThread:
///... your code here..
new Thread(new Runnable() {
#Override
public void run() {
Your_Current_Activity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
dialog.cancel();
// START ACTIVITY HERE
Your_Current_Activity.this.startActivity(new
Intent(Your_Current_Activity.this,
PdfGenerator.class));
}
});
}
}).start();
I have a button that I don't want to be clickable until a certain amount of time has run (say, 5 seconds?) I tried creating a thread like this
continueButtonThread = new Thread()
{
#Override
public void run()
{
try {
synchronized(this){
wait(5000);
}
}
catch(InterruptedException ex){
}
continueButton.setVisibility(0);
}
};
continueButtonThread.start();
But I can't modify the setVisibility property of the button within a different thread. This is the error from the LogCat:
10-02 14:35:05.908: ERROR/AndroidRuntime(14400): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
Any other way to get around this?
The problem is that you can touch views of your activity only in UI thread. you can do it by using runOnUiThread function. I would like to suggest you to use
handler.postDelayed(runnable, 5000)`
You must update your view from UI-thread. What you are doing is you are updating from non-ui-thread.
Use
contextrunOnUiThread(new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
}});
or use handler and signalize hand.sendMessage(msg) when you think is right time to update the view visibility
Handler hand = new Handler()
{
#Override
public void handleMessage(Message msg) {
/// here change the visibility
super.handleMessage(msg);
}
};
You can use the postDelayed method from the View class (A Button is a child of View)
here is simple answer i found
Button button = (Button)findViewBYId(R.id.button);
button .setVisibility(View.INVISIBLE);
button .postDelayed(new Runnable() {
public void run() {
button .setVisibility(View.VISIBLE);
}
}, 7000);