OSDN Git Service

Move classes to pipeline package
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / category / Action.java
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.gallery3d.filtershow.category;
18
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;
32
33 public class Action implements RenderingRequestCaller {
34
35     private static final String LOGTAG = "Action";
36     private FilterRepresentation mRepresentation;
37     private String mName;
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;
47
48     public Action(Context context, FilterRepresentation representation, int type) {
49         mContext = context;
50         setRepresentation(representation);
51         setType(type);
52     }
53
54     public Action(Context context, FilterRepresentation representation) {
55         this(context, representation, CROP_VIEW);
56     }
57
58     public FilterRepresentation getRepresentation() {
59         return mRepresentation;
60     }
61
62     public void setRepresentation(FilterRepresentation representation) {
63         mRepresentation = representation;
64         mName = representation.getName();
65     }
66
67     public String getName() {
68         return mName;
69     }
70
71     public void setName(String name) {
72         mName = name;
73     }
74
75     public void setImageFrame(Rect imageFrame) {
76         if (mImageFrame != null && mImageFrame.equals(imageFrame)) {
77             return;
78         }
79         Bitmap bitmap = MasterImage.getImage().getLargeThumbnailBitmap();
80         if (bitmap != null) {
81             mImageFrame = imageFrame;
82             int w = mImageFrame.width();
83             int h = mImageFrame.height();
84             if (mType == CROP_VIEW) {
85                 w /= 2;
86             }
87             Bitmap bitmapCrop = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
88             drawCenteredImage(bitmap, bitmapCrop, true);
89
90             postNewIconRenderRequest(bitmapCrop);
91         }
92     }
93
94     public Bitmap getImage() {
95         return mImage;
96     }
97
98     public void setImage(Bitmap image) {
99         mImage = image;
100     }
101
102     public void setAdapter(CategoryAdapter adapter) {
103         mAdapter = adapter;
104     }
105
106     public void setType(int type) {
107         mType = type;
108     }
109
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);
116         }
117     }
118
119     private void drawCenteredImage(Bitmap source, Bitmap destination, boolean scale) {
120         RectF image = new RectF(0, 0, source.getWidth(), source.getHeight());
121         int border = 0;
122         if (!scale) {
123             border = destination.getWidth() - destination.getHeight();
124             if (border < 0) {
125                 border = 0;
126             }
127         }
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);
133         image.set(frame);
134         m.mapRect(image);
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));
138     }
139
140     @Override
141     public void available(RenderingRequest request) {
142         mImage = request.getBitmap();
143         if (mImage == null) {
144             return;
145         }
146         if (mRepresentation.getOverlayId() != 0 && mOverlayBitmap == null) {
147             mOverlayBitmap = BitmapFactory.decodeResource(
148                     mContext.getResources(),
149                     mRepresentation.getOverlayId());
150         }
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());
156             } else {
157                 Canvas canvas = new Canvas(mImage);
158                 canvas.drawARGB(128, 0, 0, 0);
159                 drawCenteredImage(mOverlayBitmap, mImage, false);
160             }
161         }
162         if (mAdapter != null) {
163             mAdapter.notifyDataSetChanged();
164         }
165     }
166
167     public void setPortraitImage(Bitmap portraitImage) {
168         mPortraitImage = portraitImage;
169     }
170
171     public Bitmap getPortraitImage() {
172         return mPortraitImage;
173     }
174
175     public Bitmap getOverlayBitmap() {
176         return mOverlayBitmap;
177     }
178
179     public void setOverlayBitmap(Bitmap overlayBitmap) {
180         mOverlayBitmap = overlayBitmap;
181     }
182 }