【安卓笔记】图片特效之底片效果
R = 255 – R;G = 255 – G;B = 255 – B;
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); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。