Bitmap之getPixel和setPixel函数
package com.loaderman.customviewdemo; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.os.Bundle; import android.widget.ImageView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Bitmap srcBmp = BitmapFactory.decodeResource(getResources(), R.drawable.dog); ImageView iv1 = (ImageView) findViewById(R.id.img1); iv1.setImageBitmap(srcBmp); Bitmap desBmp = srcBmp.copy(Bitmap.Config.ARGB_8888, true); for (int h = 0; h < srcBmp.getHeight(); h++) { for (int w = 0; w < srcBmp.getWidth(); w++) { int originColor = srcBmp.getPixel(w, h); int red = (Color.red(originColor)); int alpha = Color.alpha(originColor); int green = Color.green(originColor); int blue = Color.blue(originColor); if (green < 200) { green += 30; } desBmp.setPixel(w, h, Color.argb(alpha, red, green, blue)); } } ImageView iv2 = (ImageView) findViewById(R.id.img2); iv2.setImageBitmap(desBmp); } }
效果:
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!
posted on 2019-01-07 10:19 LoaderMan 阅读(1191) 评论(0) 编辑 收藏 举报