屬性動畫點選自定義View實現拉伸收縮的效果
阿新 • • 發佈:2019-02-11
public class StartActivity extends AppCompatActivity {
private int i=10;
private Handler handler=new Handler();
private int a=0;
ImageView icon;
ImageView gt;
ImageView copy;
ImageView xg;
private ObjectAnimator animator;
private ObjectAnimator fanimator;
private ObjectAnimator animator1;
private ObjectAnimator fanimator1;
private ObjectAnimator animator2;
private ObjectAnimator fanimator2;
private ObjectAnimator animator3;
private ObjectAnimator fanimator3;
private Button jump;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
icon = (ImageView) findViewById(R.id.icon);
gt = (ImageView) findViewById(R.id.gt);
copy = (ImageView) findViewById(R.id.copy);
xg = (ImageView) findViewById(R.id.xg);
jump = findViewById(R.id.jump);
doJump();
//-----伸出時的動畫
animator = ObjectAnimator.ofFloat(icon, "rotation", 0f, 180f);
animator1 = ObjectAnimator.ofFloat(gt, "translationX", 0f,-80f);
animator2 = ObjectAnimator.ofFloat(copy, "translationX", 0f,-160f);
animator3 = ObjectAnimator.ofFloat(xg, "translationX", 0f,-240f);
//----縮回時的動畫
fanimator = ObjectAnimator.ofFloat(icon, "rotation", 0f, -180f);
fanimator1 = ObjectAnimator.ofFloat(gt, "translationX", -80f,0f);
fanimator2 = ObjectAnimator.ofFloat(copy, "translationX", -160f,0f);
fanimator3 = ObjectAnimator.ofFloat(xg, "translationX", -240f,0f);
//給伸出動畫設定監聽
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
icon.setImageResource(R.drawable.tamkero3);//動畫結束改變圖片
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
//給縮回動畫設定監聽
fanimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
icon.setImageResource(R.drawable.xlistview_arrow);//改變圖片
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
icon.setOnClickListener(new View.OnClickListener() {//圖片的點選事件
@Override
public void onClick(View view) {
a++;
if(a%2==1){//第一次點選是實現伸出效果
AnimatorSet animSet = new AnimatorSet();//動畫集合
animSet.play(animator).with(animator1).with(animator2).with(animator3);
animSet.setDuration(3000);
animSet.start();
}else{//再點選一次實現縮回效果
AnimatorSet animSet1 = new AnimatorSet();//動畫集合
animSet1.play(fanimator).with(fanimator1).with(fanimator2).with(fanimator3);
animSet1.setDuration(3000);
animSet1.start();
}
}
});
}
public void doJump() {
jump.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(StartActivity.this,MainActivity.class);
startActivity(intent);
}
});
final Timer timer=new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
if (i==1){
Intent intent=new Intent(StartActivity.this,MainActivity.class);
startActivity(intent);
timer.cancel();
}
i--;
}
});
}
},0,1000);
}
}
private int i=10;
private Handler handler=new Handler();
private int a=0;
ImageView icon;
ImageView gt;
ImageView copy;
ImageView xg;
private ObjectAnimator animator;
private ObjectAnimator fanimator;
private ObjectAnimator animator1;
private ObjectAnimator fanimator1;
private ObjectAnimator animator2;
private ObjectAnimator fanimator2;
private ObjectAnimator animator3;
private ObjectAnimator fanimator3;
private Button jump;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
icon = (ImageView) findViewById(R.id.icon);
gt = (ImageView) findViewById(R.id.gt);
copy = (ImageView) findViewById(R.id.copy);
xg = (ImageView) findViewById(R.id.xg);
jump = findViewById(R.id.jump);
doJump();
//-----伸出時的動畫
animator = ObjectAnimator.ofFloat(icon, "rotation", 0f, 180f);
animator1 = ObjectAnimator.ofFloat(gt, "translationX", 0f,-80f);
animator2 = ObjectAnimator.ofFloat(copy, "translationX", 0f,-160f);
animator3 = ObjectAnimator.ofFloat(xg, "translationX", 0f,-240f);
//----縮回時的動畫
fanimator = ObjectAnimator.ofFloat(icon, "rotation", 0f, -180f);
fanimator1 = ObjectAnimator.ofFloat(gt, "translationX", -80f,0f);
fanimator2 = ObjectAnimator.ofFloat(copy, "translationX", -160f,0f);
fanimator3 = ObjectAnimator.ofFloat(xg, "translationX", -240f,0f);
//給伸出動畫設定監聽
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
icon.setImageResource(R.drawable.tamkero3);//動畫結束改變圖片
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
//給縮回動畫設定監聽
fanimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
icon.setImageResource(R.drawable.xlistview_arrow);//改變圖片
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
icon.setOnClickListener(new View.OnClickListener() {//圖片的點選事件
@Override
public void onClick(View view) {
a++;
if(a%2==1){//第一次點選是實現伸出效果
AnimatorSet animSet = new AnimatorSet();//動畫集合
animSet.play(animator).with(animator1).with(animator2).with(animator3);
animSet.setDuration(3000);
animSet.start();
}else{//再點選一次實現縮回效果
AnimatorSet animSet1 = new AnimatorSet();//動畫集合
animSet1.play(fanimator).with(fanimator1).with(fanimator2).with(fanimator3);
animSet1.setDuration(3000);
animSet1.start();
}
}
});
}
public void doJump() {
jump.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(StartActivity.this,MainActivity.class);
startActivity(intent);
}
});
final Timer timer=new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
if (i==1){
Intent intent=new Intent(StartActivity.this,MainActivity.class);
startActivity(intent);
timer.cancel();
}
i--;
}
});
}
},0,1000);
}
}