Action.java revision fa474a198019851ecc3824a1dfbac94cd1928efc
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 17package com.android.gallery3d.filtershow.category; 18 19import android.content.Context; 20import android.graphics.Bitmap; 21import android.graphics.BitmapFactory; 22import android.graphics.Canvas; 23import android.graphics.Matrix; 24import android.graphics.Paint; 25import android.graphics.Rect; 26import android.graphics.RectF; 27import android.widget.ArrayAdapter; 28import android.widget.ListAdapter; 29import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation; 30import com.android.gallery3d.filtershow.pipeline.RenderingRequest; 31import com.android.gallery3d.filtershow.pipeline.RenderingRequestCaller; 32import com.android.gallery3d.filtershow.filters.FilterRepresentation; 33import com.android.gallery3d.filtershow.imageshow.MasterImage; 34import com.android.gallery3d.filtershow.pipeline.ImagePreset; 35 36public class Action implements RenderingRequestCaller { 37 38 private static final String LOGTAG = "Action"; 39 private FilterRepresentation mRepresentation; 40 private String mName; 41 private Rect mImageFrame; 42 private Bitmap mImage; 43 private ArrayAdapter mAdapter; 44 public static final int FULL_VIEW = 0; 45 public static final int CROP_VIEW = 1; 46 private int mType = CROP_VIEW; 47 private Bitmap mPortraitImage; 48 private Bitmap mOverlayBitmap; 49 private Context mContext; 50 51 public Action(Context context, FilterRepresentation representation, int type) { 52 mContext = context; 53 setRepresentation(representation); 54 setType(type); 55 } 56 57 public Action(Context context, FilterRepresentation representation) { 58 this(context, representation, CROP_VIEW); 59 } 60 61 public FilterRepresentation getRepresentation() { 62 return mRepresentation; 63 } 64 65 public void setRepresentation(FilterRepresentation representation) { 66 mRepresentation = representation; 67 mName = representation.getName(); 68 } 69 70 public String getName() { 71 return mName; 72 } 73 74 public void setName(String name) { 75 mName = name; 76 } 77 78 public void setImageFrame(Rect imageFrame, int orientation) { 79 if (mImageFrame != null && mImageFrame.equals(imageFrame)) { 80 return; 81 } 82 Bitmap bitmap = MasterImage.getImage().getLargeThumbnailBitmap(); 83 if (bitmap != null) { 84 mImageFrame = imageFrame; 85 int w = mImageFrame.width(); 86 int h = mImageFrame.height(); 87 if (orientation == CategoryView.VERTICAL 88 && mType == CROP_VIEW) { 89 w /= 2; 90 } 91 Bitmap bitmapCrop = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 92 drawCenteredImage(bitmap, bitmapCrop, true); 93 94 postNewIconRenderRequest(bitmapCrop); 95 } 96 } 97 98 public Bitmap getImage() { 99 return mImage; 100 } 101 102 public void setImage(Bitmap image) { 103 mImage = image; 104 } 105 106 public void setAdapter(ArrayAdapter adapter) { 107 mAdapter = adapter; 108 } 109 110 public void setType(int type) { 111 mType = type; 112 } 113 114 private void postNewIconRenderRequest(Bitmap bitmap) { 115 if (bitmap != null && mRepresentation != null) { 116 ImagePreset preset = new ImagePreset(); 117 preset.addFilter(mRepresentation); 118 RenderingRequest.post(mContext, bitmap, 119 preset, RenderingRequest.ICON_RENDERING, this); 120 } 121 } 122 123 private void drawCenteredImage(Bitmap source, Bitmap destination, boolean scale) { 124 RectF image = new RectF(0, 0, source.getWidth(), source.getHeight()); 125 int border = 0; 126 if (!scale) { 127 border = destination.getWidth() - destination.getHeight(); 128 if (border < 0) { 129 border = 0; 130 } 131 } 132 RectF frame = new RectF(border, 0, 133 destination.getWidth() - border, 134 destination.getHeight()); 135 Matrix m = new Matrix(); 136 m.setRectToRect(frame, image, Matrix.ScaleToFit.CENTER); 137 image.set(frame); 138 m.mapRect(image); 139 m.setRectToRect(image, frame, Matrix.ScaleToFit.FILL); 140 Canvas canvas = new Canvas(destination); 141 canvas.drawBitmap(source, m, new Paint(Paint.FILTER_BITMAP_FLAG)); 142 } 143 144 @Override 145 public void available(RenderingRequest request) { 146 mImage = request.getBitmap(); 147 if (mImage == null) { 148 return; 149 } 150 if (mRepresentation.getOverlayId() != 0 && mOverlayBitmap == null) { 151 mOverlayBitmap = BitmapFactory.decodeResource( 152 mContext.getResources(), 153 mRepresentation.getOverlayId()); 154 } 155 if (mOverlayBitmap != null) { 156 if (getRepresentation().getFilterType() == FilterRepresentation.TYPE_BORDER) { 157 Canvas canvas = new Canvas(mImage); 158 canvas.drawBitmap(mOverlayBitmap, new Rect(0, 0, mOverlayBitmap.getWidth(), mOverlayBitmap.getHeight()), 159 new Rect(0, 0, mImage.getWidth(), mImage.getHeight()), new Paint()); 160 } else { 161 Canvas canvas = new Canvas(mImage); 162 canvas.drawARGB(128, 0, 0, 0); 163 drawCenteredImage(mOverlayBitmap, mImage, false); 164 } 165 } 166 if (mAdapter != null) { 167 mAdapter.notifyDataSetChanged(); 168 } 169 } 170 171 public void setPortraitImage(Bitmap portraitImage) { 172 mPortraitImage = portraitImage; 173 } 174 175 public Bitmap getPortraitImage() { 176 return mPortraitImage; 177 } 178 179 public Bitmap getOverlayBitmap() { 180 return mOverlayBitmap; 181 } 182 183 public void setOverlayBitmap(Bitmap overlayBitmap) { 184 mOverlayBitmap = overlayBitmap; 185 } 186} 187