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

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

主站蜘蛛池模板: 公主岭市| 萍乡市| 贡觉县| 仪陇县| 车险| 石阡县| 金华市| 赤水市| 华安县| 汝南县| 湘潭县| 花莲市| 古浪县| 禄丰县| 砚山县| 璧山县| 四会市| 孟津县| 临湘市| 闵行区| 策勒县| 郁南县| 池州市| 六枝特区| 罗田县| 法库县| 濮阳市| 屏东市| 施秉县| 黔西| 获嘉县| 永宁县| 乌鲁木齐县| 尼勒克县| 伊通| 微山县| 汉中市| 若尔盖县| 涡阳县| 峨边| 通山县|