问题描述
我正在尝试图像翻转和图像过滤器但是我有一个问题当我在图像上放置过滤器然后当我翻转图像时过滤器被删除
public void Flipimg(View v1){
img1.setimageBitmap(fliph(scaledBitmap));
}
public Bitmap fliph(Bitmap bms){
Matrix mat = new Matrix();
mat.setScale(-1,1);
Bitmap bit1=Bitmap.createBitmap( bms,bms.getWidth(),bms.getHeight(),mat,true);
return bit;
public void Filterimg(View v2){
img1.setimageBitmap(applyGreyscaleEffect(scaledBitmap));
}
public Bitmap applyGreyscaleEffect(Bitmap src) {
final double GS_RED = 0.299;
final double GS_GREEN = 0.587;
final double GS_BLUE = 0.114;
Bitmap bmOut = Bitmap.createBitmap(src.getWidth(),src.getHeight(),src.getConfig());
int A,R,G,B;
int pixel;
int width = src.getWidth();
int height = src.getHeight();
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
pixel = src.getPixel(x,y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
R = G = B = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B);
bmOut.setPixel(x,y,Color.argb(A,B));}
}
return bmOut;
looking images THIS image flip
解决方法
如果您想按顺序应用效果,请通过 BitmapDrawable 获取 ImageView 上的当前位图。
public void Flipimg(View v1)
{
//img1.setImageBitmap(fliph(scaledBitmap));
Bitmap currentBitmap = ((BitmapDrawable) img1.getDrawable()).getBitmap();
img1.setImageBitmap(fliph(currentBitmap));
}
public void Filterimg(View v2)
{
//img1.setImageBitmap(applyGreyscaleEffect(scaledBitmap));
Bitmap currentBitmap = ((BitmapDrawable) img1.getDrawable()).getBitmap();
img1.setImageBitmap(applyGreyscaleEffect(currentBitmap));
}