I want to make it so that once I press a button, it moves and does an animation by quickly swapping the src of the imageview mid animation.
I've tried to use Thread.sleep .
first.setImageResource(R.drawable.cata2);
first.setImageResource(R.drawable.catb2);
ObjectAnimation Code
x += 10
first.setImageResource(R.drawable.cata2);
ObjectAnimator d = ObjectAnimator.ofFloat(first, "translationX", x);
d.setDuration(50);
d.start();
It always just switches to cata2.
Try this one. If the changing image resource not working, try to change it in background resource. Just adjust some duration and the thread sleep.
in C#:
Try to put this all inside the listener of your button
Setting up the Animation
var clockwise = AnimationUtils.LoadAnimation(this.Activity, Resource.Animation.clockwise);
clockwise.Duration = 2000;
To set your first to cata2
this.Activity.RunOnUiThread(() =>
{
first.setImageResource(R.drawable.cata2);
});
To change with animation from cata2 to catb2
Thread t = new Thread(delegate ()
{
first.StartAnimation(clockwise);
Thread.Sleep(500);
this.Activity.RunOnUiThread(() =>
{
first.setImageResource(R.drawable.catb2);
first.ClearAnimation();
return;
});
});
t.Start();
We can add a listeter to your animation
d.addListener(this);
Then generate overridess
#Override
public void onAnimationStart(Animator animation){
//add some codes
}
#Override
public void onAnimationEnd(Animator animation){
//add some codes
}
#Override
public void onAnimationRepeat(Animator animation){
//add some codes
}
#Override
public void onAnimationCancel(Animator animation){
}
Related
I have a star animation for giving rating and I created three of such LottieAnimationViews inside my fragment for the purpose of reuse-ability. The LottieAnimation doesn't even start when I run it from inside the onCreate() method of my Fragment (I am using support Fragments). The exact same code runs the animation if it is inside an Activity instead of a fragment. Here's my code...
mLottieStarView1 = findViewById(R.id.rating_lottie_star_1); //LottieView
starLayout1 = findViewById(R.id.rating_star_layout_1); //Enclosing LinearLayout
mValueAnimator1 = ValueAnimator.ofFloat(0f, 1f).setDuration(1500);
//Create animation update methods
mValueAnimator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { //Star 1
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
mLottieStarView1.setProgress((Float) valueAnimator.getAnimatedValue());
}
});
//Set listener on enclosing layout to run animation on touch
starLayout1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mLottieStarView2.setProgress(0f);
mLottieStarView3.setProgress(0f);
mValueAnimator1.start();
}
});
Make sure you set the image View to isClickable = true
and isFocusable = true in xml.
Start the lottie animation using the lottieView.playAnimation(); method and for the unclick try this
if(lottieTest.getFrame() == lottieTest.getMaxFrame()) {
lottieTest.setFrame(1);
}
I am a hybrid apps developer and new to android. I am trying to fix an android related issue for my hybrid app.
I have the following two methods on my activity: onStart and onPause.
When the app starts I need it to start as usual. When the app is on pause, I need to show an image (my companyLogo). If I set it with setContentView(R.layout.activity_base), the image is shown when the app starts again. Is there a way to dynamically create an image, and show it when app is on pause? Also, how do I remove the image once it starts? Since I am not hands on in Android, I am not sure how to go about it.
I suppose I need to create some sort of a Dialog and set an image view inside it.
Also, I am not sure, how to remove this image, when the app Starts again.
My code snippet:
public void onStart() {
Toast.makeText(getApplicationContext(), "App is in foreground", Toast.LENGTH_LONG).show();
}
public void onPause() {
Toast.makeText(getApplicationContext(), "App is in background", Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_base);
}
This code is im run it. i think u need this.
inside ur activity
ImageView img_view;
TextView txt_show;
int images[] = {R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
img_view = (ImageView) findViewById(R.id.img_show);
txt_show = (TextView) findViewById(R.id.txt_my);
}
private void animate(final ImageView imageView, final int images[], final int imageIndex, final boolean forever) {
int fadeInDuration = 1000;
int timeBetween = 5000;
int fadeOutDuration = 1000;
imageView.setVisibility(View.INVISIBLE);
imageView.setImageResource(images[imageIndex]);
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator());
fadeIn.setDuration(fadeInDuration);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator());
fadeOut.setStartOffset(fadeInDuration + timeBetween);
fadeOut.setDuration(fadeOutDuration);
AnimationSet animation = new AnimationSet(false);
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
animation.setRepeatCount(1);
imageView.setAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
if (images.length - 1 > imageIndex) {
animate(imageView, images, imageIndex + 1, forever);
} else {
if (forever) {
animate(imageView, images, 0, forever);
}
}
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
#Override
protected void onPause() {
super.onPause();
txt_show.setVisibility(View.GONE);
img_view.setVisibility(View.VISIBLE);
animate(img_view, images, 0, false);
}
#Override
protected void onResume() {
super.onResume();
img_view.setVisibility(View.GONE);
txt_show.setVisibility(View.VISIBLE);
}
inside ur xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/txt_my"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:gravity="center"
android:text="buvan"
android:textColor="#color/colorPrimary" />
<ImageView
android:id="#+id/img_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
onResume is called of minimize from cme to screen.
try this one
#Override
protected void onResume() {
super.onResume();
setContentView(R.layout.activity_main);
}
The feature you want will not work without FLAG_SECURE, because when you send the app in the background, the view you are viewing, is an OS-handled view and only OS has the permission to update the view. For that reason, you have to call FLAG_SECURE to notify OS that this view needs to be secured. Otherwise, changing views would have worked.
I think you got the idea.
I want to move a button(button1) when I click on it threw the x-axis.
Here is my code:
btn=(Button)findViewById(R.id.button1);
currentX=btn.getX();
currentY=btn.getY();
moveX=ObjectAnimator.ofFloat(btn,"translationX",currentX,currentX+10);
moveY=ObjectAnimator.ofFloat(btn,"translationY",currentY,currentY);
set=new AnimatorSet();
set.playTogether(moveX,moveY);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
set.start();
currentX=v.getX();
currentY=v.getY();
}
});
It moves at the first click, but after that it just starts from the beginning, instead of moving 10dp further on the x-axis. what am I missing?
You need to either create a new animator or you need to update the values of the moveX and moveY animations. (Actually, you do not need the moveY animation since you are not changing the Y position.)
Also, Views have a more performant animation capability using .animate() to acquire a ViewPropertyAnimator. I would refactor your code this way:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.animate().xBy(10).start()
}
});
If later you decide you also want to animate the Y position, you can simple add .yBy(value) before the .start().
i don't think the v.getx will change after the anime
try this :
int multi =1;
btn=(Button)findViewById(R.id.button1);
currentX=btn.getX();
currentY=btn.getY();
moveX=ObjectAnimator.ofFloat(btn,"translationX",currentX+10*(multi-1),currentX+10*multi);
moveY=ObjectAnimator.ofFloat(btn,"translationY",currentY,currentY);
set=new AnimatorSet();
set.playTogether(moveX,moveY);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
set.start();
multi++;
currentY=v.getY();
}
});
It is because you are not holding the position of x-axis to which your button has moved,after click you set it again to intial value.
Change these lines
use a static variable.
static int x_axis=10+btn.getX();
currentX=x_axis;
currentY=btn.getY();
moveX=ObjectAnimator.ofFloat(btn,"translationX",currentX,currentX);
moveY=ObjectAnimator.ofFloat(btn,"translationY",currentY,currentY);
set=new AnimatorSet();
set.playTogether(moveX,moveY);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
set.start();
x_axis=x_axis+10;
currentY=v.getY();
}
});
I am trying to make a animation on a textView and to change the content on onAnimationEnd but it change jump one text and take the next one. I don't know if you understand great, my english is not so good.
This is my code:
final Integer[][] listeQuestions = new Integer [][]{
{R.string.c1}, {R.string.c2}, {R.string.c3}, {R.string.c4}, {R.string.c5}, {R.string.mot1}, {R.string.c6}, {R.string.c7}, {R.string.c8}, {R.string.c9}, {R.string.c10}, {R.string.mot2},
{R.string.c11}, {R.string.c12}, {R.string.c13}, {R.string.c14}, {R.string.c15}, {R.string.mot3}, {R.string.c16}, {R.string.c17}, {R.string.c18}, {R.string.c19}, {R.string.c20}, {R.string.mot4},
{R.string.c21}, {R.string.c22}, {R.string.c23}, {R.string.c24}, {R.string.c25}, {R.string.mot5}, {R.string.c26}, {R.string.c27}, {R.string.c28}, {R.string.c29}, {R.string.c30}, {R.string.mot6},
{R.string.c31}, {R.string.c32}, {R.string.c33}, {R.string.c34}, {R.string.c35}, {R.string.mot7}
};
boutonResultat.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
resultatTexte.setVisibility(View.VISIBLE);
resultatTexte.setText(listeQuestions[0][0]);
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.laps_temps);
resultatTexte.startAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener() {
Integer curseur=0;
#Override public void onAnimationStart(Animation animation) {}
#Override public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
if(curseur<42) {
curseur++;
resultatTexte.setText(listeQuestions[curseur][0]);
animation.setAnimationListener(this);
resultatTexte.startAnimation(animation);
}
}
});
My animation is simple, it's just a alpha who begin at 0.2 to 1.0 and the duration is 3000.
I tryied a lot of possibilities (change the animation duration, put some elements in onAnimationStart, put a new code to try to block the double, I leaved the setAnimationListener in onAnimationEnd ...) but always the same problem: R.string.c1 appear great and the animation is correct, when it's finish, R.string.c2 appear maybe 0.1seconds and directly appear R.string.c3 and the animation work correctly, and R.string.c4 appear 0.1 seconds and R.string.c5 works great and like this to the end.
Maybe someone already had an equivalent problem and can help me to resolve it.
Thank you and sorry for my english :)
I have a view in the UI that is technically an extended ImageView inside a RelativeLayout that I'd rather not change, if possible. I want to show a Drawable from resources (a small PNG with a gradient or a frame) at specific coordinates as a visual feedback when user is tapping the screen (there even is a onShowPress callback). What is the simplest way to do this? I don't want to modify the layout.xml from which UI is inflated, and I'm not too enthusiastic about overriding onDraw to do extra job there.
This is one easy way to do it using view animation class:
final ImageView aboutButton = (ImageView) findViewById(R.id.AboutButton);
aboutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation myAnim2 = AnimationUtils.loadAnimation(LaunchActivity.this, R.anim.buttons);
aboutButton.startAnimation(myAnim2);
myAnim2.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
//do on click after animation ends
Intent intent = new Intent(LaunchActivity.this, MyDialog.class);
startActivity(intent);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
//change the backround of the button etc on click etc...
}
});
}});