You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android ActionBar添加3个ImageView:2个固定1个做缩放动画求助

解决ActionBar中ImageView固定显示与缩放动画问题

嘿,我来帮你搞定这个需求!要在ActionBar里实现两个固定ImageView加一个带缩放动画的ImageView,咱们一步步来:

1. 先创建自定义ActionBar布局

首先在res/layout下新建custom_action_bar.xml,布局里放三个ImageView,给需要动画的那个单独设置id:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:paddingHorizontal="16dp"
    android:orientation="horizontal">

    <!-- 第一个固定显示的ImageView -->
    <ImageView
        android:id="@+id/iv_fixed1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_fixed1"
        android:layout_marginEnd="24dp"/>

    <!-- 第二个固定显示的ImageView -->
    <ImageView
        android:id="@+id/iv_fixed2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_fixed2"
        android:layout_marginEnd="24dp"/>

    <!-- 需要缩放动画的ImageView -->
    <ImageView
        android:id="@+id/iv_animated"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_animated"/>

</LinearLayout>

2. 在Activity中配置自定义ActionBar

完善你之前的代码,加载自定义布局并隐藏默认标题:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 初始化ActionBar
    android.support.v7.app.ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        // 设置背景色
        actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorBleu)));
        // 允许自定义视图
        actionBar.setDisplayShowCustomEnabled(true);
        // 隐藏默认标题和图标
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);

        // 加载自定义布局
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View customView = inflater.inflate(R.layout.custom_action_bar, null);
        actionBar.setCustomView(customView);

        // 获取需要动画的ImageView并启动动画
        ImageView animatedIv = customView.findViewById(R.id.iv_animated);
        startScaleAnimation(animatedIv);
    }
}

3. 创建缩放动画文件

在res/anim下新建scale_animation.xml,定义循环的缩放动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:fromXScale="0.8"
        android:toXScale="1.2"
        android:fromYScale="0.8"
        android:toYScale="1.2"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="1000"
        android:repeatMode="reverse"
        android:repeatCount="infinite"/>
</set>
  • pivotX/Y设置为50%,保证以ImageView中心为缩放原点
  • repeatMode="reverse"让动画缩放后再缩回去,循环往复
  • repeatCount="infinite"设置无限循环

4. 实现动画启动方法

在Activity里添加启动动画的方法:

private void startScaleAnimation(ImageView imageView) {
    Animation scaleAnim = AnimationUtils.loadAnimation(this, R.anim.scale_animation);
    imageView.startAnimation(scaleAnim);
}

这样就能实现你要的效果啦:ActionBar背景是你设置的蓝色,两个ImageView固定显示,第三个持续执行缩放动画。如果还有问题,可以检查一下drawable资源是否正确,或者动画的参数是否符合你的预期~

内容的提问来源于stack exchange,提问作者AZER

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:00:45