图片的缩放旋转和平移
Posted zhongyinghe
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片的缩放旋转和平移相关的知识,希望对你有一定的参考价值。
1、视图
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context=".MainActivity" > 7 8 <ImageView 9 android:id="@+id/iv_1" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 /> 13 14 <ImageView 15 android:id="@+id/iv_2" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 /> 19 20 </LinearLayout>
2、代码
1 package com.example.imageresize; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.graphics.Bitmap; 6 import android.graphics.BitmapFactory; 7 import android.graphics.Canvas; 8 import android.graphics.Color; 9 import android.graphics.Matrix; 10 import android.graphics.Paint; 11 import android.view.Menu; 12 import android.widget.ImageView; 13 14 public class MainActivity extends Activity { 15 16 private ImageView iv_1; 17 private ImageView iv_2; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 23 iv_1 = (ImageView) findViewById(R.id.iv_1); 24 iv_2 = (ImageView) findViewById(R.id.iv_2); 25 26 Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 27 iv_1.setImageBitmap(bitmap1); 28 29 Bitmap alterBitmap = Bitmap.createBitmap(bitmap1.getWidth() + 10, bitmap1.getHeight() + 10, bitmap1.getConfig()); 30 Canvas canvas = new Canvas(alterBitmap); 31 Paint paint = new Paint(); 32 paint.setColor(Color.BLACK); 33 Matrix matrix = new Matrix(); 34 /***************图片缩放代码start******************/ 35 //注意x轴和y轴的缩放 36 /* matrix.setValues(new float[]{ 37 2.0f,0,1, 38 0,1,0, 39 0,0,1 40 });*/ 41 //matrix.setScale(2.0f, 1); 42 /***************图片缩放代码end******************/ 43 //matrix.setRotate(180, bitmap1.getWidth()/2, bitmap1.getHeight()/2); 44 //matrix.setRotate(15); 45 /***********镜子效果start*************/ 46 matrix.setScale(-1, 1); 47 matrix.postTranslate(bitmap1.getWidth(), 0);//这样设置就是平移 48 /***********镜子效果end*************/ 49 //matrix.setTranslate(10, 10);//x和y轴移动10个像素 50 //以bitmap1为模板画进画布中 51 canvas.drawBitmap(bitmap1, matrix, paint); 52 iv_2.setImageBitmap(alterBitmap); 53 } 54 55 56 57 }
以上是关于图片的缩放旋转和平移的主要内容,如果未能解决你的问题,请参考以下文章