android像素级图形效果

来源:互联网 发布:视频会议软件破解版 编辑:程序博客网 时间:2024/05/17 02:26

1 像素点分析

各种老照片 底片等效果 底层原理如这个方法。通过某种算法改变每个像素的值。核心方法为 bm.getPixels(oldPx, 0, width, 0, 0, width, height); 讲所有的颜色信息保存在数组oldPx中

public static Bitmap handleImageNegative(Bitmap bm) {        int width = bm.getWidth();        int height = bm.getHeight();        int color;        int r, g, b, a;        Bitmap bmp = Bitmap.createBitmap(width, height                , Bitmap.Config.ARGB_8888);        int[] oldPx = new int[width * height];        int[] newPx = new int[width * height];        bm.getPixels(oldPx, 0, width, 0, 0, width, height);        for (int i = 0; i < width * height; i++) {            color = oldPx[i];            r = Color.red(color);            g = Color.green(color);            b = Color.blue(color);            a = Color.alpha(color);            r = 255 - r;            g = 255 - g;            b = 255 - b;            if (r > 255) {                r = 255;            } else if (r < 0) {                r = 0;            }            if (g > 255) {                g = 255;            } else if (g < 0) {                g = 0;            }            if (b > 255) {                b = 255;            } else if (b < 0) {                b = 0;            }            newPx[i] = Color.argb(a, r, g, b);        }        bmp.setPixels(newPx, 0, width, 0, 0, width, height);        return bmp;    }

2 刮刮卡View
核心方法:

mPaint.setXfermode(                new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); case MotionEvent.ACTION_MOVE:                mPath.lineTo(event.getX(), event.getY());                break;                mCanvas.drawPath(mPath, mPaint);        invalidate();
public class XfermodeView extends View {    private Bitmap mBgBitmap, mFgBitmap;    private Paint mPaint;    private Canvas mCanvas;    private Path mPath;    public XfermodeView(Context context) {        super(context);        init();    }    public XfermodeView(Context context, AttributeSet attrs) {        super(context, attrs);        init();    }    public XfermodeView(Context context, AttributeSet attrs,                        int defStyleAttr) {        super(context, attrs, defStyleAttr);        init();    }    private void init() {        mPaint = new Paint();        mPaint.setAlpha(0);        mPaint.setXfermode(                new PorterDuffXfermode(PorterDuff.Mode.DST_IN));        mPaint.setStyle(Paint.Style.STROKE);        mPaint.setStrokeJoin(Paint.Join.ROUND);        mPaint.setStrokeWidth(50);        mPaint.setStrokeCap(Paint.Cap.ROUND);        mPath = new Path();        mBgBitmap = BitmapFactory.decodeResource(getResources(),                R.drawable.test);        mFgBitmap = Bitmap.createBitmap(mBgBitmap.getWidth(),                mBgBitmap.getHeight(), Bitmap.Config.ARGB_8888);        mCanvas = new Canvas(mFgBitmap);        mCanvas.drawColor(Color.GRAY);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                mPath.reset();                mPath.moveTo(event.getX(), event.getY());                break;            case MotionEvent.ACTION_MOVE:                mPath.lineTo(event.getX(), event.getY());                break;        }        mCanvas.drawPath(mPath, mPaint);        invalidate();        return true;    }    @Override    protected void onDraw(Canvas canvas) {        canvas.drawBitmap(mBgBitmap, 0, 0, null);        canvas.drawBitmap(mFgBitmap, 0, 0, null);    }
0 0
原创粉丝点击