2017-03-07 59 views
1

我一直在努力实现这一形象 -如何在android中加扰图像(图像操作)?

enter image description here

称为失真的图像在列表中的项目的点击。

问题是,我无法实现这一点,我得到一个非炒图像作为旧图像。

这是我的主要活动代码:

public class MainActivity extends AppCompatActivity { 

static final String CAMERA_PIC_DIR = "/DCIM/Camera/"; 
ImageView iv; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    iv = (ImageView) findViewById(R.id.my_image); 
    String ImageDir = Environment.getExternalStorageDirectory() 
      .getAbsolutePath() + CAMERA_PIC_DIR; 
    Intent i = new Intent(this, ListFiles.class); 
    i.putExtra("directory", ImageDir); 
      startActivityForResult(i,0); 
} 
@Override 
protected void onActivityResult(int requestCode, 
           int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if(requestCode == 0 && resultCode==RESULT_OK) { 
     String tmp = data.getExtras().getString("clickedFile"); 
     Bitmap ImageToChange= BitmapFactory.decodeFile(tmp); 
     process_image(ImageToChange); 
    } 
} 
void process_image(Bitmap image) { 
    Bitmap bm = Bitmap.createScaledBitmap(image, 480, 320, false); 
    int width = bm.getWidth(); 
    int height = bm.getHeight(); 
    int x= width>>1; 
    int y= height>>1; 
    int[] pixels1 = new int[(width*height)]; 
    int[] pixels2 = new int[(width*height)]; 
    int[] pixels3 = new int[(width*height)]; 
    int[] pixels4 = new int[(width*height)]; 
    bm.getPixels(pixels1, 0, width, 0, 0, width>>1, height>>1); 
    bm.getPixels(pixels2, 0, width, x, 0, width>>1, height>>1); 
    bm.getPixels(pixels3, 0, width, 0, y, width>>1, height>>1); 
    bm.getPixels(pixels4, 0, width, x, y, width>>1, height>>1); 
    if(bm.isRecycled()) { 
     bm.setPixels(pixels2, 0, width, 0, 0, width>>1, height>>1); 
     bm.setPixels(pixels4, 0, width, x, 0, width>>1, height>>1); 
     bm.setPixels(pixels1, 0, width, 0, y, width>>1, height>>1); 
     bm.setPixels(pixels3, 0, width, x, y, width>>1, height>>1); 
    } 
    iv.setImageBitmap(bm); 
} 
} 

任何正面答复将不胜感激。

感谢您的帮助!

+1

尝试删除'if(bm.isRecycled())'条件,一切都应该正常工作 – vasart

+0

用什么替换isRecycled()? –

+1

用'!bm.isRecycled()替换它'' – vasart

回答

1

您可以通过if(isMutable())

这种替代if(isRecycled())应该做的伎俩!

+0

我解决了上述解决方案的问题,但解决方案也应该可行。 –