如何以编程方式在android中对imageview图像应用效果?

时间:2014-05-13 06:25:52

标签: android

我想对图像视图图像应用各种效果。这个图像已经从主要活动发送。我有效果的代码,但不知道如何在按钮点击上应用它的方法。请引导我。

public class PhotoEffect extends Activity implements OnClickListener  {
ImageView camphoto; 
Bitmap bmp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photoeffect);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    Bundle extras=getIntent().getExtras();
    //b=(Bitmap)extras.get("img");

    byte[] byteArray=extras.getByteArray("img");

    bmp=BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    camphoto=(ImageView)findViewById(R.id.camimage);
        camphoto.setImageBitmap(bmp);


    /*if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }*/
}
@Override
public void onClick(View v)
{
    Bitmap bitmap = ((BitmapDrawable)camphoto.getDrawable()).getBitmap();
    switch (v.getId())
    {
    case R.id.btnsepia:
        //Bitmap bitmap = ((BitmapDrawable)camphoto.getDrawable()).getBitmap();
         camphoto.setImageBitmap(createSepiaToningEffect(bitmap, 2, .5, .6, .59));
        break;
    case R.id.btnhighlight:
        camphoto.setImageBitmap(doHighlightImage(bitmap));
        break;
    }
}

1 个答案:

答案 0 :(得分:0)

你必须像这样定义createSepiaToningEffect函数,在处理后返回一个Bitmap。

public static Bitmap createSepiaToningEffect(Bitmap src, int depth, double red, double green, double blue) {
    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
    final double GS_RED = 0.3;
    final double GS_GREEN = 0.59;
    final double GS_BLUE = 0.11;
    int A, R, G, B;
    int pixel;

    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);
            B = G = R = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);

            // apply intensity level for sepid-toning on each channel
            R += (depth * red);
            if(R > 255) { R = 255; }

            G += (depth * green);
            if(G > 255) { G = 255; }

            B += (depth * blue);
            if(B > 255) { B = 255; }

            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }

    return bmOut;

}