ImageView的使用,实现本地图片的适屏显示和裁剪功能。

调用系统的Intent的action 来实现图片的选取在imageview中根据屏幕进行适屏显示和图片裁剪后显示,并且可以实现裁剪功能。

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.imageview.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:text="选择图片" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_centerHorizontal="true"
        android:text="裁剪图片" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="82dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

activity中的代码如下,注释很详细,大家一看就能懂。

package com.example.imageview;

import java.io.IOError;

import android.R.anim;
import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {
	private Button button1, button2;
	private ImageView imageView;
	// 声明两个静态的整型变量,主要是用于意图的返回的标志。
	private static final int IMAGE_SELECT = 1;// 选择图片
	private static final int IMAGE_CUT = 2;// 裁剪图片。

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button1 = (Button) this.findViewById(R.id.button1);
		button2 = (Button) this.findViewById(R.id.button2);
		imageView = (ImageView) this.findViewById(R.id.imageView1);
		button1.setOnClickListener(this);
		button2.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();//获得图片路径。
				int dw = getWindowManager().getDefaultDisplay().getWidth();
				int dh = getWindowManager().getDefaultDisplay().getHeight() / 2;
				try {
					//实现对图片裁剪的类,是一个匿名内部类。
					BitmapFactory.Options options = new BitmapFactory.Options();
					options.inJustDecodeBounds = true;// 设置为真,允许查询图片不是按照像素分配给内存。
					Bitmap bitmap = BitmapFactory.decodeStream(
							getContentResolver().openInputStream(uri), null,
							options);
					// 对图片的宽度和高度对应手机的屏幕进行匹配。
					int hRatio = (int) Math
							.ceil(options.outHeight / (float) dh);
					// 如果大于1表示图片的高度大于手机屏幕的高度。ceil向下取整。
					int wRatio = (int) Math.ceil(options.outWidth / (float) dw);
					// 如果大于1 表示图片的宽度大于手机屏幕的宽度。
					// 缩放到1/radio的尺寸和1/radio^2像素。
					if (hRatio > 1 || wRatio > 1) {
						if (hRatio > wRatio) {// 如果高度大于宽度,以高度为准。
							options.inSampleSize = hRatio;
						} else {
							options.inSampleSize = wRatio;
						}
					}
					options.inJustDecodeBounds = false;
					bitmap = BitmapFactory.decodeStream(getContentResolver()
							.openInputStream(uri), null, options);
					imageView.setImageBitmap(bitmap);
				} catch (Exception e) {
					// TODO: handle exception
				}
			} else if (requestCode == IMAGE_CUT) {
				Bitmap bitmap = data.getParcelableExtra("data");
				imageView.setImageBitmap(bitmap);
			}
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button1:
			// 获取图片.
			Intent it = new Intent(
					Intent.ACTION_PICK,
					android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			startActivityForResult(it, IMAGE_SELECT);
			break;

		case R.id.button2:
			// 图片裁剪.
			Intent intent = getImageClipIntent();
			startActivityForResult(intent, IMAGE_CUT);
			break;
		}
	}

	private Intent getImageClipIntent() {
		// 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("outputX", 80);
		intent.putExtra("return-data", true);
		return intent;
	}

}

这个代码可以下载,点击打开链接http://download.csdn.net/detail/fengzy1990/7797219



下面是Bitmap与Imageview的显示的应用,

打开的是本地文件

imageView = (ImageView)findViewById(R.id.imageView);
String path = "sdcard/test.jpg";
File file = new File(path);
if (file.exists()) {
        Bitmap bmp = BitmapFactory.decodeFile(path);
        imageView.setImageBitmap(bmp);
}


Android用ImageView显示本地和网上的图片

ImageView是Android程序中经常用到的组件,它将一个图片显示到屏幕上。
在UI xml定义一个ImageView如下:


public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.myimage);
     ImageView image1 = (ImageView) findViewById(R.myImage.image);
     //Bitmap bitmap = getLoacalBitmap("/aa/aa.jpg"); //从本地取图片
     Bitmap bitmap =
getHttpBitmap("http://blog.3gstdy.com/wp-content/themes/twentyten/images/headers/path.jpg");
//从网上取图片
     image1 .setImageBitmap(bitmap);	//设置Bitmap
}
/**
* 加载本地图片
* http://bbs.3gstdy.com
* @param url
* @return
*/
public static Bitmap getLoacalBitmap(String url) {
     try {
          FileInputStream fis = new FileInputStream(url);
          return BitmapFactory.decodeStream(fis);
     } catch (FileNotFoundException e) {
          e.printStackTrace();
          return null;
     }
}
/**
* 从服务器取图片
*http://bbs.3gstdy.com
* @param url
* @return
*/
public static Bitmap getHttpBitmap(String url) {
     URL myFileUrl = null;
     Bitmap bitmap = null;
     try {
          Log.d(TAG, url);
          myFileUrl = new URL(url);
     } catch (MalformedURLException e) {
          e.printStackTrace();
     }
     try {
          HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
          conn.setConnectTimeout(0);
          conn.setDoInput(true);
          conn.connect();
          InputStream is = conn.getInputStream();
          bitmap = BitmapFactory.decodeStream(is);
          is.close();
     } catch (IOException e) {
          e.printStackTrace();
     }
     return bitmap;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值