码迷,mamicode.com
首页 > 其他好文 > 详细

Matrix学习

时间:2019-01-07 17:47:44      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:rgb   getheight   return   load   print   ack   idt   resource   inf   

package com.loaderman.customviewdemo;

import android.app.Activity;
import android.graphics.ColorMatrix;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ColorMatrix colorMatrix1 = new ColorMatrix(new float[]{
                0.1f, 0.2f, 0.3f, 0.4f, 0.5f,
                0, 1, 0, 0, 0,
                0, 0, 1, 0, 0,
                0, 0, 0, 1, 0,
        });


        ColorMatrix colorMatrix2 = new ColorMatrix(new float[]{
                0.11f, 0, 0, 0, 0,
                0, 0.22f, 0, 0, 0,
                0, 0, 0.33f, 0, 0,
                0, 0, 0, 0.44f, 0,
        });

        printSetConcat(colorMatrix1,colorMatrix2);
//        printPreConcat(colorMatrix1,colorMatrix2);
//        printPostConcat(colorMatrix1,colorMatrix2);

    }

    private void printPostConcat(ColorMatrix colorMatrix1, ColorMatrix colorMatrix2){
        Log.d("loaderman",printArray(colorMatrix1.getArray()));
        Log.d("loaderman",printArray(colorMatrix2.getArray()));
        colorMatrix2.postConcat(colorMatrix1);
        Log.d("loaderman",printArray(colorMatrix2.getArray()));
    }

    private void printPreConcat(ColorMatrix colorMatrix1,ColorMatrix colorMatrix2){
        Log.d("loaderman",printArray(colorMatrix1.getArray()));
        colorMatrix1.preConcat(colorMatrix2);
        Log.d("loaderman",printArray(colorMatrix2.getArray()));
        Log.d("loaderman",printArray(colorMatrix1.getArray()));
    }

    private void printSetConcat(ColorMatrix colorMatrix1,ColorMatrix colorMatrix2){
        ColorMatrix resultMatrix = new ColorMatrix(new float[]{
                0, 0, 0, 0, 0,
                0, 0, 0, 0, 0,
                0, 0, 0, 0, 0,
                0, 0, 0, 0, 0,
        });
        resultMatrix.setConcat(colorMatrix1,colorMatrix2);

        Log.d("loaderman",printArray(colorMatrix1.getArray()));
        Log.d("loaderman",printArray(colorMatrix2.getArray()));
        Log.d("loaderman",printArray(resultMatrix.getArray()));
    }


    private String printArray(float[] array){
        StringBuilder builder = new StringBuilder("array dump:\n");
        for (int i=0;i<array.length;i++){
            if (i%5==0){
                builder.append("\n");
            }
            builder.append(array[i]+" ");
        }
        return builder.toString();
    }

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#ffffff"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.loaderman.customviewdemo.MyView
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>
package com.loaderman.customviewdemo;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {
    private Paint mPaint = new Paint();
    private Bitmap bitmap;// 位图

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mPaint.setAntiAlias(true);
        // 获取位图
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.dog);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        drawRect(canvas);
        drawBitmap(canvas);
    }

    private void drawRect(Canvas canvas){
        mPaint.setARGB(255,200,100,100);
        // 绘制原始位图
        canvas.drawRect(0,0,500,600,mPaint);

        canvas.translate(550,0);
        // 生成色彩矩阵
        ColorMatrix colorMatrix = new ColorMatrix(new float[]{
                0, 0, 0, 0, 0,
                0, 0, 0, 0, 0,
                0, 0, 1, 0, 0,
                0, 0, 0, 1, 0,
        });
        mPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        canvas.drawRect(0,0,500,600,mPaint);
    }

    private void drawBitmap(Canvas canvas){
        canvas.drawBitmap(bitmap, null, new Rect(0, 0, 500, 500 * bitmap.getHeight() / bitmap.getWidth()), mPaint);

        canvas.save();
        canvas.translate(510, 0);
        // 生成色彩矩阵
//        ColorMatrix colorMatrix = new ColorMatrix(new float[]{
//                0.213f, 0.715f, 0.072f, 0, 0,
//                0.213f, 0.715f, 0.072f, 0, 0,
//                0.213f, 0.715f, 0.072f, 0, 0,
//                0,       0,    0, 1, 0,
//        });
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setScale(1,1.3f,1,1);
        mPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));


        canvas.drawBitmap(bitmap, null, new Rect(0, 0, 500, 500 * bitmap.getHeight() / bitmap.getWidth()), mPaint);

        canvas.restore();
//        canvas.translate(0,500);
//        ColorMatrix colorMatrix2 = new ColorMatrix(new float[]{
//                0.3086f, 0.6094f, 0.0820f, 0, 0,
//                0.3086f, 0.6094f, 0.0820f, 0, 0,
//                0.3086f, 0.6094f, 0.0820f, 0, 0,
//                0    , 0    , 0    , 1, 0
//        });
//
//        mPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix2));
//        canvas.drawBitmap(bitmap, null, new Rect(0, 0, 500, 500 * bitmap.getHeight() / bitmap.getWidth()), mPaint);
//
//        canvas.translate(510,0);
//        ColorMatrix colorMatrix3 = new ColorMatrix(new float[]{
//                0, 0, 0, 0, 0,
//                0, 0, 0, 0, 0,
//                0, 0, 1, 0, 0,
//                0, 0, 0, 1, 0,
//        });
//        mPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix3));
//        canvas.drawBitmap(bitmap, null, new Rect(0, 0, 500, 500 * bitmap.getHeight() / bitmap.getWidth()), mPaint);

    }
}

效果图:

技术分享图片

Matrix学习

标签:rgb   getheight   return   load   print   ack   idt   resource   inf   

原文地址:https://www.cnblogs.com/loaderman/p/10232338.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!