package com.example.test; 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.widget.ImageView; public class TestBitmap2 extends AppCompatActivity { private ImageView mIv1,mIv2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_bitmap2); mIv1= (ImageView) findViewById(R.id.iv_bit1); mIv2= (ImageView) findViewById(R.id.iv_bit2); Bitmap bitmap1= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher); mIv1.setImageBitmap(bitmap1); Bitmap bitmap2=Bitmap.createBitmap(bitmap1.getWidth(),bitmap1.getHeight(),bitmap1.getConfig()); Canvas canvas=new Canvas(bitmap2); Paint paint=new Paint(); paint.setColor(Color.BLACK); Matrix matrix=new Matrix(); //图片缩放 /* matrix.setValues(new float[]{ 0.5f,0,0, 0,0.5f,0, 0,0,1 });*/ //图片旋转 // matrix.setRotate(180,bitmap1.getWidth()/2,bitmap1.getHeight()/2); /* matrix.setRotate(15); paint.setAntiAlias(true);//消除锯齿*/ //图片平移 matrix.setTranslate(10,10);//水平和竖直方向移动10个像素 //镜子效果 /* matrix.setScale(-1,1); matrix.postTranslate(bitmap1.getWidth(),0);*/ //倒影效果 /* matrix.setScale(1,-1); matrix.postTranslate(0,bitmap1.getHeight()); canvas.drawBitmap(bitmap1,matrix,paint);*/ mIv2.setImageBitmap(bitmap2); } }
package com.example.test; 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.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; public class TestBitmap3 extends AppCompatActivity { private ImageView mIv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_bitmap3); mIv= (ImageView) findViewById(R.id.iv_bit3); //图片合成画布, 先画图片A,再画图片B Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.ic_guide_1); Bitmap alterBitmap=Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),bitmap.getConfig()); Canvas canvas=new Canvas(alterBitmap); Paint paint=new Paint(); paint.setColor(Color.BLACK); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DARKEN));//设置图形的混合模式 canvas.drawBitmap(bitmap,new Matrix(),paint); Bitmap ic_luncher=BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher); canvas.drawBitmap(ic_luncher,new Matrix(),paint); mIv.setImageBitmap(alterBitmap); } }