我是android的新手,我尝试使用带有按钮的circuler Reveal。因此,当我点击按钮时,它移动到中心,然后来自中心的圆圈显示。 当我按下后退按钮时,循环显示继续,我的按钮移动到中心位置。直到这里它工作正常。但是 问题现在出现之后第一次没有点击按钮循环显示再来一次?
package redblood.myself.animateparticulerviewonlayout;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewPropertyAnimator;
import android.view.animation.Interpolator;
import android.widget.ImageButton;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageButton imageButton;
ImageView imageView;
// here we save initial postion of button so we can come again
// after moving
int buttonxIntial,buttonYinitial;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView= (ImageView) findViewById(R.id.screenView);
imageView.setVisibility(View.INVISIBLE);
imageButton= (ImageButton) findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonxIntial= (int) v.getPivotX();
buttonYinitial= (int) v.getPivotY();
final ViewPropertyAnimator anim= v.animate().xBy((imageView.getWidth()/2)-20).yBy((imageView.getHeight()/2)-15).setDuration(1000);
anim.setListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
show(imageView);
}
});
}
});
}
private void show(View view) {
int x=view.getWidth()/2;
int y=view.getHeight()/2;
int rad=Math.max(view.getWidth(),view.getHeight());
final Animator anim= ViewAnimationUtils.createCircularReveal(view,x,y,0,rad);
anim.setDuration(1000);
view.setVisibility(View.VISIBLE);
anim.start();
}
//here i am override back buuton so when i back my button can go
//to its orginal position
@Override
public void onBackPressed() {
hide(imageView);
}
private void hide(ImageView view) {
int x=view.getWidth()/2;
int y=view.getHeight()/2;
int rad=Math.max(view.getWidth(),view.getHeight());
Animator anim= ViewAnimationUtils.createCircularReveal(view,x,y,rad,0);
anim.setDuration(1000);
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
imageView.setVisibility(View.INVISIBLE);
imageButton.animate().x(buttonxIntial).y(buttonYinitial).setDuration(1000);
}
});
anim.start();
}
}
***//This is activity_main.xml***
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg"
android:id="@+id/imageButton"
android:src="@drawable/ic_album_24dp"
/>
<ImageView
android:background="#343434"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/screenView"
/>
</RelativeLayout>