018_01图片特效

  1 package com.example.day18_01picturewizad;
  2 
  3 import android.app.Activity;
  4 import android.graphics.Bitmap;
  5 import android.graphics.Bitmap.Config;
  6 import android.graphics.BitmapFactory;
  7 import android.graphics.Canvas;
  8 import android.graphics.Matrix;
  9 import android.graphics.Paint;
 10 import android.os.Bundle;
 11 import android.view.View;
 12 import android.widget.ImageView;
 13 
 14 public class MainActivity extends Activity {
 15 
 16     private ImageView iv_picture;
 17     private float degree;
 18 
 19     @Override
 20     protected void onCreate(Bundle savedInstanceState) {
 21         super.onCreate(savedInstanceState);
 22         setContentView(R.layout.activity_main);    
 23         iv_picture = (ImageView) findViewById(R.id.iv_picture);    
 24     }
 25 
 26     /**
 27      * 将原始图片放大或缩小
 28      * @param v
 29      */
 30     public void scale (View v){
 31             
 32         Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
 33         Bitmap blank_btimap =Bitmap.createBitmap(bitmap.getWidth()*2, bitmap.getHeight()*2, Config.ARGB_8888);
 34         Canvas canvas = new Canvas(blank_btimap);
 35         Paint paint = new Paint();
 36         
 37         //canvas.drawBitmap(bitmap, 0, 0, paint);
 38         //现在我们需要将原图 通过放大或者缩小变换,重新绘制到blank_bitmap上
 39         
 40         
 41         //matrix 矩阵
 42           Matrix matrix  = new Matrix();
 43           
 44           float[] values = {1, 0, 0,     //  表示向量x = 1x + 0y + 0z 
 45                             0, 1, 0,     //  表示向量y = 0x + 1y + 0z
 46                             0, 0, 1 };   //  表示向量z = 0x + 0y + 1z
 47  
 48           matrix.setValues(values);
 49           canvas.drawBitmap(bitmap, matrix, paint);        
 50 
 51              iv_picture.setImageBitmap(blank_btimap);
 52         
 53     }
 54      
 55     
 56     public void horizontal_reverse(View v){
 57         
 58         Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);        
 59         Bitmap blank_btimap =Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
 60         
 61         Canvas canvas = new Canvas(blank_btimap);//会画到blankbitmap上
 62         Paint paint = new Paint();        
 63         //canvas.drawBitmap(bitmap, 0, 0, paint);
 64         //现在我们需要将原图 通过放大或者缩小变换,重新绘制到blank_bitmap上
 65          //matrix 矩阵
 66           Matrix matrix  = new Matrix();
 67           
 68           float[] values = {-1, 0, 0,     //  表示向量x = 1x + 0y + 0z 
 69                              0, 1, 0,     //  表示向量y = 0x + 1y + 0z
 70                              0, 0, 1};   //  表示向量z = 0x + 0y + 1z
 71 
 72           matrix.setValues(values);
 73           matrix.postTranslate(bitmap.getWidth(), 0);
 74 
 75           canvas.drawBitmap(bitmap, matrix, paint);        
 76              iv_picture.setImageBitmap(blank_btimap);
 77     }
 78     
 79     
 80     public void vertical_reverse(View v){
 81            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);        
 82             Bitmap blank_btimap =Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
 83             
 84             Canvas canvas = new Canvas(blank_btimap);//会画到blankbitmap上
 85             Paint paint = new Paint();        
 86             //canvas.drawBitmap(bitmap, 0, 0, paint);
 87             //现在我们需要将原图 通过放大或者缩小变换,重新绘制到blank_bitmap上
 88              //matrix 矩阵
 89               Matrix matrix  = new Matrix();
 90               
 91               float[] values = { 1, 0, 0,     //  表示向量x = 1x + 0y + 0z 
 92                                  0, -1, 0,     //  表示向量y = 0x + 1y + 0z
 93                                  0, 0, 1};   //  表示向量z = 0x + 0y + 1z
 94 
 95               matrix.setValues(values);
 96               matrix.postTranslate(0, bitmap.getHeight());
 97 
 98               canvas.drawBitmap(bitmap, matrix, paint);        
 99                  iv_picture.setImageBitmap(blank_btimap);        
100     }
101     
102     
103     public void rotate(View v){
104         
105             Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);        
106             Bitmap blank_btimap =Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
107             
108             Canvas canvas = new Canvas(blank_btimap);//会画到blankbitmap上
109             Paint paint = new Paint();        
110             //canvas.drawBitmap(bitmap, 0, 0, paint);
111             //现在我们需要将原图 通过放大或者缩小变换,重新绘制到blank_bitmap上
112              //matrix 矩阵
113               Matrix matrix  = new Matrix();
114               
115               float[] values = { 1, 0, 0,     //  表示向量x = 1x + 0y + 0z 
116                                  0, 1, 0,     //  表示向量y = 0x + 1y + 0z
117                                  0, 0, 1};   //  表示向量z = 0x + 0y + 1z
118               matrix.setValues(values);
119               
120               degree = degree+20;
121               matrix.setRotate(degree, bitmap.getWidth()/2, bitmap.getHeight()/2);
122 
123               canvas.drawBitmap(bitmap, matrix, paint);        
124                  iv_picture.setImageBitmap(blank_btimap);
125     }
126     
127     
128     public void transform(View v){
129          Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);        
130          Bitmap blank_btimap =Bitmap.createBitmap(bitmap.getWidth()+10, bitmap.getHeight()+10, Config.ARGB_8888);
131         
132          Canvas canvas = new Canvas(blank_btimap);//会画到blankbitmap上
133          Paint paint = new Paint();        
134         //canvas.drawBitmap(bitmap, 0, 0, paint);
135         //现在我们需要将原图 通过放大或者缩小变换,重新绘制到blank_bitmap上
136          //matrix 矩阵
137           Matrix matrix  = new Matrix();          
138           float[] values = { 1, 0, 0,     //  表示向量x = 1x + 0y + 0z 
139                              0, 1, 0,     //  表示向量y = 0x + 1y + 0z
140                              0, 0, 1};   //  表示向量z = 0x + 0y + 1z
141           matrix.setValues(values);
142           matrix.postTranslate(10, 10);
143         
144           canvas.drawBitmap(bitmap, matrix, paint);        
145              iv_picture.setImageBitmap(blank_btimap);
146     }
147 }
物随心转,境由心造,一切烦恼皆由心生。
原文地址:https://www.cnblogs.com/woodrow2015/p/4542658.html