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

本文介绍如何使用Android系统调用本地图片程序选择图片,并在图片上实现手势绘制功能。包括调用图片程序、图片处理及触屏事件处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一篇我们介绍了如何调用本机自带摄像头,这篇我们就接上一篇的,如何调用本机图片程序来选择图片,并在选择的图片上对手指的手势进行绘制,先来看图片

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

<?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>

接下来就是单击按钮调用本机的图片程序
//调用本机图片程序
		Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,
				android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		startActivityForResult(choosePictureIntent, 0);

下一步就是对选择的图片进行处理了

@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;
					}

				}

				bmpFactoryOptions.inJustDecodeBounds = false;
				//缩放后的图片
				bmp = BitmapFactory
						.decodeStream(
								getContentResolver().openInputStream(
										imageFileUri), null, bmpFactoryOptions);

				//创建一张新图片
				alteredBitmap = Bitmap.createBitmap(bmp.getWidth(),
						bmp.getHeight(), bmp.getConfig());

				canvas = new Canvas(alteredBitmap);
				paint = new Paint();
				paint.setColor(Color.GREEN);
				//调定画笔宽度
				paint.setStrokeWidth(5);
				matrix = new Matrix();
				canvas.drawBitmap(bmp, matrix, paint);

				choosenImageView.setImageBitmap(alteredBitmap);
				choosenImageView.setOnTouchListener(this);

			} catch (Exception e) {

			}

		}

	}
最后就是触屏事件了

float downx = 0;
	float downy = 0;
	float upx = 0;
	float upy = 0;

	//触屏事件
	@Override
	public boolean onTouch(View v, MotionEvent event) {

		int action = event.getAction();

		switch (action) {
		case MotionEvent.ACTION_DOWN:

			downx = event.getX();
			downy = event.getY();

			break;

		case MotionEvent.ACTION_MOVE:

			upx = event.getX();
			upy = event.getY();

			//画线条
			canvas.drawLine(downx, downy, upx, upy, paint);
			//刷新ImageView
			choosenImageView.invalidate();

			downx = upx;
			downy = upy;

			break;

		case MotionEvent.ACTION_UP:

			upx = event.getX();
			upy = event.getY();

			canvas.drawLine(downx, downy, upx, upy, paint);
			choosenImageView.invalidate();

			break;

		case MotionEvent.ACTION_CANCEL:

			break;

		default:
			break;
		}

		return true;
	}
其实代码不多,大家基本能看得懂吧,要源码的请发留言吧,也是抽空写一下自己学习的代码,希望对大家有帮助。




评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值