分享一个图片特效-------底片
-------------------------------------------------------------------------------
算法原理:
将当前像素点的RGB值分别与255之差后的值作为当前点的RGB值,即
R = 255 – R;G = 255 – G;B = 255 – B;
R = 255 – R;G = 255 – G;B = 255 – B;
-------------------------------------------------------------------------------
示例:
新建一个activity,布局很简单就不贴了。其中核心代码在func函数中。
package com.example.graphicsdemo1; 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) { if(v.getId() == R.id.but_func) { func(); }else if(v.getId() == R.id.but_old) { iv_show.setImageResource(R.raw.girl); } } /** * 底片效果 * 注意图片分辨率不能太高防止oom */ public void func() { //原图 Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.raw.girl); int width = bitmap.getWidth(); int height = bitmap.getHeight(); //创建可写的bitmap Bitmap base = Bitmap.createBitmap(width,height,bitmap.getConfig()); Paint paint = new Paint();//新建画笔 // paint.setStrokeWidth(1); // 新建画布 Canvas canvas = new Canvas(base); canvas.drawBitmap(bitmap, new Matrix(), 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); //修改图片每个点的像素的rgb值 base.setPixel(i,j, Color.argb(a, 255-r, 255-g, 255-b)); } } bitmap.recycle(); iv_show.setImageBitmap(base); } }
效果:
原图:
---------------------------------------------------------完-------------------------------------------------
ps.妹子是我们学校的哦
-------------------------------------------------------------------------------------------------------------