你的位置:首页 > 操作系统

[操作系统]ImageScale


package com.example.imagescale;import android.os.Bundle;import android.app.Activity;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.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity {  private ImageView iv1,iv2;  private Button bt,bt2,bt3,bt4;  private Bitmap bp;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    iv1=(ImageView)this.findViewById(R.id.imageView1);    iv2=(ImageView)this.findViewById(R.id.imageView2);    bt=(Button)this.findViewById(R.id.button1);    bt2=(Button)this.findViewById(R.id.button2);    bt3=(Button)this.findViewById(R.id.button3);    bt4=(Button)this.findViewById(R.id.button4);    bt.setOnClickListener(new OnClickListener() {        @Override        public void onClick(View arg0) {          // TODO Auto-generated method stub          bp = BitmapFactory.decodeResource(getResources(), R.drawable.xh);            //iv1.setImageBitmap(bp);            //对照图片            //以bp在高、宽决定Canvas画布的大小            Bitmap bp2 = Bitmap.createBitmap(bp.getWidth(), bp.getHeight(), bp.getConfig());//            Canvas canvas = new Canvas(bp2);            Paint paint = new Paint();            paint.setColor(Color.BLACK);            Matrix matrix = new Matrix();          matrix.setScale(0.5f, 1);          canvas.drawBitmap(bp, matrix, paint);          iv2.setImageBitmap(bp2);        }      });    bt2.setOnClickListener(new OnClickListener() {      @Override      public void onClick(View arg0) {        // TODO Auto-generated method stub        bp = BitmapFactory.decodeResource(getResources(), R.drawable.xh);          //iv1.setImageBitmap(bp);          //对照图片          //以bp在高、宽决定Canvas画布的大小          Bitmap bp2 = Bitmap.createBitmap(bp.getWidth(), bp.getHeight(), bp.getConfig());//          Canvas canvas = new Canvas(bp2);          Paint paint = new Paint();          paint.setColor(Color.BLACK);          Matrix matrix = new Matrix();          matrix.setScale(1, -1);          matrix.postTranslate(0, bp.getHeight());        canvas.drawBitmap(bp, matrix, paint);        iv2.setImageBitmap(bp2);      }    });    bt3.setOnClickListener(new OnClickListener() {      @Override      public void onClick(View arg0) {        // TODO Auto-generated method stub        bp = BitmapFactory.decodeResource(getResources(), R.drawable.xh);          //iv1.setImageBitmap(bp);          //对照图片          //以bp在高、宽决定Canvas画布的大小          Bitmap bp2 = Bitmap.createBitmap(bp.getWidth(), bp.getHeight(), bp.getConfig());//          Canvas canvas = new Canvas(bp2);          Paint paint = new Paint();          paint.setColor(Color.BLACK);          Matrix matrix = new Matrix();          matrix.setRotate(90, bp.getWidth()/2, bp.getHeight()/2);          canvas.drawBitmap(bp, matrix, paint);        iv2.setImageBitmap(bp2);      }    });    bt4.setOnClickListener(new OnClickListener() {      @Override      public void onClick(View arg0) {        // TODO Auto-generated method stub        bp = BitmapFactory.decodeResource(getResources(), R.drawable.xh);          //iv1.setImageBitmap(bp);          //对照图片          //以bp在高、宽决定Canvas画布的大小          Bitmap bp2 = Bitmap.createBitmap(bp.getWidth(), bp.getHeight(), bp.getConfig());//          Canvas canvas = new Canvas(bp2);          Paint paint = new Paint();          paint.setColor(Color.BLACK);          Matrix matrix = new Matrix();          matrix.setTranslate(100, 100);        canvas.drawBitmap(bp, matrix, paint);        iv2.setImageBitmap(bp2);      }    });        //原始图片        //bp = BitmapFactory.decodeResource(getResources(), R.drawable.xh);          //iv1.setImageBitmap(bp);          //对照图片          //以bp在高、宽决定Canvas画布的大小          //Bitmap bp2 = Bitmap.createBitmap(bp.getWidth(), bp.getHeight(), bp.getConfig());//          //Canvas canvas = new Canvas(bp2);          //Paint paint = new Paint();          //paint.setColor(Color.BLACK);          //Matrix matrix = new Matrix();          //1.缩放            //方法一:使用矩阵            /*matrix.setValues(new float[]{                0.5f,0,0,                0,1,0,                0,0,1            });*/            //方法二:            //matrix.setScale(0.5f, 1);          //2.旋转            //以图片左上角为中心旋转            //matrix.setRotate(15);            //以图片中心旋转            //matrix.setRotate(90, bp.getWidth()/2, bp.getHeight()/2);            //消除锯齿            //paint.setAntiAlias(true);          //3.平移            //matrix.setTranslate(50, 50);          //4.镜子效果            //matrix.setScale(-1, 1);            //matrix.postTranslate(bp.getWidth(), 0);          //5.倒影效果            //matrix.setScale(1, -1);            //matrix.postTranslate(0, bp.getHeight());                       //canvas.drawBitmap(bp, matrix, paint);            //iv2.setImageBitmap(bp2);      }  @Override  public boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu; this adds items to the action bar if it is present.    getMenuInflater().inflate(R.menu.main, menu);    return true;  }  }

执行效果图: