android 中浮动操作按钮的简单动画?
Simple animation for floating action button in android?
我需要一个非常简单的解释,说明如何在单击时为 'add to cart' FloatingActionButton
设置动画。我想要的只是流畅的 'left-right' 或 'down-up' 运动动画。
请看下面的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1"
android:background="@color/cardview_light_background">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:layout_width="54dp"
android:layout_height="54dp"
android:layout_gravity="bottom|right"
android:src="@mipmap/ic_add_shopping_cart_black_24dp"
android:layout_marginBottom="40dp"
android:layout_marginRight="30dp"
app:backgroundTint="@android:color/holo_blue_light" />
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
提前致谢!
试试 AndroidViewAnimations 库。这个库提供了简单的方法来动画你的视图。有很多效果。例如
YoYo.with(Techniques.Tada)
.duration(700)
.playOn(findViewById(R.id.edit_area));
floatingActionButton.animate().xBy(10).yBy(10);
在其onClick
!坐标随便给!
或
shake.xml > res/anim/shake.xml(不需要额外的库你可以自定义这个xml)
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:duration="150"
android:fromXDelta="-10%"
android:repeatCount="5"
android:repeatMode="reverse"
android:toXDelta="10%"/>
</set>
..
final FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation anim = android.view.animation.AnimationUtils.loadAnimation(floatingActionButton.getContext(), R.anim.shake);
anim.setDuration(200L);
floatingActionButton.startAnimation(anim);
}
});
看看这个:
https://github.com/Scalified/fab
它有从左到右或从上到下的移动动画
在你的 gradle:
dependencies {
compile 'com.scalified:fab:1.1.3'
}
布局中:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.scalified.fab.ActionButton
android:id="@+id/action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="@dimen/fab_margin"
android:layout_marginBottom="@dimen/fab_margin"
/>
</RelativeLayout>
现搬家使用:
// And then find it within the content view:
ActionButton actionButton = (ActionButton) findViewById(R.id.action_button);
// Initialize the moving distance
int distance = 100.0f // in density-independent pixels
// Move ActionButton left
actionButton.moveLeft(distance);
// Move ActionButton up
actionButton.moveUp(distance);
// Move ActionButton right
actionButton.moveRight(distance);
// Move ActionButton down
actionButton.moveDown(distance);
我需要一个非常简单的解释,说明如何在单击时为 'add to cart' FloatingActionButton
设置动画。我想要的只是流畅的 'left-right' 或 'down-up' 运动动画。
请看下面的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1"
android:background="@color/cardview_light_background">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:layout_width="54dp"
android:layout_height="54dp"
android:layout_gravity="bottom|right"
android:src="@mipmap/ic_add_shopping_cart_black_24dp"
android:layout_marginBottom="40dp"
android:layout_marginRight="30dp"
app:backgroundTint="@android:color/holo_blue_light" />
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
提前致谢!
试试 AndroidViewAnimations 库。这个库提供了简单的方法来动画你的视图。有很多效果。例如
YoYo.with(Techniques.Tada)
.duration(700)
.playOn(findViewById(R.id.edit_area));
floatingActionButton.animate().xBy(10).yBy(10);
在其onClick
!坐标随便给!
或
shake.xml > res/anim/shake.xml(不需要额外的库你可以自定义这个xml)
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:duration="150"
android:fromXDelta="-10%"
android:repeatCount="5"
android:repeatMode="reverse"
android:toXDelta="10%"/>
</set>
..
final FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation anim = android.view.animation.AnimationUtils.loadAnimation(floatingActionButton.getContext(), R.anim.shake);
anim.setDuration(200L);
floatingActionButton.startAnimation(anim);
}
});
看看这个:
https://github.com/Scalified/fab
它有从左到右或从上到下的移动动画
在你的 gradle:
dependencies {
compile 'com.scalified:fab:1.1.3'
}
布局中:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.scalified.fab.ActionButton
android:id="@+id/action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="@dimen/fab_margin"
android:layout_marginBottom="@dimen/fab_margin"
/>
</RelativeLayout>
现搬家使用:
// And then find it within the content view:
ActionButton actionButton = (ActionButton) findViewById(R.id.action_button);
// Initialize the moving distance
int distance = 100.0f // in density-independent pixels
// Move ActionButton left
actionButton.moveLeft(distance);
// Move ActionButton up
actionButton.moveUp(distance);
// Move ActionButton right
actionButton.moveRight(distance);
// Move ActionButton down
actionButton.moveDown(distance);