在按钮的 onClick() 中调用具有多个参数的方法

call method with multiple parameters in onClick() of a button

我是 Android 的新手,我正在尝试使用 Edit Activity 构建图库应用程序。我附上了我的应用程序编辑屏幕的屏幕截图。有一个 ImageView、一个 SeekBar 和 4 个 ImageButton。 ImageButtons 实现了 4 种编辑功能——亮度、饱和度等。我有所有的效果方法。我想知道的是当我单击图像按钮(可能是亮度)并拖动搜索栏时,亮度应该增加,同样,当我单击对比度图像按钮并拖动搜索栏时,图像对比度应该增加。我该如何实施。有人可以帮助我吗.. 我尝试对 ImageButtons 使用 setOnTouchListener() 但它起作用了,因为它只接受视图作为参数而不接受位图。请提前帮助me.Thanks

下面是我的编辑Activity

public class EditActivity extends Activity
   {
         private Bitmap bitmap;
         private ImageView image;
         private ImageButton bb,sab,shb,cb;
         private BitmapDrawable drawable;
         private SeekBar seekBar;

         @Override
         public void onCreate( Bundle savedInstanceState)
         {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.filters);


    // Get intent data
    Intent i = getIntent();

    // Selected image id
    final int position = i.getExtras().getInt("id");
    ImageAdapter imageAdapter = new ImageAdapter(this);

    image = (ImageView) findViewById(R.id.imageView);
    image.setImageResource(imageAdapter.ids[position]);


    bb = (ImageButton) findViewById(R.id.brightButton);
    drawable = (BitmapDrawable) image.getDrawable();
    bitmap = drawable.getBitmap();
    bb.setImageBitmap(bitmap);

    sab=(ImageButton)findViewById(R.id.saturationButton);
    drawable = (BitmapDrawable) image.getDrawable();
    bitmap = drawable.getBitmap();
    sab.setImageBitmap(bitmap);

    cb=(ImageButton) findViewById(R.id.contrastButton);
    drawable = (BitmapDrawable) image.getDrawable();
    bitmap = drawable.getBitmap();
    cb.setImageBitmap(bitmap);

    shb=(ImageButton) findViewById(R.id.sharpButton);
    drawable = (BitmapDrawable) image.getDrawable();
    bitmap = drawable.getBitmap();
    shb.setImageBitmap(bitmap);


    SeekBar seekBar=(SeekBar)findViewById(R.id.seekBar);
    seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
    {
        int brightness;

        @Override
        public void onStopTrackingTouch(SeekBar arg0) {


        }

        @Override
        public void onStartTrackingTouch(SeekBar arg0) {


        }

        @Override
        public void onProgressChanged(SeekBar arg0, final int progress, boolean arg2)
        {
            brightness = progress;


                    Bitmap bBitMap = brightClicked(bitmap,progress);
                    image.setImageBitmap(bBitMap);
                    Bitmap saBitMap = sharpClicked(bitmap,progress);
                    image.setImageBitmap(saBitMap);
                    Bitmap cBitMap = saturationClicked(bitmap,progress);
                    image.setImageBitmap(cBitMap);
                    Bitmap shBitMap = contrastClicked(bitmap,progress);
                    image.setImageBitmap(shBitMap);

        }
    });


}



public Bitmap brightClicked(Bitmap bbitmap,int value)
{

    Bitmap bmOut= Bitmap.createBitmap(bbitmap.getWidth(), bbitmap.getHeight(),bbitmap.getConfig());
    int A, R, G, B;
    int pixel;

    for(int i=0; i<bbitmap.getWidth(); i++){
        for(int j=0; j<bbitmap.getHeight(); j++)
        {
            pixel = bbitmap.getPixel(i, j);
            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);
            R += value;
            if (R > 255)
            {
                R = 255;
            } else if (R < 0)
            {
                R = 0;
            }
            G += value;
            if (G > 255)
            {
                G = 255;
            } else if (G < 0)
            {
                G = 0;
            }
            B += value;
            if (B > 255)
            {
                B = 255;
            } else if (B < 0)
            {
                B = 0;
            }
            bmOut.setPixel(i, j, Color.argb(A, R, G, B));
        }
    }
    return bmOut;
}// and other methods of effects like sharpness,contrast..

下面是Screenshot of EditImage

我将从定义状态开始,以便 SeekBar 可以快速确定它应该修改的属性(对比度、亮度、锐度、饱和度)。将它们放入枚举 class(Google 建议避免使用 enums in Android),如果您喜欢或在 class 本身内将它们定义为整数。例如:

public class EditActivity extends Activity {
    private int currentState;
    private static final int SATURATE = 0;
    private static final int CONTRAST = 1;
    ....
}

现在您可以根据单击的按钮更新状态:

bb = (ImageButton) findViewById(R.id.brightButton);
bb.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          currentState = BRIGHTNESS;
      }
}

然后使用 switch-case 语句快速确定 Activity 的状态,并通过您的图像方法相应地调整图像。

seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
    @Override
    public void onProgressChanged(SeekBar arg0, final int progress, boolean arg2)
    {
        brightness = progress;
        switch (currentState) {
               case (SATURATE) : 
                     image.setImageBitmap(saturationClicked(bitmap,progress));
                     break;
               case (CONTRAST) :
                     image.setImageBitmap(contrastClicked(bitmap,progress));
                     break;

看起来您当前的实现会在搜索栏移动时 运行 图像上的所有过滤器,我认为这会导致糟糕的性能。开关盒可以帮助您避免这种情况。