DaoYingActivity.java package com.example.rootimage;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Matrix;import android.graphics.Pain
package com.example.rootimage; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; public class DaoYingActivity extends AppCompatActivity { private ImageView YuanTu,change; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dao_ying); YuanTu=findViewById(R.id.YuanTu); change=findViewById(R.id.change); } public void DaoYing(View view){ Bitmap SrcBitmap = BitmapFactory.decodeFile("mnt/sdcard/img2.png");//插入文件路径 YuanTu.setImageBitmap(SrcBitmap); Bitmap CopyBitmap = Bitmap.createBitmap(SrcBitmap.getWidth(), SrcBitmap.getHeight(), SrcBitmap.getConfig());//临摹原图 Canvas canvas = new Canvas(CopyBitmap);//创建画布 Paint paint=new Paint();//创建画笔 paint.setColor(Color.BLUE); Matrix matrix=new Matrix();//matrix是矩阵的意思 matrix.setScale(1,-1);//原图横坐标不变,纵坐标取反 matrix.postTranslate(0,SrcBitmap.getHeight());//取反后在纵向移动原图的高 canvas.drawBitmap(SrcBitmap,matrix,paint); change.setImageBitmap(CopyBitmap); } }MirrorActivity.java
package com.example.rootimage; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.webkit.WebView; import android.widget.ImageView; public class MirrorActivity extends AppCompatActivity { private ImageView YuanTu,change; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mirror); YuanTu= findViewById(R.id.YuanTu); change=findViewById(R.id.change); } public void mirror(View view){ Bitmap SrcBitmap = BitmapFactory.decodeFile("mnt/sdcard/img3.png"); YuanTu.setImageBitmap(SrcBitmap); Bitmap mirrorBitmap = Bitmap.createBitmap(SrcBitmap.getWidth(), SrcBitmap.getHeight(), SrcBitmap.getConfig()); Canvas canvas=new Canvas(mirrorBitmap); Paint paint=new Paint(); paint.setColor(Color.BLACK); Matrix matrix=new Matrix(); matrix.setScale(-1,1); matrix.postTranslate(SrcBitmap.getWidth(),0);//实现镜像效果,postTranslate方法在原有变换上再加以变换。 canvas.drawBitmap(SrcBitmap,matrix,paint); change.setImageBitmap(mirrorBitmap); } }activity_dao_ying.xml
activity_mirror.xml