android中知道图片name时获取图片的简单方法

1. 图片放在sdcard中, 代码如下:
  Bitmap imageBitmap = BitmapFactory.decodeFile(path)  (path 是图片的路径,跟目录是/sdcard)

2. 图片在项目的res文件夹下面 代码如下:


  //得到application对象
  ApplicationInfo appInfo = getApplicationInfo();
  //得到该图片的id(name 是该图片的名字,"drawable" 是该图片存放的目录,appInfo.packageName是应用程序的包)
  int resID = getResources().getIdentifier(name, "drawable", appInfo.packageName);


  3. 图片放在src目录下 代码如下:
  String path = "com/timanetworks/jerome/activity/test.png"; //图片存放的路径
  InputStream is = getClassLoader().getResourceAsStream(path); //得到图片流

                                                                         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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值