Android开发实现图片平移、缩放、倒影及旋转功能的方法

发布时间:2020-09-21 14:36:25 作者:CharlinGod
来源:脚本之家 阅读:272

本文实例讲述了Android开发实现图片平移、缩放、倒影及旋转功能的方法。分享给大家供大家参考,具体如下:

解析:

1)根据原来的图片创建新的图片

Bitmap modBm = Bitmap.createBitmap(bm.getWidth()+20, bm.getHeight()+20, bm.getConfig());

2)设置到画布

Canvas canvas = new Canvas(modBm);

3)使用矩阵进行平移…

Matrix matrix = new Matrix();
matrix.setRotate(90, bm.getWidth()/2, bm.getHeight()/2);

4)把改变后图片设置到

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

示例代码:

1、平移:

public class TranslateImageActivity 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.mm0);
    imageView1.setImageBitmap(bm);
    Bitmap modBm = Bitmap.createBitmap(bm.getWidth()+20, bm.getHeight()+20, 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.postTranslate(20, 20);
    canvas.drawBitmap(bm, matrix, paint);
    imageView2.setImageBitmap(modBm);
  }
}

2、缩放

public class ScaleImageActivity extends BaseActivity {
  private static final int DOUBLE = 2;
  private ImageView iv1, iv2;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scale_iamge);
    iv1 = (ImageView) findViewById(R.id.imageView1);
    iv2 = (ImageView) findViewById(R.id.imageView2);
    Bitmap b1 = BitmapFactory.decodeResource(getResources(), R.drawable.p1);
    iv1.setImageBitmap(b1);
    Bitmap b2 = Bitmap.createBitmap(b1.getWidth()*DOUBLE, b1.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(b2);
    Matrix matrix = new Matrix();
    matrix.setValues(new float[]{
        DOUBLE * 1.0f, 0,    0,//x
        0,       1.0f,  0,//y
        0,       0,    1.0f//z
    });
    canvas.drawBitmap(b1, matrix, new Paint());
    iv2.setImageBitmap(b2);
  }
}

3、倒影

public class ShadeImageActivity 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(0, bm.getHeight());
    canvas.drawBitmap(bm, matrix, paint);
    imageView2.setImageBitmap(modBm);
  }
}

4、旋转

public class RotateImageActivity 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.mm0);
    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);
    canvas.drawBitmap(bm, matrix, paint);
    imageView2.setImageBitmap(modBm);
  }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

推荐阅读:
  1. 关于Canvas的旋转和平移
  2. [IOS]图片的旋转和缩放

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android 图片 roi

上一篇:用Python生成我一面国旗头像

下一篇:在python安装bs4的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》