a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁(yè) > 系統(tǒng) > Android > 正文

Android 圖片縮放與旋轉(zhuǎn)的實(shí)現(xiàn)詳解

2020-04-11 12:08:31
字體:
供稿:網(wǎng)友
本文使用Matrix實(shí)現(xiàn)Android實(shí)現(xiàn)圖片縮放與旋轉(zhuǎn)。示例代碼如下:
復(fù)制代碼 代碼如下:

package com.android.matrix;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;
/**
 * Android實(shí)現(xiàn)圖片縮放與旋轉(zhuǎn)。
 * @author Administrator
 *
 */
public class MatixActivity extends Activity {
    public void onCreate(Bundle icicle) {

        super.onCreate(icicle);

        setTitle("Android實(shí)現(xiàn)圖片縮放與旋轉(zhuǎn)。");
        LinearLayout linLayout = new LinearLayout(this);

        //加載需要操作的圖片,這里是一張圖片
        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.r);

        //獲取這個(gè)圖片的寬和高
        int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();

        //定義預(yù)轉(zhuǎn)換成的圖片的寬度和高度
        int newWidth = 200;
        int newHeight = 200;

        //計(jì)算縮放率,新尺寸除原始尺寸
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // 創(chuàng)建操作圖片用的matrix對(duì)象
        Matrix matrix = new Matrix();

        // 縮放圖片動(dòng)作
        matrix.postScale(scaleWidth, scaleHeight);

        //旋轉(zhuǎn)圖片 動(dòng)作
        matrix.postRotate(45);

        // 創(chuàng)建新的圖片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
        width, height, matrix, true);

        //將上面創(chuàng)建的Bitmap轉(zhuǎn)換成Drawable對(duì)象,使得其可以使用在ImageView, ImageButton中
        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

        //創(chuàng)建一個(gè)ImageView
        ImageView imageView = new ImageView(this);

        // 設(shè)置ImageView的圖片為上面轉(zhuǎn)換的圖片
        imageView.setImageDrawable(bmd);

        //將圖片居中顯示
        imageView.setScaleType(ScaleType.CENTER);

        //將ImageView添加到布局模板中
        linLayout.addView(imageView,
        new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
        )
        );

        // 設(shè)置為本activity的模板
        setContentView(linLayout);
     } 
}

上例是靜態(tài)地實(shí)現(xiàn)圖片縮放,下例中可以通過鼠標(biāo)滑輪和方向鍵實(shí)現(xiàn)圖片動(dòng)態(tài)的放大與縮小。
程序結(jié)構(gòu)如下圖:



Zoom.java文件中代碼:
復(fù)制代碼 代碼如下:

package com.android.zooming;
import android.view.View;
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.drawable.Drawable; 
import android.view.KeyEvent; 
public class Zoom extends View {
     private Drawable image; 
     private int zoomControler=20; 

     public Zoom(Context context){ 
         super(context); 
         image=context.getResources().getDrawable(R.drawable.x); 
         setFocusable(true); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
         // TODO Auto-generated method stub 
         super.onDraw(canvas); 
         //控制圖像的寬度和高度
         image.setBounds((getWidth()/2)-zoomControler, (getHeight()/2)-zoomControler, (getWidth()/2)+zoomControler, (getHeight()/2)+zoomControler); 
         image.draw(canvas); 
     } 

     @Override 
     public boolean onKeyDown(int keyCode, KeyEvent event) { 

             if(keyCode==KeyEvent.KEYCODE_DPAD_UP)//放大
                     zoomControler+=10; 

             if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN) //縮小
                     zoomControler-=10; 

             if(zoomControler<10) 
                     zoomControler=10; 

             invalidate(); 
             return true; 
     } 
}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 传媒av在线 | 国产精品久久久久久久久久东京 | 日本黄色一区二区 | 久久小视频 | 国产精品一区欧美 | 国产成人精品免高潮在线观看 | 日韩激情免费 | 精品国产乱码久久久久久1区2区 | 国产在线观看av | 狠狠色噜噜狠狠狠狠2018 | 成人av教育 | www.国产精品 | 国产成在线观看免费视频 | 国产区免费视频 | 国产成人精品久久 | 中文一区二区 | 国产在线小视频 | 日韩成人影院 | 在线国产91| av在线成人 | 国产小视频一区二区 | 久久精品久久久久久久久久久久久 | 亚洲自拍偷拍第一页 | 蜜桃久久久久久 | 91精品一区二区三区久久久久 | 中文字幕八区 | 国产一级特黄aaa大片 | 欧美精品在线观看一区二区 | 日韩亚洲视频在线观看 | 欧美一区二区三区在线看 | 中文字国产精久久无 | 亚洲毛片在线 | 国产精品视频久久久 | 久久99网 | 夜本色 | 久久国产精品无码网站 | 五月激情综合网 | 久久久久久99 | 久久国产精品99久久久久久老狼 | 亚洲骚片 | 精品无人乱码区1区2区3区 |