/*
* Copyright (C) 2012 CyberAgent
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jp.co.cyberagent.android.gpuimage;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import java.io.File;
public class GPUImageView extends GLSurfaceView {
private GPUImage mGPUImage;
private GPUImageFilter mFilter;
private float mRatio = 0.0f;
public GPUImageView(Context context) {
super(context);
init();
}
public GPUImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mGPUImage = new GPUImage(getContext());
mGPUImage.setGLSurfaceView(this);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mRatio == 0.0f) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
} else {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int newHeight;
int newWidth;
if (width / mRatio < height) {
newWidth = width;
newHeight = Math.round(width / mRatio);
} else {
newHeight = height;
newWidth = Math.round(height * mRatio);
}
int newWidthSpec = MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY);
int newHeightSpec = MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY);
super.onMeasure(newWidthSpec, newHeightSpec);
}
}
// TODO Should be an xml attribute. But then GPUImage can not be distributed as .jar anymore.
public void setRatio(float ratio) {
mRatio = ratio;
requestLayout();
mGPUImage.deleteImage();
}
/**
* Set the filter to be applied on the image.
*
* @param filter Filter that should be applied on the image.
*/
public void setFilter(GPUImageFilter filter) {
mFilter = filter;
mGPUImage.setFilter(filter);
requestRender();
}
/**
* Get the current applied filter.
*
* @return the current filter
*/
public GPUImageFilter getFilter() {
return mFilter;
}
/**
* Sets the image on which the filter should be applied.
*
* @param bitmap the new image
*/
public void setImage(final Bitmap bitmap) {
mGPUImage.setImage(bitmap);
}
/**
* Sets the image on which the filter should be applied from a Uri.
*
* @param uri the uri of the new image
*/
public void setImage(final Uri uri) {
mGPUImage.setImage(uri);
}
/**
* Sets the image on which the filter should be applied from a File.
*
* @param file the file of the new image
*/
public void setImage(final File file) {
mGPUImage.setImage(file);
}
/**
* Save current image with applied filter to Pictures. It will be stored on
* the default Picture folder on the phone below the given folerName and
* fileName. <br />
* This method is async and will notify when the image was saved through the
* listener.
*
* @param folderName the folder name
* @param fileName the file name
* @param listener the listener
*/
public void saveToPictures(final String folderName, final String fileName,
final GPUImage.OnPictureSavedListener listener) {
mGPUImage.saveToPictures(folderName, fileName, listener);
}
}