ImageScale,imagescaletype

來源:互聯網
上載者:User

ImageScale,imagescaletype

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;    }    }

執行:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.