Android 实现镜像效果

一、解析
—————————————————–

1、首先定义两张图片:
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.mm3);
第二张根据第一张生成
Bitmap modBm = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());

2、以生成的第二张图片做画板
Canvas canvas = new Canvas(modBm);

3、创建矩阵,并水平平移
matrix.setScale(-1, 1);
matrix.postTranslate(bm.getWidth(), 0);

4、使用矩阵绘制镜像图片
canvas.drawBitmap(bm, matrix, paint);

5、最后设置到imageview里

二、源码

/**
 * 镜子效果
 * @author chen.lin
 *
 */
public class MirrorImageActivity extends Activity {

    private ImageView imageView1;
    private ImageView imageView2;

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

    private void initViews() {
        imageView1 = (ImageView) findViewById(R.id.imageView1);
        imageView2 = (ImageView) findViewById(R.id.imageView2);

        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.mm3);
        imageView1.setImageBitmap(bm);

        Bitmap modBm = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());
        Canvas canvas = new Canvas(modBm);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);
        Matrix matrix = new Matrix();
        //matrix.setRotate(90, bm.getWidth()/2, bm.getHeight()/2);
        //matrix.setTranslate(20, 20);
        //镜子效果
        matrix.setScale(-1, 1);
        matrix.postTranslate(bm.getWidth(), 0);

        canvas.drawBitmap(bm, matrix, paint);
        imageView2.setImageBitmap(modBm);
    }
}

布局文件:
**

你可能感兴趣的:(android,基础,Android图片处理)