1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/system/user/rounded_image_view.h"
6 #include "skia/ext/image_operations.h"
7 #include "third_party/skia/include/core/SkPaint.h"
8 #include "third_party/skia/include/core/SkPath.h"
9 #include "ui/gfx/canvas.h"
10 #include "ui/gfx/image/image_skia_operations.h"
11 #include "ui/gfx/skia_util.h"
16 RoundedImageView::RoundedImageView(int corner_radius
, bool active_user
)
17 : active_user_(active_user
) {
18 for (int i
= 0; i
< 4; ++i
)
19 corner_radius_
[i
] = corner_radius
;
22 RoundedImageView::~RoundedImageView() {}
24 void RoundedImageView::SetImage(const gfx::ImageSkia
& img
,
25 const gfx::Size
& size
) {
29 // Try to get the best image quality for the avatar.
30 resized_
= gfx::ImageSkiaOperations::CreateResizedImage(
31 image_
, skia::ImageOperations::RESIZE_BEST
, size
);
32 if (GetWidget() && visible()) {
33 PreferredSizeChanged();
38 void RoundedImageView::SetCornerRadii(int top_left
,
42 corner_radius_
[0] = top_left
;
43 corner_radius_
[1] = top_right
;
44 corner_radius_
[2] = bottom_right
;
45 corner_radius_
[3] = bottom_left
;
48 gfx::Size
RoundedImageView::GetPreferredSize() const {
49 return gfx::Size(image_size_
.width() + GetInsets().width(),
50 image_size_
.height() + GetInsets().height());
53 void RoundedImageView::OnPaint(gfx::Canvas
* canvas
) {
54 View::OnPaint(canvas
);
55 gfx::Rect
image_bounds(size());
56 image_bounds
.ClampToCenteredSize(GetPreferredSize());
57 image_bounds
.Inset(GetInsets());
58 const SkScalar kRadius
[8] = {
59 SkIntToScalar(corner_radius_
[0]),
60 SkIntToScalar(corner_radius_
[0]),
61 SkIntToScalar(corner_radius_
[1]),
62 SkIntToScalar(corner_radius_
[1]),
63 SkIntToScalar(corner_radius_
[2]),
64 SkIntToScalar(corner_radius_
[2]),
65 SkIntToScalar(corner_radius_
[3]),
66 SkIntToScalar(corner_radius_
[3])
69 path
.addRoundRect(gfx::RectToSkRect(image_bounds
), kRadius
);
71 paint
.setAntiAlias(true);
72 paint
.setXfermodeMode(active_user_
? SkXfermode::kSrcOver_Mode
73 : SkXfermode::kLuminosity_Mode
);
74 canvas
->DrawImageInPath(
75 resized_
, image_bounds
.x(), image_bounds
.y(), path
, paint
);