当前位置:操作系统 > 安卓/Android >>

Android[高级教程] Android程序调用本地图片并进行绘制

上一篇http://www.zzzyk.com/kf/201202/119329.html我们介绍了如何调用本机自带摄像头,这篇我们就接上一篇的,如何调用本机图片程序来选择图片,并在选择的图片上对手指的手势进行绘制,先来看图片

 \\
\

 



 

首先看一下布局,这里面只有一个按钮和一个图片


[html] <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="选取图片" />
 
 
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_launcher" />
 
 
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="选取图片" />


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_launcher" />


</LinearLayout>
接下来就是单击按钮调用本机的图片程序[java] //调用本机图片程序 
        Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(choosePictureIntent, 0);
//调用本机图片程序
  Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,
    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  startActivityForResult(choosePictureIntent, 0);
下一步就是对选择的图片进行处理了


[java]  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub 
        super.onActivityResult(requestCode, resultCode, data);
 
        if (resultCode == RESULT_OK) {
 
            //取得返回数据的Uri 
            Uri imageFileUri = data.getData();
            //取得当前显示区域 
            Display currentDisplay = getWindowManager().getDefaultDisplay();
 
            float dw = currentDisplay.getWidth();
            float dh = currentDisplay.getHeight();
 
            try {
 
                //对图片进行缩放 
                BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
                bmpFactoryOptions.inJustDecodeBounds = true;
                bmp = BitmapFactory
                        .decodeStream(
                                getContentResolver().openInputStream(
                                        imageFileUri), null, bmpFactoryOptions);
 
                int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight
                        / (float) dh);
                int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth
                        / (float) dw);
 
                if (heightRatio > 1 && widthRatio > 1) {
 
                    if (heightRatio > widthRatio) {
 
                        bmpFactoryOptions.inSampleSize = heightRatio;
                    } else {
                        bmpFactoryOptions.inSampleSize = widthRatio;
     &

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,