如何在Android中使图像适合自定义256dp大图片通知? [英] How to make an image fit a custom 256dp Big Picture Notification in Android?

查看:136
本文介绍了如何在Android中使图像适合自定义256dp大图片通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将从Web服务获得的2种图像(中间和内部)放入Big Picture样式的Notification中.一种是97px * 150px,另一种是300px * 100px.

I'm trying to fit (center and inside) 2 kind of images obtained from web services in a Big Picture styled Notification. One kind is 97px*150px sized and the other 300px*100px.

当我检查图像必须适合最大256dp的最大高度以适合果冻豆的Big Picture通知可视区域时,因此我期望可以在ImageView上调用矩阵比例尺(fitXT,centerInside等),但我感到惊讶没有缩放方法行为可配置,并且位图总是居中裁剪,这是最糟糕的默认矩阵行为,我将默认设置为默认行为,因为图像总是以某种方式被裁切.我认为以默认行为为中心会更好.

As I checked the image must fit 256dp tall max height to fit on Jelly Bean's Big Picture notification visual area, so I expected that I can call the matrix scales availables on ImageView (fitXT, centerInside, etc) but i'm surprised that no scaling method behavior is available to configure and the bitmap is always center-cropped wich by the way is worst default matrix behavior I would selected for default because images always are cutted in some way. A center inside behavior for default it would be better, I think.

因此,这为我留出了这样的可能性:

So, this leaves me room for this posibilities:

  • 使用某种像素到dp方法对图像进行预缩放.该方法还必须考虑长宽比.在将新图片用于Notification Builder之后,(我希望)这将尊重iomages,因为不会发生超出范围的行为.
  • 创建我自己的RemoteView以使用ImageView并获得比例矩阵行为的访问权限.
  • 向Google提出新功能请求:)
  • 向Google提交错误请求:)

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

Which option do you think or know as the best? Is there any other option that am I could be missing?

我已经测试了几种成功对图像进行矩阵缩放的方法,但是默认情况下,中心裁切始终会出现,以打断并剪切我的图像,使其无法使用.

我现在将尝试使用RemoteView方法.

我还为此提交了58318问题,以使尝试完成此任务的未来开发人员更加轻松:

I also filed the issue 58318 for this, trying to make life easier for future devs trying to accomplish this task: https://code.google.com/p/android/issues/detail?id=58318

推荐答案

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

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

以及RemoteView实现:

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

在其中myNotification是您的通知生成器,而randomInt是我自己的实现,没有重复的通知(请检查是否不需要替换通知或生成多个通知).

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

就是这样.也许有点脏,但是可以用.我将向Google开放该错误,如果收到一些答复,我将对此进行更新.

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.

这篇关于如何在Android中使图像适合自定义256dp大图片通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆