我想为应用工具栏中的铃铛图标添加动画

sak*_*dev 2 java android android-studio material-design

我想将我的 android 应用程序链接到网页。每当网页的内容更改时,应触发触发器并为我的 android 应用程序工具栏中的铃铛图标创建动画,以便用户能够知道网页中添加了新内容。请帮忙!!

Pra*_*pat 5

尝试这个。

创建动画:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="80"
    android:fromDegrees="-10"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="5"
    android:repeatMode="reverse"
    android:toDegrees="10" />
Run Code Online (Sandbox Code Playgroud)

创建菜单

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/item_notification"
        app:actionLayout="@layout/your_custom_layout"
        android:title="Notification"
        app:showAsAction="always" />
</menu>
Run Code Online (Sandbox Code Playgroud)

为菜单创建自定义布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:padding="16dp"
        android:id="@+id/ivNotification"
        android:src="@drawable/ic_action_notification"
        android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在活动中添加动画代码

public class Testactivity extends AppCompatActivity{
    ImageView ivNotification;
    Animation shake;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button btn = new Button(this);
        btn.setText("Shake");
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shake();
            }
        });
        shake = AnimationUtils.loadAnimation(this, R.anim.shake);
        setContentView(btn);
    }

    private void shake() {
        ivNotification.startAnimation(shake);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_test, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        ivNotification = (ImageView) menu.findItem(R.id.item_notification).getActionView().findViewById(R.id.ivNotification);
        return super.onPrepareOptionsMenu(menu);
    }

}
Run Code Online (Sandbox Code Playgroud)