您的位置:首页 > 移动开发 > Android开发

android学习笔记---图片拉伸和裁剪

2014-05-08 23:42 381 查看
package com.android.imageview;

import java.io.FileNotFoundException;

import java.net.URI;

import android.app.Activity;

import android.content.DialogInterface;

import android.content.DialogInterface.OnClickListener;

import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.net.Uri;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

public class ImageViews extends Activity implements

        android.view.View.OnClickListener {

    private Button selectbutton, catbutton;

    private ImageView imageView;

    private static final int IMAGE_SELECT = 1;

    private static final int IMAGE_CUT = 2;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        setContentView(R.layout.imageview);

        Button selButton = (Button) this.findViewById(R.id.select);

        Button cuButton = (Button) this.findViewById(R.id.cut);

        imageView = (ImageView) this.findViewById(R.id.imageview);

        selButton.setOnClickListener(this);

        cuButton.setOnClickListener(this);

    }

    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        // TODO Auto-generated method stub

        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {

            if (requestCode == IMAGE_SELECT) {

                Uri uri = data.getData();// 取得图片

                // 取得手机屏幕的高度,/2避免图片拉伸

                int dh = getWindowManager().getDefaultDisplay().getHeight() / 2;//屏高度

                int dw = getWindowManager().getDefaultDisplay().getWidth();//屏宽度

                // 设置factory参数对图片进行适屏缩放

                BitmapFactory.Options factory = new BitmapFactory.Options();

                factory.inJustDecodeBounds = true;

                try {

                    Bitmap bmp = BitmapFactory.decodeStream(

                            getContentResolver().openInputStream(uri), null,

                            factory);

                    int hRatio = (int) Math

                            .ceil(factory.outHeight / (float) dh);

                    int wRatio = (int) Math.ceil(factory.outWidth / (float) dw);

                    Log.d("TAG", "hRatio = " + hRatio + " wRatio = " + wRatio);

                    if (hRatio > 1 || wRatio > 1) {

                        if (hRatio > wRatio) {

                            factory.inSampleSize = hRatio;

                        } else {

                            factory.inSampleSize = wRatio;

                        }
                    }

                   //必须设置为false,否则得不到缩放后的图片

                    factory.inJustDecodeBounds = false;

                    bmp = BitmapFactory.decodeStream(getContentResolver()

                            .openInputStream(uri), null, factory);

                    imageView.setImageBitmap(bmp);

                } catch (FileNotFoundException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                }
            } else if (requestCode == IMAGE_CUT) {

//“data”参数必须与"return-data"保持一致

                Bitmap bmp = data.getParcelableExtra("data");

                imageView.setImageBitmap(bmp);

                ;

            }

        }

    }

    @Override

    public void onClick(View v) {

        // TODO Auto-generated method stub

        switch (v.getId()) {

        case R.id.select:

            Intent intent = new Intent(

                    Intent.ACTION_PICK,

                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(intent, IMAGE_SELECT);

            break;

        case R.id.cut:

            Intent intent2 = getImageCutIntent();

            startActivityForResult(intent2, IMAGE_CUT);

        default:

            break;

        }

    }

    private Intent getImageCutIntent() {

        // TODO Auto-generated method stub

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);

        // 所有类型的图片
        intent.setType("image/*");

//注意第二个参数是字符串类型

        intent.putExtra("crop", "true");// 允许滑动选择

        intent.putExtra("aspectX", 1);// 1:1比例进行裁剪

        intent.putExtra("aspectY", 1);

        intent.putExtra("outputX", 80);// 输出图片的大小
        intent.putExtra("outputY", 80);

//注意第一个参数,换做其他字符串都不行

        intent.putExtra("return-data", true);// 有返回值

        return intent;

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android bitmap imageview