本实例是使用原图片进行简单的过滤处理成自己想要的效果,过滤图片的处理类使用的是daizhj提供的,github上面有下载:
源码下载:
https://github.com/daizhenjun/ImageFilterC C++ for ios
https://github.com/daizhenjun/ImageFilterForWindowsPhone Windows Phone7
https://github.com/daizhenjun/ImageFilterForAndroid Android 2.1版本及以上
原文链接:http://www.cnblogs.com/daizhj/archive/2012/06/21/2557550.html
下面的效果是基于以上作者提供的处理包做优化和实现的。
效果如下:
1.首先实选择列表从最左边开始排序并且默认选中第一项,这里为了实现从最左边开始所以使用HorizontalScrollView结合GridView实现Gallery view 图片效果图,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- Gallery view 图片效果图 -->
<HorizontalScrollView
android:id="@+id/galleryScroll"
android:layout_width="fill_parent"
android:layout_height="90dip"
android:scrollbars="none"
android:focusable="false"
android:layout_alignParentBottom="true"
android:background="@drawable/camera_filter_review_bg"
>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="false"
android:layout_gravity="center_vertical"
>
<LinearLayout
android:layout_width="770dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:focusable="false"
android:gravity="center"
android:layout_gravity="center"
>
<GridView android:id="@+id/gallery"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:horizontalSpacing="1.0dip"
android:verticalSpacing="1.0dip"
android:stretchMode="spacingWidthUniform"
android:numColumns="auto_fit"
android:columnWidth="70dip"
android:focusable="false"
android:cacheColorHint="#00000000"
android:listSelector="#00000000"
android:layout_gravity="center"
>
</GridView>
</LinearLayout>
</FrameLayout>
</HorizontalScrollView>
</LinearLayout>
2.下面把主界面代码贴出来,主要是加载数据,以及异步渲染图片操作。
import java.util.ArrayList;
import java.util.List;
import HaoRan.ImageFilter.AutoAdjustFilter;
import HaoRan.ImageFilter.ColorQuantizeFilter;
import HaoRan.ImageFilter.FeatherFilter;
import HaoRan.ImageFilter.IImageFilter;
import HaoRan.ImageFilter.Image;
import HaoRan.ImageFilter.OldPhotoFilter;
import HaoRan.ImageFilter.R;
import HaoRan.ImageFilter.RainBowFilter;
import HaoRan.ImageFilter.SepiaFilter;
import HaoRan.ImageFilter.VignetteFilter;
import HaoRan.ImageFilter.VintageFilter;
import HaoRan.ImageFilter.WaterWaveFilter;
import HaoRan.ImageFilter.XRadiationFilter;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class ImageFilterMain extends Activity {
private ImageView imageView;
private ProgressBar mProgressBar;
private List<FilterInfo> filterArray = new ArrayList<FilterInfo>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView= (ImageView) findViewById(R.id.cropimage_filter_show_iv);
mProgressBar = (ProgressBar) findViewById(R.id.progress_large);
//注:在android系统上,手机图片尺寸尽量控制在480*480范围内,否则在高斯运算时可以造成内存溢出的问题
Bitmap bitmap = BitmapFactory.decodeResource(ImageFilterMain.this.getResources(), R.drawable.image);
imageView.setImageBitmap(bitmap);
loadData();
LoadImageFilter();
}
private void LoadImageFilter() {
GridView gallery = (GridView) findViewById(R.id.gallery);
final ImageAdapter filterAdapter = new ImageAdapter(ImageFilterMain.this,filterArray);
gallery.setAdapter(filterAdapter);
gallery.setNumColumns(11);
gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
filterAdapter.changeStatus(position);
filterAdapter.notifyDataSetChanged();
IImageFilter filter = (IImageFilter) filterAdapter.getItem(position);
//开始渲染
new processImageTask(ImageFilterMain.this, filter).execute();
}
});
}
/**
* 加载图片filter
*/
private void loadData(){
filterArray.add(new FilterInfo(R.drawable.saturationmodity_filter,null/* 此处会生成原图效果 */,true));
filterArray.add(new FilterInfo(R.drawable.vignette_filter, new VignetteFilter(),false));
filterArray.add(new FilterInfo(R.drawable.autoadjust_filter,new AutoAdjustFilter(),false));
filterArray.add(new FilterInfo(R.drawable.colorquantize_filter, new ColorQuantizeFilter(),false));
filterArray.add(new FilterInfo(R.drawable.waterwave_filter, new WaterWaveFilter(),false));
filterArray.add(new FilterInfo(R.drawable.vintage_filter,new VintageFilter(),false));
filterArray.add(new FilterInfo(R.drawable.oldphoto_filter,new OldPhotoFilter(),false));
filterArray.add(new FilterInfo(R.drawable.sepia_filter, new SepiaFilter(),false));
filterArray.add(new FilterInfo(R.drawable.rainbow_filter,new RainBowFilter(),false));
filterArray.add(new FilterInfo(R.drawable.feather_filter,new FeatherFilter(),false));
filterArray.add(new FilterInfo(R.drawable.xradiation_filter,new XRadiationFilter(),false));
}
//渲染,过滤图片异步任务
public class processImageTask extends AsyncTask<Void, Void, Bitmap> {
private IImageFilter filter;
private Activity activity = null;
public processImageTask(Activity activity, IImageFilter imageFilter) {
this.filter = imageFilter;
this.activity = activity;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mProgressBar.setVisibility(View.VISIBLE);
}
public Bitmap doInBackground(Void... params) {
Image img = null;
try
{
Bitmap bitmap = BitmapFactory.decodeResource(activity.getResources(), R.drawable.image);
img = new Image(bitmap);
if (filter != null) {
img = filter.process(img);
img.copyPixelsFromBuffer();
}
return img.getImage();
}
catch(Exception e){
if (img != null && img.destImage.isRecycled()) {
img.destImage.recycle();
img.destImage = null;
System.gc(); // 提醒系统及时回收
}
}
finally{
if (img != null && img.image.isRecycled()) {
img.image.recycle();
img.image = null;
System.gc(); // 提醒系统及时回收
}
}
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
if(result != null){
super.onPostExecute(result);
imageView.setImageBitmap(result);
}
mProgressBar.setVisibility(View.GONE);
}
}
}
3.最好把代码附上,有需要的可以自己下载慢慢研究