2017-03-07 43 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

我解決了上述解決方案的問題,但解決方案也應該可行。 –