我正在开发一个应用程序,它需要像 Instagram 这样的视频和图像过滤器我花了很多时间在互联网上我没有发现任何非常有用的东西现在我正在将此代码用于图像过滤器,但响应时间很慢。
public class LightingFilterPagerAdapter extends PagerAdapter {
public static String[] styles = { "No Filter", "Chrome", "Cool", "Classic", "Fade", "Noir", "Greyscale", "Highlight", "Invert", "Snow", "MeanRemova", "SepiaToning" };
private Bitmap originBitmap, filteredBitmap;
private SparseArray<View> selectorArray = new SparseArray<View>();
private LayoutInflater layoutInflater;
public LightingFilterPagerAdapter(Context context) {
layoutInflater = LayoutInflater.from(context);
}
public void setFilterable(Bitmap bitmap) {
this.originBitmap = null == bitmap ? BitmapFactory.decodeResource(layoutInflater.getContext().getResources(), R.drawable.ic_launcher) : Bitmap.createScaledBitmap(bitmap,
150, 150, false);
}
@Override
public float getPageWidth(int position) {
return 0.35f;
}
public Object instantiateItem(View container, final int position) {
View view = layoutInflater.inflate(R.layout.filter_pager_item, null);
if (position == 0 || position > styles.length) {
filteredBitmap = originBitmap;
} else {
filteredBitmap = applyStyle(position, originBitmap);
}
// initially selected no filter
View selView = view.findViewById(R.id.selector_v);
selectorArray.put(position, selView);
selView.setVisibility(0 == position ? View.VISIBLE : View.GONE);
ImageView filterIv = (ImageView) view.findViewById(R.id.filter_iv);
filterIv.setImageBitmap(filteredBitmap);
TextView filterTv = (TextView) view.findViewById(R.id.filter_tv);
filterTv.setText(styles[position]);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// unselect all except selected one
for (int i = 0; i < selectorArray.size(); i++) {
View selectorView = selectorArray.get(selectorArray.keyAt(i));
selectorView.setVisibility(i == position ? View.VISIBLE : View.GONE);
}
if (null != filterSelectedListener) {
filterSelectedListener.onFilterSelected(position, styles[position]);
}
}
});
((ViewPager) container).addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
@Override
public int getCount() {
return null == styles ? 0 : styles.length;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
private FilterSelectedListener filterSelectedListener;
public void setFilterSelectedListener(FilterSelectedListener listener) {
this.filterSelectedListener = listener;
}
public interface FilterSelectedListener {
void onFilterSelected(int position, String name);
}
public interface StyleListener {
void onStyleApplied(int position, String filter, Bitmap filtered);
}
public static void applyStyleAsync(final Context context, final int position, final Bitmap bitmap, final StyleListener listener) throws IllegalArgumentException {
if (null == context || position < 0 || position >= styles.length || null == listener) {
throw new IllegalArgumentException("Arguments should avoid conditions: (null == context || position < 0 || position >= styles.length || null == listener)");
}
new Thread(new Runnable() {
@Override
public void run() {
final Bitmap filtered = position == 0 ? bitmap : applyStyle(position, bitmap);
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
listener.onStyleApplied(position, styles[position], filtered);
}
});
}
}).start();
}
/**
* "No Filter", "Chrome", "Cool", "Classic", "Fade", "Noir"
*
* @param styleNo
* @param originBitmap
* @return
*/
public static Bitmap applyStyle(int styleNo, Bitmap originBitmap) {
Bitmap changeBitmap = null;
switch (styleNo) {
case 1:
// changeBitmap = getFilteredBitmap(originBitmap, 1, 1, 1.5f, true);
// changeBitmap = getFilteredBitmap(originBitmap, 1, 69, 9, true);
//changeBitmap=ImageFilters.applyFleaEffect(originBitmap);
changeBitmap = applyTintFilter(originBitmap, "#FFCC00");
break;
case 2:
changeBitmap = applyTintFilter(originBitmap, "#bfc3ff");
break;
case 3:
changeBitmap = applyTintFilter(originBitmap, "#ffffcc");
break;
case 4:
changeBitmap = getFilteredBitmap(originBitmap, 1, -30, 0, false);
break;
case 5:
changeBitmap = getFilteredBitmap(originBitmap, 1, 1, 0.05f, true);
break;
case 6:
changeBitmap = ImageFilters.applyGreyscaleEffect(originBitmap);
break;
case 7:
changeBitmap = ImageFilters.applyHighlightEffect(originBitmap);
break;
case 8:
changeBitmap = ImageFilters.applyInvertEffect(originBitmap);
break;
case 9:
changeBitmap = ImageFilters.applySnowEffect(originBitmap);
break;
case 10:
changeBitmap = ImageFilters.applyMeanRemovalEffect(originBitmap);
break;
case 11:
changeBitmap = ImageFilters.applySepiaToningEffect(originBitmap, 1, 21, 20, 255);
break;
case 12:
changeBitmap = ImageFilters.applyDecreaseColorDepthEffect(originBitmap, 5);
break;
case 13:
changeBitmap = getFilteredBitmap(originBitmap, 1, 1, 0.05f, true);
break;
case 14:
changeBitmap = getFilteredBitmap(originBitmap, 1, 1, 0.05f, true);
break;
case 15:
changeBitmap = getFilteredBitmap(originBitmap, 1, 1, 0.05f, true);
break;
default:
changeBitmap = originBitmap;
break;
}
return changeBitmap;
}
private static Bitmap getFilteredBitmap(Bitmap bmp, float contrast, float brightness, float saturation, boolean bandw) {
Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
Canvas canvas = new Canvas(alteredBitmap);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.set(new float[] { contrast, 0, 0, 0, brightness, 0, contrast, 0, 0, brightness, 0, 0, contrast, 0, brightness, 0, 0, 0, 1, 0 });
if (bandw) {
cm.setSaturation(saturation);
}
paint.setColorFilter(new ColorMatrixColorFilter(cm));
Matrix matrix = new Matrix();
canvas.drawBitmap(bmp, matrix, paint);
return alteredBitmap;
}
private static Bitmap applyTintFilter(Bitmap bitmap, String htmlColor) {
Bitmap bmOut = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
Canvas canvas = new Canvas(bmOut);
Paint paint = new Paint();
ColorFilter cf = new PorterDuffColorFilter(Color.parseColor(htmlColor), Mode.MULTIPLY);
paint.setColorFilter(cf);
Matrix matrix = new Matrix();
canvas.drawBitmap(bitmap, matrix, paint);
return bmOut;
}