图像翻转和图像过滤器android

问题描述

我正在尝试图像翻转和图像过滤器但是我有一个问题当我在图像上放置过滤器然后当我翻转图像时过滤器被删除

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

THIS image apply filter

解决方法

如果您想按顺序应用效果,请通过 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));
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...