How to make an image fit a custom 256dp Big Picture Notification in Android?

可紊 提交于 2019-12-06 00:24:12

The only solution is to create my own RemoteView as follows:

First, a custom layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/status_bar_latest_event_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:drawable/list_selector_background">

<ImageView
        android:id="@+id/big_icon"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="15dp"
        android:scaleType="fitCenter"
        />

<TextView
        android:id="@+id/title"
        android:text="sometitlestringfromresources"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_alignBottom="@+id/big_icon"
        android:layout_toRightOf="@+id/big_icon"
        android:layout_marginLeft="15dp"/>


<ImageView
        android:id="@+id/big_picture"
        android:layout_width="match_parent"
        android:layout_height="192dp"
        android:layout_marginTop="64dp"
        android:scaleType="fitCenter" //This scale will show your picture without crop
        />

And the RemoteView implementation:

  if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            RemoteViews views;
            views = new RemoteViews(getPackageName(), R.layout.custom_notification);
            views.setImageViewBitmap(R.id.big_picture, bitmap);
            views.setImageViewBitmap(R.id.big_icon, BitmapFactory.decodeResource(getResources(), R.drawable.your_24x24dp_brand_icon));
            views.setTextViewText(R.id.title, message);
            myNotification.bigContentView = views;
        }

        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);

                notificationManager.notify(randomInt, myNotification);

Where myNotification is your notification builder and randomInt is my own implementation to no overlap notifications (check if this is not your need to replace notifications or generate several).

  Random randomGenerator = new Random();
  int randomInt = randomGenerator.nextInt(100);

And that's it. Maybe is a little dirty but it works. I'll leave the bug open to Google and if I receive some response i'll update this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!