简体   繁体   English

我想在我的应用程序工具栏中向铃声图标添加动画

[英]I want to add animation to bell icon in my app toolbar

I want to link my android app to the webpage. 我想将我的Android应用程序链接到该网页。 whenever the content of the web page changes a trigger should be fired and create animation to the bell icon in toolbar of my android app, so that user is able to know there is something new added to the webpage. 每当网页内容更改时,都应触发一个触发器,并在我的Android应用程序工具栏中的铃铛图标中创建动画,以便用户能够知道该网页中添加了一些新内容。 please help!! 请帮忙!!

Try this. 尝试这个。

Create animation: 创建动画:

<?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" />

Create menu 创建菜单

<?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>

Create custom layout for menu 创建菜单的自定义布局

<?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>

Add animation code in activity 在活动中添加动画代码

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);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM