Facebook圆形图像显示为椭圆形而非矩形?

时间:2016-10-25 08:51:34

标签: android facebook bitmap android-notifications

我想从Facebook API抓取图像并将其显示在我的通知中,但照片似乎总是呈椭圆形,我似乎无法弄清楚我做错了什么。这是我的尝试:

private class NotificationPicture extends AsyncTask<String, Void, Bitmap> {

    Context context;
    String userId;
    String postId;
    String name;
    String notificationBody;
    String from;

    public NotificationPicture(Context context) {
        super();
        this.context = context;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        userId = params[0];
        postId = params[1];
        name = params[2];
        notificationBody = params[3];
        from = params[4];
        try {
            URL url = new URL("https://graph.facebook.com/" + userId + "/picture?type=large");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream in = connection.getInputStream();
            Bitmap  bitmap = BitmapFactory.decodeStream(in);
            final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                    bitmap.getHeight(), Bitmap.Config.ARGB_8888);
            final Canvas canvas = new Canvas(output);

            final int color = Color.RED;
            final Paint paint = new Paint();
            final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            final RectF rectF = new RectF(rect);

            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawOval(rectF, paint);

            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);

            bitmap.recycle();

            return output;
        } catch (IOException e) {
            FirebaseCrash.report(e);
            return null;
        }
    }

    @Override
    protected void onPostExecute(Bitmap result) {

        super.onPostExecute(result);
        try {
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(notificationBody)
                    .setTicker(from + " has responded!")
                    .setAutoCancel(true)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody))
                    .setSmallIcon(R.drawable.ic_tabs_notification_2)
                    .setLargeIcon(result);
            Intent resultIntent = new Intent(context, CommentsActivity.class);
            setupPostDetails(notificationBuilder, resultIntent, postId, userId, name, context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

以下是最终的结果:

enter image description here

但如果可能的话,我希望这看起来更像一个圆圈。任何帮助,将不胜感激。谢谢!

0 个答案:

没有答案