97干视频,99国产精品懂色,亚洲精品99久久久久中文字幕,伊人五月丁香综合AⅤ,国产精品成人免费999

  您的位置:華清遠(yuǎn)見教育科技集團(tuán) >> 新聞動(dòng)態(tài) >>Android資料>> Android 圖像縮放之bitmap類  
 
Android 圖像縮放之bitmap類
分享到:

在Android中,對(duì)圖像處理提供了很強(qiáng)的支持能力,縮放圖像是圖像處理中的一個(gè)常見功能,圖像旋轉(zhuǎn)也是如此,在Android中,專門實(shí)現(xiàn)了一個(gè)Matrix類來(lái)配合Bitmap類一起實(shí)現(xiàn)這類的特效,下面是Android中的一個(gè)實(shí)現(xiàn):

代碼:伸縮圖像
    public static Bitmap createScaledBitmap(Bitmap src, int dstWidth,int dstHeight, boolean filter)
      {
        Matrix m;
        synchronized (Bitmap.class)
            {
                m=sScaleMatr ix;
                sScaleMatrix =null;
            }
        if (m==null)
            {
                m=new Matrix();
            }
    final int width=src.getWidth ();        //獲取圖像寬
    final int height=src.getHeight ();        //獲取圖像高
    final float sx=dstWidth/(float) width;        //計(jì)算X軸伸縮因子
    final float sy=dstHeight/(float) height;        //計(jì)算Y軸伸縮因子
    m.setScale(sx, sy);        //設(shè)置 伸縮因子
    Bitmap b=Bitmap.createBitmap(src, 0, 0, width, height, m, filter);    //返回目的圖像
    synchronized (Bitmap.class)
        {
            if (sScaleMatrix==null)
                 {
                     sScaleMatrix=m;
                }
        }
        return b;
      }

在上面的例子中,會(huì)創(chuàng)建一個(gè)在X\Y軸上伸縮因子為sx、sy的圖像。具體的圖像創(chuàng)建是由createBitmap()方法來(lái) 完成的。

在Android中,目前支持多種伸縮類型:"matrix"、"fitStart"、"fitXY"、"fitCenter"、
"fitEnd"、"center"、"centerCrop"、"centerInside",如果希望在布局文件中將ImageView加載的圖片能顯示在固定的寬高內(nèi),方法如下:

< ImageView
        android:layout_width="match_parent"
        android:layout_height="1dip"
        android:scaleType="fitXY"
        android:gravity="fill_horizontal"
        android:src="@drawable/dialog_divider_horizontal_light"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"/>

下面是一個(gè)圖像縮放的具體例子。

代碼:圖像縮放
        package com.miaozl.test;
        import android.app.Activity;
        import android.graphics.Bitmap;
        import android.graphics.BitmapFactory;
        import android.graphics.Matrix;
        import android.os.Bundle;
        import android.widget.ImageView;
        public class TestActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Matrix m = new Matrix();
        Bitmap bitmap = BitmapFactory.decodeResource (getResources(), R.drawable.rabbit);
        ImageView src=(ImageView)findViewById(R.id.src);
        src.setImageBitmap(bitmap);
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int newWidth = 160;
        int newHeight = 120;
        float scaleWidth = ((float) newWidth)/width;
        float scaleHeight = ((float) newHeight)/height;
        m.setScale(scaleWidth, scaleHeight);
        ImageView dest=(ImageView)findViewById (R.id.dest);
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,height, m, true);
        dest.setImageBitmap(resizedBitmap);
            }
        }

下面是工程的布局文件main.xml的具體實(shí)現(xiàn):
        < ?xml version="1.0" encoding="utf-8"?>
        < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="fill_parent"             android:layout_height="fill_parent">
        < ImageView
            android:layout_width="wrap_content"< br />             android:layout_height="wrap_content"
            android:layout_gravity="center_horiz ontal"
            android:id="@+id/src"/>
        < ImageView
            android:layout_width="wrap_content"< br />             android:layout_height="wrap_content"
            android:layout_gravity="center_horiz ontal"
            android:scaleType="center"
            android:id="@+id/dest"/>
        < /LinearLayout>

下圖是對(duì)410×308的原始rabbit.jpg和縮放后的處理效果對(duì)比。

 更多相關(guān)文章

·Android 2D圖像處理基本接口
·Android HTML5視頻播放
·Android基于MediaPlayer的視頻播放
·Android 中如何使用SIP服務(wù)
·Android音頻混響特效的設(shè)置