效果:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2QvwVe0lmdhJ3ZvwFM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2LcZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39zM3kjNwUTNwITNygDM1EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
布局代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<ImageView
android:id="@+id/iv_src"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/iv_copy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout></span>
MainActivity.java
package com.example.cn;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.Menu;
import android.widget.ImageView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String path = Environment.getExternalStorageDirectory() + "/" + "3.jpg";
//這個對象是隻讀的
Bitmap bmSrc = BitmapFactory.decodeFile(path);
//建立圖檔副本
//1.在記憶體中建立一個與原圖一模一樣大小的bitmap對象,建立與原圖大小一緻的白紙
Bitmap bmCopy = Bitmap.createBitmap(bmSrc.getWidth(), bmSrc.getHeight(), bmSrc.getConfig());
//2.建立畫筆對象
Paint paint = new Paint();
//3.建立畫闆對象,把白紙鋪在畫闆上
Canvas canvas = new Canvas(bmCopy);
//4.開始作畫,把原圖的内容繪制在白紙上
Matrix mt = new Matrix();
//平移
// mt.setTranslate(20, 40);
//縮放
//sx:水準方向的縮放比例
//sy:豎直方向的縮放比例
// mt.setScale(0.5f, 0.5f);
// mt.setScale(0.5f, 0.5f, bmCopy.getWidth() / 2, bmCopy.getHeight() / 2);
//旋轉
// mt.setRotate(45, bmCopy.getWidth() / 2, bmCopy.getHeight() / 2);
//鏡面
mt.setScale(-1, 1);
mt.postTranslate(bmCopy.getWidth(), 0);
//倒影
// mt.setScale(1, -1);
// mt.postTranslate(0, bmCopy.getHeight());
canvas.drawBitmap(bmSrc, mt, paint);
ImageView iv_src = (ImageView) findViewById(R.id.iv_src);
ImageView iv_copy = (ImageView) findViewById(R.id.iv_copy);
iv_src.setImageBitmap(bmSrc);
iv_copy.setImageBitmap(bmCopy);
}
}
</span>