Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android開發實例 圖片查看程序

Android開發實例 圖片查看程序

編輯:Android開發實例

  代碼如下:
  java:
  package snowfox.android;
  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.util.Log;

  import android.view.KeyEvent;

  import android.widget.ImageView;
  public class MainActivity extends Activity {

      /** Called when the activity is first created. */

          private ImageView v1;

          private int widthOrg, heightOrg, w;

          float scale = 1;

          float scaleWidth, scaleHeight;

          Bitmap OrgMap, newMap;

          BitmapDrawable bmd;

          Matrix matrix = new Matrix();

          @Override

      public void onCreate(Bundle savedInstanceState) {

          super.onCreate(savedInstanceState);

          setContentView(R.layout.main);

          v1 = (ImageView)findViewById(R.id.image01);

          OrgMap = BitmapFactory.decodeResource(getResources(), R.drawable.test);

          widthOrg = OrgMap.getWidth();

          heightOrg = OrgMap.getHeight();

          //確定圖片初始縮放比例,一般以適應屏幕寬度為准

          w = 320;       

          scale = (float)w/widthOrg;

            matrix = new Matrix();

          matrix.postScale(scale, scale);

          newMap = Bitmap.createBitmap(OrgMap, 0, 0, widthOrg, heightOrg, matrix, true);

                  bmd = new BitmapDrawable(newMap);

          v1.setImageDrawable(bmd);

      }

          @Override

          public boolean onKeyDown(int keyCode, KeyEvent event)

          {

                  //放大圖片

                  if(keyCode == KeyEvent.KEYCODE_VOLUME_UP)

                  {

                          //設置放大尺寸,若大於2倍,則停止放大,另外,小於1200是為了防止因圖片太大造成內存洩露

                          if(scale < 2.0 && w < 1200)

                            {

                                  scale += 0.1;

                                  matrix.reset();                                            //重置矩陣

                                  matrix.postScale(scale, scale);                //設置矩陣屬性

                                  //重新繪圖

                                  newMap = Bitmap.createBitmap(OrgMap, 0, 0, widthOrg, heightOrg, matrix, true);
                       

                                  //轉化為drawable圖像,使其可以顯示在imageview中

                                  bmd = new BitmapDrawable(newMap);

                                  v1.setImageDrawable(bmd);

                                  w = newMap.getWidth();

                                  int h = newMap.getHeight();

                                  Log.i("ta==========", w + " " +h);

                          }

                          return true;

                  }

                            //縮小圖片

                  if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)

                  {

                          //設置縮放尺寸,若小於0.1倍,則停止縮小

                          if(scale > 0.1)

                          {

                                  scale -= 0.05;

                                                          matrix.reset();                                                //重置矩陣

                                  matrix.postScale(scale, scale);                //設置矩陣屬性

                                //重新繪圖

                                  newMap = Bitmap.createBitmap(OrgMap, 0, 0, widthOrg, heightOrg, matrix, true);
                       

                                  //轉化為drawable圖像,使其可以顯示在imageview中

                          bmd = new BitmapDrawable(newMap);

                          v1.setImageDrawable(bmd);

                       

                          w = newMap.getWidth();

                          int h = newMap.getHeight();

                          Log.i("ta==========", w + " " +h);

                          }

                          return true;

                  }

                  else

                          return super.onKeyDown(keyCode, event);        //在其他情況下才調用super是為了屏蔽音量按鍵原來的功能,同時保證其他按鍵原功能可用

          }

  }

  xml:
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

      android:layout_width="fill_parent" android:layout_height="fill_parent"

      android:fillViewport="true">

      <HorizontalScrollView

              android:layout_width = "fill_parent"

              android:layout_height = "fill_parent"

              android:fillViewport = "true">

              <LinearLayout

                      android:gravity ="center"

                      android:orientation="horizontal"

                  android:layout_width="fill_parent" android:layout_height="fill_parent">

                  <ImageView

                          android:id = "@+id/image01"

                          android:layout_width="wrap_content"

                      android:layout_height="wrap_content"

                      android:scaleType ="fitCenter"/>

                     </LinearLayout>

      </HorizontalScrollView>

  </ScrollView>

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved