Matrix图片变换处理

2019-03-22  本文已影响0人  沈溺_16e5
1、Activity
package com.jiyun.bitmap;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {

    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private ImageView img1;
    private ImageView img2;
    private Bitmap bitmap;
    private Button btn5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
    }

    private void initView() {
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        img1 = (ImageView) findViewById(R.id.img1);
        img2 = (ImageView) findViewById(R.id.img2);
        btn5 = (Button) findViewById(R.id.btn5);

        btn5.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_icon5);
        img1.setImageBitmap(bitmap);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_icon5);
                img1.setImageBitmap(bitmap);

                ImageUtils.bitmapScale(bitmap,2.0f,2.0f,img2);
                break;
            case R.id.btn2:
                bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_icon5);
                img1.setImageBitmap(bitmap);

                ImageUtils.bitmapRotate(bitmap, 180, img2);
                break;
            case R.id.btn3:
                bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_icon5);
                img1.setImageBitmap(bitmap);

                ImageUtils.bitmapSkew(bitmap, 0.1f, 0.2f, img2);
                break;
            case R.id.btn4:
                bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_icon5);
                img1.setImageBitmap(bitmap);

                ImageUtils.bitmapTranslate(bitmap, 100f, 100f, img2);
                break;
            case R.id.btn5:
                bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.abcd);
                img1.setImageBitmap(bitmap);

                ImageUtils.bitmapNoRed(bitmap, img1);
                break;
        }
    }


}

2、工具类
/**
 * Created by $lzj on 2019/3/19.
 */
public class ImageUtils {

    /**
     * 缩放图片
     */
    public static void bitmapScale(Bitmap bitmap, float x, float y, ImageView img2) {
        // 因为要将图片放大,所以要根据放大的尺寸重新创建Bitmap,Android不允许对原图进行处理
        Bitmap afterBitmap = Bitmap.createBitmap(
                (int) (bitmap.getWidth() * x),
                (int) (bitmap.getHeight() * y), bitmap.getConfig());
        Canvas canvas = new Canvas(afterBitmap);
        // 初始化Matrix对象
        Matrix matrix = new Matrix();
        // 根据传入的参数设置缩放比例
        matrix.setScale(x, y);
        // 根据缩放比例,把图片draw到Canvas上
        canvas.drawBitmap(bitmap, matrix,new Paint());
        img2.setImageBitmap(afterBitmap);
    }

    /**
     * 图片旋转
     */
    public static void bitmapRotate(Bitmap baseBitmap,float degrees,ImageView iv_after) {
        // 创建一个和原图一样大小的图片
        Bitmap afterBitmap = Bitmap.createBitmap(baseBitmap.getWidth(),
                baseBitmap.getHeight(), baseBitmap.getConfig());
        Canvas canvas = new Canvas(afterBitmap);
        Matrix matrix = new Matrix();
        // 根据原图的中心位置旋转
        matrix.setRotate(degrees, baseBitmap.getWidth() / 2,
                baseBitmap.getHeight() / 2);
        canvas.drawBitmap(baseBitmap, matrix, new Paint());
        iv_after.setImageBitmap(afterBitmap);
    }

    /**
     * 图片移动
     */
    public static  void bitmapTranslate(Bitmap baseBitmap,float dx, float dy,ImageView iv_after) {
        // 需要根据移动的距离来创建图片的拷贝图大小
        Bitmap afterBitmap = Bitmap.createBitmap(
                (int) (baseBitmap.getWidth() + dx),
                (int) (baseBitmap.getHeight() + dy), baseBitmap.getConfig());
        Canvas canvas = new Canvas(afterBitmap);
        Matrix matrix = new Matrix();
        // 设置移动的距离
        matrix.setTranslate(dx, dy);
        canvas.drawBitmap(baseBitmap, matrix, new Paint());
        iv_after.setImageBitmap(afterBitmap);
    }

    /**
     * 倾斜图片
     */
    public static void bitmapSkew(Bitmap baseBitmap,float dx, float dy,ImageView iv_after) {
        // 根据图片的倾斜比例,计算变换后图片的大小,
        Bitmap afterBitmap = Bitmap.createBitmap(baseBitmap.getWidth()
                + (int) (baseBitmap.getWidth() * dx), baseBitmap.getHeight()
                + (int) (baseBitmap.getHeight() * dy), baseBitmap.getConfig());
        Canvas canvas = new Canvas(afterBitmap);
        Matrix matrix = new Matrix();
        // 设置图片倾斜的比例
        matrix.setSkew(dx, dy);
        canvas.drawBitmap(baseBitmap, matrix, new Paint());
        iv_after.setImageBitmap(afterBitmap);
    }

    public static void bitmapNoRed(Bitmap mBitmap,ImageView mIvNew){
        Bitmap bitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), mBitmap.getConfig());
        //去掉红色
       float[] mMatrix = new float[]{
               0.33f, 0.59f, 0.11f, 0, 0,
               0.33f, 0.59f, 0.11f, 0, 0,
               0.33f, 0.59f, 0.11f, 0, 0,
               0, 0, 0, 1, 0,
       };
        //色彩矩阵
        ColorMatrix colorMatrix = new ColorMatrix(mMatrix);
        //画板
        Canvas canvas = new Canvas(bitmap);
        //画笔
        Paint paint = new Paint();
        //给画笔设置颜色过滤器,里面使用色彩矩阵
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        //将mBitmap临摹到bitmap上,使用含有色彩矩阵的画笔
        canvas.drawBitmap(mBitmap, 0, 0, paint);
        mIvNew.setImageBitmap(bitmap);
    }
}
上一篇下一篇

猜你喜欢

热点阅读