android开发步步为营之83:android图片处理技术之二(通过Matrix移动缩放旋转错切)

        Android中matrix是一个3*3的矩阵,用来对图片进行处理,它可以与Bitmap或者Canvas一起使用。使用它能对图形进行移动、缩放、旋转、错切变换。直接上代码比较清晰。

        一、和Bitmap一起使用

      

btnScale = (Button) findViewById(R.id.btn_scale);
        //图片缩放
        btnScale.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmapSource = BitmapFactory.decodeResource(getResources(), R.mipmap.ab);
                //方法一:使用Bitmap类缩放图片
//                Bitmap bitmapNew = Bitmap.createScaledBitmap(bitmapSource, 200, 200 * bitmapSource.getHeight() / bitmapSource.getWidth(), true);
//                //Bitmap bitmapNew = Bitmap.createScaledBitmap(bitmapSource, 600, 600 * bitmapSource.getHeight() / bitmapSource.getWidth(), true);
//                Log.i(tag,"new w="+bitmapNew.getWidth()+",new h="+bitmapNew.getHeight());
//                ivNew.setImageBitmap(bitmapNew);
                //方法二:使用Matrix缩放图片
                Matrix matrix = new Matrix();
                //第一个参数是x轴的缩放比例,而第二个参数是y轴的缩放比例。
                matrix.postScale(1.5f,1f);
                Bitmap scaleBitmap = Bitmap.createBitmap(bitmapSource, 0, 0, bitmapSource.getWidth(), bitmapSource.getHeight(), matrix, false);
                ivNew.setImageBitmap(scaleBitmap);



            }
        });
        btnTranslate = (Button) findViewById(R.id.btn_translate);
        //图片平移
        btnTranslate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmapSource = BitmapFactory.decodeResource(getResources(), R.mipmap.ab);
                Matrix matrix = new Matrix();
                //第一个参数是x轴的缩放比例,而第二个参数是y轴的缩放比例。
                //matrix.postTranslate(200f,200f);
                //沿x轴翻转180度
                matrix.setScale(1, -1);
                matrix.postTranslate(0, bitmapSource.getHeight());
                Bitmap scaleBitmap = Bitmap.createBitmap(bitmapSource, 0, 0, bitmapSource.getWidth(), bitmapSource.getHeight(), matrix, false);
                ivNew.setImageBitmap(scaleBitmap);


            }
        });

        btnRotate = (Button) findViewById(R.id.btn_rotate);
        //图片旋转
        btnRotate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmapSource = BitmapFactory.decodeResource(getResources(), R.mipmap.ab);
                Matrix matrix = new Matrix();
                //正数表示顺时针旋转,负数表示逆时针旋转,默认绕(0,0)旋转
                //matrix.setRotate(15,bmp.getWidth()/2,bmp.getHeight()/2); 绕中心点旋转
                //matrix.postRotate((float) 90);
                //matrix.setRotate((float) 90);//一回事
                matrix.preRotate((float) 90);//

                Bitmap rotaBitmap = Bitmap.createBitmap(bitmapSource, 0, 0, bitmapSource.getWidth(), bitmapSource.getHeight(), matrix, false);
                ivNew.setImageBitmap(rotaBitmap);
            }
        });
        btnSkew = (Button) findViewById(R.id.btn_skew);
        //图片错切
        btnSkew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmapSource = BitmapFactory.decodeResource(getResources(), R.mipmap.ab);
                Matrix matrix = new Matrix();
                matrix.setSkew(0.5f,0);

                Bitmap skewBitmap = Bitmap.createBitmap(bitmapSource, 0, 0, bitmapSource.getWidth(), bitmapSource.getHeight(), matrix, false);
                ivNew.setImageBitmap(skewBitmap);
            }
        });

       二、和Canvas一起使用

package com.figo.study.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.figo.study.R;

/**
 * Created by figo on 15/11/15.
 */
public class MatrixView extends ImageView{

    public MatrixView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    private Context mContext;
    private Bitmap bitmap=null;
    private Matrix matrix;
    private MatrixView view;
    public MatrixView(Context context) {
        super(context);
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ab);
        matrix = new Matrix();
        mContext=context;

    }
    //重绘变换后的图片
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap, matrix, null);
        super.onDraw(canvas);
    }

    @Override
    public void setImageMatrix(Matrix matrix)
    {
        this.matrix.set(matrix);
        super.setImageMatrix(matrix);
    }

    public void setImageBitmap(Bitmap mBitmap)
    {
        bitmap=mBitmap;
    }

    public Bitmap getImageBitmap()
    {
        return bitmap;
    }
    //获取变换后的图片
    public Bitmap CreatNewPhoto() {
        Bitmap bitmapNew = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                Bitmap.Config.ARGB_8888); // 背景图片
        Canvas canvas = new Canvas(bitmapNew); // 新建画布
        canvas.drawBitmap(bitmapNew, matrix, null); // 画图片
        canvas.save(Canvas.ALL_SAVE_FLAG); // 保存画布
        canvas.restore();
        return bitmapNew;
    }
}

      外部通过调用

Matrix matrix = new Matrix();

// 1. 平移

matrix.postTranslate(view.getImageBitmap().getWidth(), view.getImageBitmap().getHeight());

// 在x方向平移view.getImageBitmap().getWidth(),在y轴方向view.getImageBitmap().getHeight()

view.setImageMatrix(matrix);

//触发view的onDraw方法重绘图片

view.invalidate();

//或者获取变换后的新图片

view.createNewPhoto();


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值