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

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類對圖片進行縮放和旋轉操作。

主站蜘蛛池模板: 如皋市| 乐陵市| 壤塘县| 芮城县| 应城市| 淮滨县| 绥棱县| 灵丘县| 朔州市| 枞阳县| 凤凰县| 云霄县| 聂荣县| 武义县| 乌兰县| 盘锦市| 阜南县| 鄂温| 樟树市| 隆子县| 怀仁县| 涟水县| 华安县| 南汇区| 尖扎县| 正定县| 彰化县| 莱西市| 建瓯市| 博野县| 和政县| 措勤县| 洞口县| 余干县| 阿瓦提县| 丹寨县| 张家港市| 白城市| 崇信县| 内丘县| 崇阳县|