官术网_书友最值得收藏!

4.6 Bitmap

Bitmap稱為點陣圖像或繪制圖像,是由稱作像素(圖片元素)的單個點組成的,這些點通過不同的排列和染色以構成圖樣。Bitmap是Android系統中圖像處理最重要的類之一,用它可以獲取圖像文件信息,對圖像進行剪切、旋轉、縮放等操作,并可以將圖像保存成特定格式的文件。Bitmap位于android.graphics包中,Bitmap不提供對外的構造方法,只能通過BitmapFactory類進行實例化。利用BitmapFactory的decodeFile方法可以從特定文件中獲取Bitmap對象,也可以使用decodeResource()從特定的圖片資源中獲取Bitmap對象。

實例BitmapDemo從資源文件中創建Bitmap對象,并對其進行一些操作,運行效果如圖4.40所示。

圖4.40 Bitmap對象的效果

其對應布局文件Main.xml內容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
       <SeekBar
            android:id="@+id/seekBarId"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />



       <ImageView
            android:id="@+id/imageview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/im01" />



    </LinearLayout>

BitmapDemoActivity.Java代碼如下:

    package introduction.android.bitmapDemo;



    import com.sie.bitmapdemo.R;
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Matrix;
    import android.os.Bundle;
    import android.widget.ImageView;
    import android.widget.SeekBar;
    import android.widget.SeekBar.OnSeekBarChangeListener;
    import android.widget.TextView;



    public class BitmapDemoActivity extends Activity
    {
        ImageView myImageView;
        Bitmap myBmp, newBmp;
        int bmpWidth, bmpHeight;
        SeekBar seekbarRotate;
        float rotAngle;



        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            myImageView=(ImageView)findViewById(R.id.imageview);
            // 由Resource載入圖片
            myBmp=BitmapFactory.decodeResource(getResources(), R.drawable.im01);
            bmpWidth=myBmp.getWidth();
            bmpHeight=myBmp.getHeight();
            // 實例化matrix
            Matrix matrix=new Matrix();
            //設定Matrix屬性 x,y縮放比例為1.5
            matrix.postScale(1.5F, 1.5F);
            //順時針旋轉45度
            matrix.postRotate(45.0F);
            newBmp=Bitmap.createBitmap(myBmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
            seekbarRotate=(SeekBar)findViewById(R.id.seekBarId);
            seekbarRotate.setOnSeekBarChangeListener(onRotate);
        }
        private SeekBar.OnSeekBarChangeListener onRotate=new SeekBar.OnSeekBarChangeListener(){



            public void onStopTrackingTouch(SeekBar seekBar)
            {
                // TODO Auto-generated method stub



            }



            public void onStartTrackingTouch(SeekBar seekBar)
            {
                // TODO Auto-generated method stub



            }



            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser)
            {
                // TODO Auto-generated method stub
                Matrix m=new Matrix();
                m.postRotate((float)progress*3.6F);
                newBmp=Bitmap.createBitmap(myBmp, 0, 0, bmpWidth, bmpHeight, m, true);
                myImageView.setImageBitmap(newBmp);
            }
        };
    }

本實例實現了拖動進度條圖片旋轉的效果。使用BitmapFactory從資源中載入圖片,并獲取圖片的寬和高,之后使用Matrix類對圖片進行縮放和旋轉操作。

主站蜘蛛池模板: 天津市| 社旗县| 陇川县| 徐闻县| 哈密市| 岑溪市| 五台县| 宝丰县| 江孜县| 长海县| 和林格尔县| 霍城县| 青岛市| 富蕴县| 拉萨市| 比如县| 塔城市| 湾仔区| 馆陶县| 洪洞县| 定襄县| 科技| 台南县| 米泉市| 鹤山市| 日照市| 剑川县| 闽侯县| 四子王旗| 屯留县| 比如县| 西丰县| 清徐县| 棋牌| 延吉市| 大连市| 萨迦县| 华容县| 马山县| 长治县| 钦州市|