2 * Copyright (C) 2013 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.gallery3d.filtershow.category;
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.BitmapFactory;
22 import android.graphics.Canvas;
23 import android.graphics.Matrix;
24 import android.graphics.Paint;
25 import android.graphics.Rect;
26 import android.graphics.RectF;
27 import com.android.gallery3d.filtershow.pipeline.RenderingRequest;
28 import com.android.gallery3d.filtershow.pipeline.RenderingRequestCaller;
29 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
30 import com.android.gallery3d.filtershow.imageshow.MasterImage;
31 import com.android.gallery3d.filtershow.pipeline.ImagePreset;
33 public class Action implements RenderingRequestCaller {
35 private static final String LOGTAG = "Action";
36 private FilterRepresentation mRepresentation;
38 private Rect mImageFrame;
39 private Bitmap mImage;
40 private CategoryAdapter mAdapter;
41 public static final int FULL_VIEW = 0;
42 public static final int CROP_VIEW = 1;
43 private int mType = CROP_VIEW;
44 private Bitmap mPortraitImage;
45 private Bitmap mOverlayBitmap;
46 private Context mContext;
48 public Action(Context context, FilterRepresentation representation, int type) {
50 setRepresentation(representation);
54 public Action(Context context, FilterRepresentation representation) {
55 this(context, representation, CROP_VIEW);
58 public FilterRepresentation getRepresentation() {
59 return mRepresentation;
62 public void setRepresentation(FilterRepresentation representation) {
63 mRepresentation = representation;
64 mName = representation.getName();
67 public String getName() {
71 public void setName(String name) {
75 public void setImageFrame(Rect imageFrame) {
76 if (mImageFrame != null && mImageFrame.equals(imageFrame)) {
79 Bitmap bitmap = MasterImage.getImage().getLargeThumbnailBitmap();
81 mImageFrame = imageFrame;
82 int w = mImageFrame.width();
83 int h = mImageFrame.height();
84 if (mType == CROP_VIEW) {
87 Bitmap bitmapCrop = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
88 drawCenteredImage(bitmap, bitmapCrop, true);
90 postNewIconRenderRequest(bitmapCrop);
94 public Bitmap getImage() {
98 public void setImage(Bitmap image) {
102 public void setAdapter(CategoryAdapter adapter) {
106 public void setType(int type) {
110 private void postNewIconRenderRequest(Bitmap bitmap) {
111 if (bitmap != null && mRepresentation != null) {
112 ImagePreset preset = new ImagePreset();
113 preset.addFilter(mRepresentation);
114 RenderingRequest.post(bitmap,
115 preset, RenderingRequest.ICON_RENDERING, this);
119 private void drawCenteredImage(Bitmap source, Bitmap destination, boolean scale) {
120 RectF image = new RectF(0, 0, source.getWidth(), source.getHeight());
123 border = destination.getWidth() - destination.getHeight();
128 RectF frame = new RectF(border, 0,
129 destination.getWidth() - border,
130 destination.getHeight());
131 Matrix m = new Matrix();
132 m.setRectToRect(frame, image, Matrix.ScaleToFit.CENTER);
135 m.setRectToRect(image, frame, Matrix.ScaleToFit.FILL);
136 Canvas canvas = new Canvas(destination);
137 canvas.drawBitmap(source, m, new Paint(Paint.FILTER_BITMAP_FLAG));
141 public void available(RenderingRequest request) {
142 mImage = request.getBitmap();
143 if (mImage == null) {
146 if (mRepresentation.getOverlayId() != 0 && mOverlayBitmap == null) {
147 mOverlayBitmap = BitmapFactory.decodeResource(
148 mContext.getResources(),
149 mRepresentation.getOverlayId());
151 if (mOverlayBitmap != null) {
152 if (getRepresentation().getFilterType() == FilterRepresentation.TYPE_BORDER) {
153 Canvas canvas = new Canvas(mImage);
154 canvas.drawBitmap(mOverlayBitmap, new Rect(0, 0, mOverlayBitmap.getWidth(), mOverlayBitmap.getHeight()),
155 new Rect(0, 0, mImage.getWidth(), mImage.getHeight()), new Paint());
157 Canvas canvas = new Canvas(mImage);
158 canvas.drawARGB(128, 0, 0, 0);
159 drawCenteredImage(mOverlayBitmap, mImage, false);
162 if (mAdapter != null) {
163 mAdapter.notifyDataSetChanged();
167 public void setPortraitImage(Bitmap portraitImage) {
168 mPortraitImage = portraitImage;
171 public Bitmap getPortraitImage() {
172 return mPortraitImage;
175 public Bitmap getOverlayBitmap() {
176 return mOverlayBitmap;
179 public void setOverlayBitmap(Bitmap overlayBitmap) {
180 mOverlayBitmap = overlayBitmap;