【问题标题】:How to make an image fit a custom 256dp Big Picture Notification in Android?如何使图像适合 Android 中的自定义 256dp 大图片通知?
【发布时间】:2013-07-28 14:20:18
【问题描述】:

我正在尝试将两种从 Web 服务获得的图像放入大图片样式的通知中(中心和内部)。一种是 97px*150px 大小,另一种是 300px*100px。

当我检查图像必须适合 256dp 的最大高度以适合 Jelly Bean 的大图片通知可视区域时,所以我希望我可以在 ImageView(fitXT、centerInside 等)上调用可用的矩阵比例,但我很惊讶没有可用于配置的缩放方法行为,并且位图总是居中裁剪,顺便说一句,这是我选择默认的最差的默认矩阵行为,因为图像总是以某种方式被剪切。我认为,默认情况下的中心内部行为会更好。

所以,这为我留出了空间:

  • 使用某种像素到 dp 方法对图像进行预缩放。这种方法还必须尊重纵横比。之后将新图片用于 Notification Builder,它(我希望)会尊重 iimage,因为不会发生越界行为。
  • 创建我自己的 RemoteView 以使用 ImageView 并获得对比例矩阵行为的访问权限。
  • 向 Google 提交新功能请求 :)
  • 向 Google 提交错误请求 :)

您认为或知道哪个选项最好? 我还有其他选择吗?

编辑:我已经测试了几种方法来成功地对图像进行矩阵缩放,但是默认情况下,中心裁剪总是在那里破坏和剪切我的图像,使其无法使用。

我现在试试 RemoteView 方法。

我还为此提交了问题 58318,试图让未来尝试完成此任务的开发人员的生活更轻松:https://code.google.com/p/android/issues/detail?id=58318

【问题讨论】:

    标签: android scale android-imageview android-notifications


    【解决方案1】:

    唯一的解决方案是创建我自己的 RemoteView,如下所示:

    首先,自定义布局:

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

    以及 RemoteView 实现:

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

    其中 myNotification 是您的通知生成器,而 randomInt 是我自己的无重叠通知实现(检查您是否需要替换通知或生成多个通知)。

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

    就是这样。也许有点脏,但它有效。我会把这个错误留给谷歌,如果我收到一些回复,我会更新这个。

    【讨论】:

      猜你喜欢
      • 2015-09-24
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多