Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> ImageScale,imagescaletype

ImageScale,imagescaletype

編輯:關於android開發

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

執行效果圖:

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved