Matrix图片操作

先写一个自定义view

private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Matrix matrix = new Matrix();
private Bitmap bitmap;
public MainImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
}
//平移方法
public void translationBitmap(){
    matrix.reset();
    matrix.preTranslate(100f,100f);
    invalidate();
}
//缩放方法
public void scallBitmap(){
    matrix.reset();
    matrix.preScale(2f,2f,bitmap.getWidth()/2,bitmap.getHeight()/2);
    invalidate();
}
//旋转方法
public void rotateBitmap(){
    matrix.reset();
    matrix.preRotate(180,bitmap.getWidth()/2,bitmap.getHeight()/2);
    invalidate();
}
public void skewBitmap(){
    matrix.reset();
    matrix.preSkew(2,2);
    invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawBitmap(bitmap,matrix,paint);
    super.onDraw(canvas);
}

布局文件

<Button
    android:text="平移"
    android:id="@+id/btn1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<Button
    android:text="缩放"
    android:id="@+id/btn2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<Button
    android:text="旋转"
    android:id="@+id/btn3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<Button
    android:text="错切"
    android:id="@+id/btn4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<Button
    android:text="水饮"
    android:id="@+id/btn5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<com.example.matrixdemo.MainImageView
    android:id="@+id/img"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
private MainImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    img = findViewById(R.id.img);
    findViewById(R.id.btn1).setOnClickListener(this);
    findViewById(R.id.btn2).setOnClickListener(this);
    findViewById(R.id.btn3).setOnClickListener(this);
    findViewById(R.id.btn4).setOnClickListener(this);
    findViewById(R.id.btn5).setOnClickListener(this);

}

@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.btn1:
            img.translationBitmap();
            break;
        case R.id.btn2:
            img.scallBitmap();
            break;
        case R.id.btn3:
            img.rotateBitmap();
            break;
        case R.id.btn4:
            img.skewBitmap();
            break;
        case R.id.btn5:
            img.setImageBitmap(buildBitmap(
                    BitmapFactory.decodeResource(getResources(),R.mipmap.a8)
                    ,BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)));
            break;
    }

}
public Bitmap buildBitmap(Bitmap old,Bitmap now){
    Bitmap bitmap = null;
    //ARGB_8888->4字节 ARGB_4444 RGB_565 ->2字节
    //RGB->JPG
    //ARGB->PNG->存在透明通道
    bitmap = Bitmap.createBitmap(old.getWidth(),
            old.getHeight(),Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(old,0,0,null);
    canvas.drawBitmap(now,10,10,null);
    canvas.save();
    return bitmap;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值