Skip to content

Instantly share code, notes, and snippets.

@cheankumio
Created March 7, 2018 09:00
Show Gist options
  • Save cheankumio/50c4da2f083c3676e26d5709f1c87986 to your computer and use it in GitHub Desktop.
Save cheankumio/50c4da2f083c3676e26d5709f1c87986 to your computer and use it in GitHub Desktop.
public class MainActivity extends AppCompatActivity {
View layout2;
private float screenWidth,screenHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 隱藏ActionBar
getSupportActionBar().hide();
// 取得手機螢幕尺寸
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.heightPixels;
screenHeight = metrics.widthPixels;
layout2 = findViewById(R.id.layout2);
}
//enter是設定在activity_layout.xml內Button的onClick欄位填入的名稱
public void leftbutton(View v){
AnimatorSet ans = new AnimatorSet();
ObjectAnimator[] oan = new ObjectAnimator[3];
// 移動Y軸,由原始位置移動screenWidth*0.45個位置
oan[0] = ObjectAnimator.ofFloat(layout2, "translationX", 0,-(screenWidth*0.45f));
// 縮放0.87倍
oan[1] = ObjectAnimator.ofFloat(layout2, "scaleX", 1f,0.87f);
oan[2] = ObjectAnimator.ofFloat(layout2, "scaleY", 1f,0.87f);
// 動畫波形
ans.setInterpolator(new AnticipateOvershootInterpolator());
ans.playTogether(oan);
ans.setDuration(450);
ans.start();
}
public void rightbutton(View v){
AnimatorSet ans = new AnimatorSet();
ObjectAnimator[] oan = new ObjectAnimator[3];
oan[0] = ObjectAnimator.ofFloat(layout2, "translationX", -(screenWidth*0.45f),0);
oan[1] = ObjectAnimator.ofFloat(layout2, "scaleX", 0.87f,1f);
oan[2] = ObjectAnimator.ofFloat(layout2, "scaleY", 0.87f,1f);
ans.setInterpolator(new AnticipateOvershootInterpolator());
ans.playTogether(oan);
ans.setDuration(450);
ans.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment