xiaoyan_12 2013-01-31 06:17 采纳率: 25%
浏览 4285

如何调整gallery中的图像大小?

第一个文件允许用户从gallery中选择一个图片,然后把图片传到一个activity中处理。问题是图像相对于手机屏幕来说太大了,所以只能看见图像的左上角,就像图像是被放大的。

Button useGallery = (Button)findViewById(R.id.loadfromgallery);
        useGallery.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                photoPickerIntent.setType("image/*");
                startActivityForResult(photoPickerIntent, SELECT_PHOTO); 
            }}) ;

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

        switch(requestCode) { 
        case SELECT_PHOTO:
            if(resultCode == RESULT_OK){  
                Uri selectedImage = imageReturnedIntent.getData();
                InputStream imageStream = null;
                try {
                    imageStream = getContentResolver().openInputStream(selectedImage);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                Intent intent = new Intent(mContext, DisplayUndistortedBitmapFromGalleryActivity.class);
                intent.setData(selectedImage);
                startActivity(intent);

                if(yourSelectedImage != null){
                Log.e(TAG,"pic ok");
                }else{
                     Log.e(TAG,"pic not ok");
                }
            }
        }

第二个文件是activity,从intent中接收到图片图像数据,然后把它放置在一个包含bitmap的URI中。

public class DisplayUndistortedBitmapFromGalleryActivity extends Activity {

    private static final String TAG = "*********DUBFGActivity";
    private Context mContext = this;
    Uri uri;
    private Bitmap mbitmap = null;

    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);


         uri = getIntent().getData();
        if(uri != null){
            Log.e(TAG, "uri ok");
        }else {
            Log.e(TAG, "uri not ok");
        }


        try {
              mbitmap = Media.getBitmap(getContentResolver(), uri);
             //setMbitmap(bitmap);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

第三个文件是activity的customview。下面这一行从activity中检索出bitmap并显示它。是否有精简bitmap的方法,让它符合屏幕的大小?

Bitmap bm = ((DisplayUndistortedBitmapFromGalleryActivity)getContext()).getMbitmap();
  • 写回答

1条回答

  • yongyong_21 2013-01-31 09:48
    关注

    我用过的一个最简单的方法:

    public class bitmaptest extends Activity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        LinearLayout linLayout = new LinearLayout(this);
    
        // load the origial BitMap (500 x 500 px)
        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
               R.drawable.android);
    
        int width = bitmapOrg.width();
        int height = bitmapOrg.height();
        int newWidth = 200;
        int newHeight = 200;
    
        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
    
        // createa matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
        // rotate the Bitmap
    
    
        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                          width, height, matrix, true);
    
        // make a Drawable from Bitmap to allow to set the BitMap
        // to the ImageView, ImageButton or what ever
        BitmapDrawable bmd = new BitmapDrawable
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 matlab求解平差
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办