I am making an app that has a list of two items that have two gifs, one in each (the same gif). As you can see in the video, when you click on the gif, the gif is not shown in motion and it is shown as a solid image, but when you click the right or left arrow it is shown correctly in motion despite that the function to load the gif is called in both the OnCreate function and the btn_next.setOnClickListener() function and the btn_prev.setOnClickListener() function. So the same thing is done but onCreate() the gif is not shown in motion and on btn_next and btn_prev setOnClickListener() it is shown. Please, someone could tell me what could be happening here? Bcause actually the same code is executed but on the first clikc is not working
Video: https://youtube.com/shorts/S5fDsqu3CeI?feature=share
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
init();
glide();
}
private void init() {
img_1 = findViewById(R.id.img_image1);
btn_prev = findViewById(R.id.dialog_btn_prev);
btn_next = findViewById(R.id.dialog_btn_next);
btn_next.setOnClickListener(view -> {
glide();
});
btn_prev.setOnClickListener(view -> {
glide();
}
});
}
public void glide() {
Glide.with(getApplicationContext()).load(R.drawable.earth).into(img_1);
}
Change your code when you are initialising Glide with this.
Glide.with(this).asGif().load(R.raw. earth).into(img_1);
Try this!
Glide.with(this)
.asGif()
.load(R.raw._img)
.centerCrop()
.into(new ImageViewTarget<GifDrawable>(img) {
#Override
protected void setResource(#Nullable GifDrawable resource) {
img.setImageDrawable(resource);
}
});
Add dependencies build.gradle(App Level)
implementation 'com.github.bumptech.glide:glide:4.13.2'
Related
I've found the solution for this problem, but it doesn't work quite well. It will restore some position but not exactly. If put some headline at the top, on rotation it won't be the same title on top but it won't put it back at the top too. I have different portrait and landscape UI, so I used this code
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putIntArray(SCROLL_STATE_KEY, new int[]{mScrollView.getScrollX(),
mScrollView.getScrollY()});
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
final int[] scrollPosition = savedInstanceState.getIntArray(SCROLL_STATE_KEY);
if(scrollPosition != null) {
mScrollView.post(new Runnable() {
#Override
public void run() {
mScrollView.scrollTo(scrollPosition[0], scrollPosition[1]);
}
});
}
}
Here's the gif. It is too fast but, I can't put more than 2MB here, so , hope you can see what's not working properly
Not the solution as sourcecode but an idea how to do this userfriendly:
Before rotation I would get the (text) content of the first visible few words/images of the scrollview and try to scroll the scrollview so that these words/images are on top of the scrollview again after the rotation.
I hav-nt done this for floating text yet but successfully for listviews and grids.
I'm still learning how to program in Android and I created an Activity with a start view:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
mMainView = (RajawaliSurfaceView) findViewById(R.id.my_view);
mMainView.setSurfaceRenderer(new Renderer());
mMainView.setZOrderOnTop(false);
// Also set up a surface view I'd like to switch on back and forth
mGLView = new GLSurfaceView(this);
mGLView.setEGLContextClientVersion(2);
mGLView.setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR);
mGLView.setRenderer(cameraRenderer_ = new GLRenderer(this));
mGLView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
This works and I can even switch to the GL view seamlessly:
public void switchToGLView(View view) {
setContentView(mGLView);
}
But after I'm done with the GL thing, I have the GLRenderer call a restore function in my main activity
public void restoreRenderer() {
// Restoring the previous view (my_view) with the non-GL renderer
setContentView(mMainView); <<<<<< FREEZES HERE (blackscreen)
}
and unfortunately this never works: the screen remains black from the previous GL renderer and by debugging the setContentView line I discovered that:
I don't get ANY error or warning log in the Android Monitor
The execution NEVER continues. It gets stuck in there forever
Any tips or hints on how to solve this?
You could solve this problem using a Frame or Relative layout.
If I understand correctly, both mMainView and mGLView are fullscreen.
There are a few options:
You could change the visibility of the views:
public void switchViews() {
if(mMainView.getVisibility() == View.VISIBLE){
mMainView.setVisibility(View.GONE);
mGLView.setVisibility(View.VISIBLE);
} else{
mMainView.setVisibility(View.VISIBLE);
mGLView.setVisibility(View.GONE);
}
}
The other option is to change the Z order of the views:
public void switchViews(View view){
view.bringToFront();
view.invalidate();
}
Unless there are any drawbacks you encountered using the above methods.
I have this in my MainActivity.java:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupHomeScreen();
}
public void setupHomeScreen() {
File latestPic = getMostRecentSnappiePicture();
if(latestPic != null){
//display pic
LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackground(Drawable.createFromPath(latestPic.getAbsolutePath()));
}
else{
layout.setBackgroundDrawable(Drawable.createFromPath(latestPic.getAbsolutePath()));
}
//hide tutorial
findViewById(R.id.howitworks).setVisibility(View.INVISIBLE);
}
else{
//show tutorial
findViewById(R.id.howitworks).setVisibility(View.VISIBLE);
new ShowcaseView.Builder(this)
.setTarget(new ActionViewTarget(this, ActionViewTarget.Type.HOME))
.setContentTitle("ShowcaseView")
.setContentText("This is highlighting the Home button")
.hideOnTouchOutside()
.build();
}
}
}
As you can see, in onCreate, it calls setupHomeScreen and checks if a file exists. If it doesn't exist, it displays a tutorial "howitworks" layout image as well as building a showcase view.
So this all works fine and well. The only issue comes when trying to leave the activity while the showcaseView is still there, OR sometimes even after you exit the showcase view and try and launch the new activity, this error comes up: ShowcaseView - width and height must be > 0
As you can see in the answers, the solution is to only create the showcase view in the callback after the original view has been created like so:
someView.post(new Runnable() {
#Override
public void run() {
// my ShowcaseView builder here
}
});
The only thing is, I have no idea where to put this, since my showcase view should only show up if the file from getMostRecentSnappiePicture() is null. So how can I put the view creation callback inside of my logic to check that that file is null first?
it looks like you're highlighting the HOME button instead of the 'howitworks' view. Try switching the line
.setTarget(new ActionViewTarget(this, ActionViewTarget.Type.HOME))
with
.setTarget(new ViewTarget(R.id.howitworks,this));
Also, the following video might help. It's 20 minute tutorial on how to use ShowCaseView inside an activity with 3 buttons. He is declaring an onClickListener where he changes programmatically the TargetView highlighted by the showCaseView
https://www.youtube.com/watch?v=3zdeFSBplps
The video is in spanish, but at least you'll be able to follow the steps, since he's writing the code from scratch.
I need execute a method when the fragment is visible (to the user).
Example:
I have 2 buttons (button 1 and button 2) ,
2 fragments(fragment 1 and fragment 2)
and the method loadImages() inside the class fragment 2.
when I press "button2" I want to replace fragment 1 by fragment 2
and then after the fragment 2 is visible (to the user) call loadImages().
I tried to use onResume() in the fragment class but it calls the method before the fragment is visible and it makes some delay to the transition.
I tried setUserVisibleHint() too and did not work.
A good example is the Instagram app. when you click on profile it loads the profile activity first and then import all the images.
I hope someone can help me. I will appreciate your help so much. Thank you.
Use the ViewTreeObserver callbacks:
#Override
public void onViewCreated(View v, Bundle savedInstanceState) {
super.onViewCreated(v, savedInstanceState);
final View view = v;
// Add a callback to be invoked when the view is drawn
view.getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
#Override
public void onDraw() {
// Immediately detach the listener so it only is called once
view.getViewTreeObserver().removeOnDrawListener(this);
// You're visible! Do your stuff.
loadImages();
}
});
}
I'm a little confused by what you are trying to do. It sounds like the images are loading too fast for you... so does that mean that you have the images ready to display? And that is a bad thing?
My guess (and this is just a guess) is that Instagram does not have the profile pictures in memory, so they have to make an API call to retrieve them, which is why they show up on a delay. If the same is the case for you, consider starting an AsyncTask in the onResume method of the fragment. Do whatever loading you need to do for the images in the background, and then make the images appear in the onPostExecute callback on the main thread. Make sure you only start the task if the images are not already loaded.
However, if you already have the images loaded in memory, and you just want a delay before they appear to the user, then you can do a postDelayed method on Handler. Something like this:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
loadImages();
}
}, 1000);
Edit
As kcoppock points out, the handler code is pretty bad. I meant it to be a quick example, but it is so wrong I should not have included it in the first place. A more complete answer would be:
private Handler handler;
public void onResume(){
super.onResume();
if(handler == null){
handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
loadImages();
}
}, 1000);
}
}
public void onDestroyView(){
super.onDestroyView();
handler.removeCallbacksAndMessages(null);
handler = null;
}
Use the onActivityCreated() callBck
I want to make 2 animation on activity for 2 images, and I want to do it with 2 conditions:
1. I want to start the animation after the activity finished to load to page, so instead of putting animation code under onCreate i put it under onResume is that OK? There is a better way to do it?
2. I want the second animation will start only after the first animation is finished...
Thanks
You'll want to use an Animation.AnimationListner You can set one on your first animation that will get a callback when the animation is complete. Inside that callback you can add the code that will start the second animation.
Depending on the API level you are coding for you can use AnimationSet or AnimatorSet. Also if you are extending View or one of its subclasses you can override View.onAnimationStart() and View.onAnimationFinish(). Or use the listener Tim mentions.
public class SplashActivity extends Activity{
Animation FadeInanimation, FadeOutanimation;
ImageView img;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
img= (ImageView) findViewById(R.id.img);
//Your Code Block....
FadeInanimation = AnimationUtils.loadAnimation(this,R.anim.image_fadein);
//FadeInanimation.setRepeatCount(Animation.INFINITE);
//FadeInanimation.setRepeatMode(Animation.RESTART);
FadeInanimation.setAnimationListener(FadeInAnimationListener);
FadeOutanimation = AnimationUtils.loadAnimation(this,R.anim.image_fadeout);
//FadeOutanimation.setRepeatCount(Animation.INFINITE);
//FadeOutanimation.setRepeatMode(Animation.RESTART);
FadeOutanimation.setAnimationListener(fadeOutAnimationListener);
img.startAnimation(FadeInanimation);
}
AnimationListener FadeInAnimationListener = new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
plane.startAnimation(FadeOutanimation);
}
};
}