Skip to content

Instantly share code, notes, and snippets.

@myamamic
Last active November 26, 2015 16:08
Show Gist options
  • Save myamamic/72064e54269c5084c8c0 to your computer and use it in GitHub Desktop.
Save myamamic/72064e54269c5084c8c0 to your computer and use it in GitHub Desktop.
[android] パルスアニメーション
// アニメーション
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<alpha
android:duration="500"
android:repeatCount="-1"
android:repeatMode="restart"
android:fromAlpha="0.8"
android:toAlpha="0.0" />
<scale
android:duration="500"
android:repeatCount="-1"
android:repeatMode="restart"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="2.0"
android:toYScale="2.0" />
</set>
// パルスイメージ
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="48dp"
android:height="48dp" />
<solid android:color="@color/colorAccent" />
</shape>
// レイアウト
<FrameLayout
android:layout_width="96dp"
android:layout_height="96dp"
app:layout_anchor="@id/main"
app:layout_anchorGravity="bottom|right|end">
<ImageView
android:id="@+id/image_anim_fab"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:background="@drawable/image_pulse" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_measure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_fab_measure"
app:fabSize="normal" />
</FrameLayout>
// コード
private Animation mPulseAnimation;
private ImageView mPulseImage;
private boolean mPulseStarted = false;
mPulseImage = (ImageView) findViewById(R.id.image_anim_fab);
mPulseAnimation = AnimationUtils.loadAnimation(
getActivity().getApplicationContext(), R.anim.pulse);
private void togglePulseAnimation() {
if (!mPulseStarted) {
startPulseAnimation();
} else {
stopPulseAnimation();
}
}
private void startPulseIfNeeded() {
MainActivity activity = (MainActivity) getActivity();
if (activity.isServiceStarted()) {
startPulseAnimation();
}
}
private void startPulseAnimation() {
if (mPulseImage != null && mPulseAnimation != null) {
mPulseImage.startAnimation(mPulseAnimation);
mPulseStarted = true;
}
}
private void stopPulseAnimation() {
if (mPulseImage != null && mPulseAnimation != null) {
mPulseImage.clearAnimation();
mPulseStarted = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment