Android drawing mechanism 2

ColorMatrix

In Android, the most commonly used data structure for image processing is Bitmap. The whole image is composed of pixel lattice and corresponding transparency, red, green, and blue color values. In Android, the system uses ColorMatrix class to process image color effects. ColorMatrix is actually a 4x5 color matrix

        imageView = (ImageView)findViewById(R.id.icon_group);
        ColorMatrix hueMatrix = new ColorMatrix();
        hueMatrix.setRotate(0,1);//Set the color tone and use 0 1 2 to represent red, green and blue
        hueMatrix.setRotate(1,2);
        hueMatrix.setRotate(2,1);
        ColorMatrix saturationMatrix = new ColorMatrix();
        saturationMatrix.setSaturation(0);//When the saturation of the color is set to 0, the image becomes grayscale
        ColorMatrix lumMatrix = new ColorMatrix();
        lumMatrix.setScale(0.33f,2f,91f,1);//Set the brightness of the image to 0 when it is all black
        ColorMatrix imageMatrix = new ColorMatrix();
        imageMatrix.postConcat(hueMatrix);//Mix the functions of the matrix and overlay the processing effect
        imageMatrix.postConcat(saturationMatrix);
        imageMatrix.postConcat(lumMatrix);
        Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.meijing);
        Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(),bm.getHeight(),Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
        canvas.drawBitmap(bm,0,0,paint);
        imageView.setImageBitmap(bitmap);
// Mode 2 change the coefficient of matrix to change the color
        Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.meijing);
        Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(),bm.getHeight(),Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        float[] mColorMatrix = new float[20];
        for (int i = 0;i<20;i++){
            mColorMatrix[i]= Float.valueOf(String.valueOf(i));//Change the coefficient of matrix
        }
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.set(mColorMatrix);//Set the ARGB matrix of 4x5 after the coefficient changes
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        canvas.drawBitmap(bm,0,0,paint);
        imageView.setImageBitmap(bitmap);

Matrix

Android uses a 3 x 3 Matrix to process image transformation. The system provides Matrix class to process image deformation

        float[] mMatrix = new float[]{1,0,0,0,1,0,0,0,1};
        Matrix matrix = new Matrix();
        matrix.setValues(mMatrix);
        matrix.setTranslate(200,200);//Image panning
        matrix.setRotate(15);//Image rotation
        matrix.setSkew(15,15);//Image crosscutting
        canvas.drawBitmap(bm,matrix,paint);//Draw the image with this transformation matrix
        imageView.setImageBitmap(bitmap);

Tags: Android

Posted on Mon, 04 May 2020 10:41:56 -0700 by ikebaldo