private void updateButtonState(SendButtonState buttonState) { if (mButtonState != buttonState) { mButtonState = buttonState; float translation = Units.dpToPx(mContext, 14) / 3; float barRotation1 = mButtonBar1.getRotation(); float barTranslation1 = mButtonBar1.getTranslationY(); float barRotation2 = mButtonBar2.getRotation(); float barTranslation2 = mButtonBar2.getTranslationY(); float barRotationTarget1 = mButtonState == SendButtonState.ATTACH ? 0 : 225; float barTranslationTarget1 = mButtonState == SendButtonState.SEND ? -translation : 0; float barRotationTarget2 = mButtonState == SendButtonState.ATTACH ? 90 : 135; float barTranslationTarget2 = mButtonState == SendButtonState.SEND ? translation : 0; ObjectAnimator.ofFloat(mButtonBar1, "rotation", barRotation1, barRotationTarget1) .setDuration(ANIMATION_DURATION) .start(); ObjectAnimator.ofFloat(mButtonBar2, "rotation", barRotation2, barRotationTarget2) .setDuration(ANIMATION_DURATION) .start(); ObjectAnimator.ofFloat(mButtonBar1, "translationY", barTranslation1, barTranslationTarget1) .setDuration(ANIMATION_DURATION) .start(); ObjectAnimator.ofFloat(mButtonBar2, "translationY", barTranslation2, barTranslationTarget2) .setDuration(ANIMATION_DURATION) .start(); } }
// test to check if the image view'state is same across screen rotation public void testHotWaterTapImageStateOnScreenOrientationChanged() { float angle_before = 0; float angle_after = 0; imageView1.setPressed(true); angle_before = imageView1.getRotation(); mainActivity.finish(); mainActivity = getActivity(); if (imageView1 != null) { angle_after = imageView1.getRotation(); } assertEquals("HOtTap ImageView is restored after rotation", angle_before, angle_after); }
/** 利用属性动画旋转箭头 */ private void rotateArrow(ImageView imageView) { if (imageView == refreshArrowImg && pullDownY == 0) { // 下拉箭头恢复最初状态 ObjectAnimator.ofFloat(imageView, "rotation", 0).setDuration(150).start(); } else if (imageView == loadArrowImg && pullUpY == 0) { // 上拉箭头恢复最初状态 ObjectAnimator.ofFloat(imageView, "rotation", 180).setDuration(150).start(); } else { // 强行指向竖直方向 if (imageView.getRotation() % 180 != 0) { if ((int) (imageView.getRotation() / 180) % 2 == 0) { ObjectAnimator.ofFloat(imageView, "rotation", 180).setDuration(150).start(); } else { ObjectAnimator.ofFloat(imageView, "rotation", 0).setDuration(150).start(); } } else { ObjectAnimator.ofFloat(imageView, "rotation", imageView.getRotation() + 180) .setDuration(150) .start(); } } }
@Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: if (!dragging) { reloadIcon.animate().alpha(0).rotation(0).setDuration(0); originY = view.getTop(); startY = motionEvent.getRawY(); offsetY = startY - originY; dragging = true; Log.i("Coffee", "Started dragging"); return true; } break; case MotionEvent.ACTION_MOVE: if (dragging) { float dy = motionEvent.getRawY(); // if(dy - startY > 0) { float alpha = (float) (1 - 1000 / Math.pow(dy - startY, 2)); view.animate().y(dy - offsetY).setDuration(0); reloadIcon.animate().alpha(alpha).rotation(dy).setDuration(0); return true; // } } break; case MotionEvent.ACTION_UP: if (dragging) { dragging = false; float angle = reloadIcon.getRotation(); view.animate().y(originY).setDuration(300); reloadIcon.animate().alpha(0).rotation(angle + 180).setDuration(300); Log.i("Coffee", "Stopped dragging"); if (Math.abs(motionEvent.getRawY() - startY) > 200) { Log.i("Coffee", "Attempted to update text"); statusField.animate().y(statusFieldPosition + 1000).setDuration(0); new UpdateStatusTask().execute(); } return true; } } return false; }
public void startAnimation(View view) { float dest = 0; final ImageView aniView = (ImageView) findViewById(R.id.imageView1); switch (view.getId()) { case R.id.Button01: dest = 360; if (aniView.getRotation() == 360) { dest = 0; } // ObjectAnimator animation1 = ObjectAnimator.ofFloat(aniView, // "rotation", dest); // animation1.setDuration(2000); // animation1.start(); aniView .animate() .rotation(dest) .setDuration(1000) .scaleX(2) .scaleY(2) .withEndAction( new Runnable() { @Override public void run() { // aniView .animate() .rotationXBy(100) .rotation(Math.abs(360 - aniView.getRotation())) .scaleX(0.4F) .scaleY(0.4F) .setDuration(1000); } }); // Show how to load an animation from XML // Animation animation1 = AnimationUtils.loadAnimation(this, // R.anim.myanimation); // animation1.setAnimationListener(this); // animatedView1.startAnimation(animation1); break; case R.id.Button02: // Shows how to define a animation via code // Also use an Interpolator (BounceInterpolator) Paint paint = new Paint(); TextView aniTextView = (TextView) findViewById(R.id.textView1); float measureTextCenter = paint.measureText(aniTextView.getText().toString()); dest = 0 - measureTextCenter; if (aniTextView.getX() < 0) { dest = 0; } ObjectAnimator animation2 = ObjectAnimator.ofFloat(aniTextView, "x", dest); animation2.setDuration(2000); animation2.start(); break; case R.id.Button03: // Demonstrate fading and adding an AnimationListener dest = 1; if (aniView.getAlpha() > 0) { dest = 0; } ObjectAnimator animation3 = ObjectAnimator.ofFloat(aniView, "alpha", dest); animation3.setDuration(2000); animation3.start(); break; case R.id.Button04: ObjectAnimator fadeOut = ObjectAnimator.ofFloat(aniView, "alpha", 0f); fadeOut.setDuration(2000); ObjectAnimator mover = ObjectAnimator.ofFloat(aniView, "translationX", -500f, 0f); mover.setDuration(2000); ObjectAnimator fadeIn = ObjectAnimator.ofFloat(aniView, "alpha", 0f, 1f); fadeIn.setDuration(2000); AnimatorSet animatorSet = new AnimatorSet(); animatorSet.play(mover).with(fadeIn).after(fadeOut); animatorSet.start(); break; default: break; } }