【安卓笔记】图片特效之黑白效果
package com.example.graphicsdemo2; import java.util.Formatter.BigDecimalLayoutForm; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity implements OnClickListener { private Button but_old = null; private Button but_func = null; private ImageView iv_show = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); but_func = (Button) findViewById(R.id.but_func); but_old = (Button) findViewById(R.id.but_old); iv_show = (ImageView) findViewById(R.id.iv_show); but_func.setOnClickListener(this); but_old.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.but_func: func(); break; case R.id.but_old: iv_show.setImageResource(R.raw.girl); break; } } public void func() { Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.raw.girl); int width = bitmap.getWidth(); int height = bitmap.getHeight(); Bitmap base = Bitmap.createBitmap(width, height,bitmap.getConfig()); Canvas canvas = new Canvas(base);//以base为模板创建canvas对象 canvas.drawBitmap(bitmap, new Matrix(),new Paint()); for(int i = 0; i < width; i++)//遍历像素点 { for(int j = 0; j < height; j++) { int color = bitmap.getPixel(i, j); int r = Color.red(color); int g = Color.green(color); int b = Color.blue(color); int a = Color.alpha(color); int avg = (r+g+b)/3;//RGB均值 if(avg >= 100)//100可以理解为经验值 { base.setPixel(i, j,Color.argb(a, 255, 255, 255));//设为白色 } else { base.setPixel(i, j,Color.argb(a, 0, 0, 0));//设为黑色 } } } bitmap.recycle(); iv_show.setImageBitmap(base); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。