图片的画画板
布局文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 8 <Button 9 android:onClick="selectImage" 10 android:id="@+id/button1" 11 android:layout_width="fill_parent" 12 android:layout_height="wrap_content" 13 android:text="选择图片" /> 14 <Button 15 android:onClick="saveImage" 16 android:id="@+id/button1" 17 android:layout_width="fill_parent" 18 android:layout_height="wrap_content" 19 android:text="保存图片" /> 20 <ImageView 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:scaleType="centerInside" 24 android:id="@+id/iv" /> 25 </LinearLayout>
项目代码
1 package cn.itcast.paint; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.InputStream; 7 8 import android.app.Activity; 9 import android.content.Intent; 10 import android.graphics.Bitmap; 11 import android.graphics.BitmapFactory; 12 import android.graphics.Bitmap.CompressFormat; 13 import android.graphics.BitmapFactory.Options; 14 import android.graphics.Canvas; 15 import android.graphics.Color; 16 import android.graphics.Matrix; 17 import android.graphics.Paint; 18 import android.graphics.Rect; 19 import android.net.Uri; 20 import android.os.Bundle; 21 import android.os.Environment; 22 import android.view.Display; 23 import android.view.MotionEvent; 24 import android.view.View; 25 import android.view.View.OnTouchListener; 26 import android.view.WindowManager; 27 import android.widget.ImageView; 28 import android.widget.Toast; 29 30 public class DemoActivity extends Activity { 31 private ImageView iv; 32 private Bitmap alterBitmap; 33 private Canvas canvas; 34 private Paint paint; 35 private WindowManager wm; 36 private Display display; 37 38 @Override 39 public void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 setContentView(R.layout.main); 42 iv = (ImageView) findViewById(R.id.iv); 43 paint = new Paint(); 44 paint.setColor(Color.GREEN); 45 paint.setStrokeWidth(10); 46 wm = getWindowManager(); 47 //获取手机的屏幕宽高 48 display = wm.getDefaultDisplay(); 49 iv.setOnTouchListener(new OnTouchListener() { 50 int startx; 51 int starty; 52 @Override 53 public boolean onTouch(View v, MotionEvent event) { 54 switch (event.getAction()) { 55 case MotionEvent.ACTION_DOWN://手指第一次触摸屏幕 56 startx = (int) event.getX(); 57 starty = (int) event.getY(); 58 59 break; 60 case MotionEvent.ACTION_MOVE: //手指在imageview上中移动 61 int x = (int) event.getX(); 62 int y = (int) event.getY(); 63 canvas.drawLine(startx, starty, x, y, paint); 64 startx = (int) event.getX(); 65 starty = (int) event.getY(); 66 iv.invalidate(); 67 break; 68 } 69 70 71 72 return true; 73 } 74 }); 75 } 76 77 78 public void selectImage(View view){ 79 Intent intent = new Intent(); 80 intent.setAction(Intent.ACTION_PICK); 81 intent.addCategory("android.intent.category.DEFAULT"); 82 intent.setType("image/*"); 83 startActivityForResult(intent, 0); 84 } 85 86 87 @Override 88 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 89 try { 90 if (data != null) { 91 // 得到点击图片的uri 92 Uri uri = data.getData(); 93 //对 要加载的图片进行缩放 94 //1.得到图片的宽度和高度. 95 //由于我们只想得到图片的宽度和高度,这个时候 只需要解析图片的头信息 96 BitmapFactory.Options opts = new Options(); 97 opts.inJustDecodeBounds = true;//让 bimapfactory假的解析这个位图,只获取位图的边框信息 98 InputStream is = getContentResolver().openInputStream(uri); 99 BitmapFactory.decodeStream(is, new Rect(), opts); 100 101 int bitmapHeight = opts.outHeight; 102 int bitmapWidth = opts.outWidth; 103 104 int windowHeight = display.getHeight(); 105 int windowWidth = display.getWidth(); 106 //获取水平和垂直方向的缩放比 107 int scaley = bitmapHeight / windowHeight; 108 int scalex = bitmapWidth / windowWidth; 109 110 111 112 if(scalex>scaley&&scaley>1){ 113 opts.inSampleSize = scalex; 114 } 115 if(scaley>scalex&&scalex>1){ 116 opts.inSampleSize = scaley; 117 } 118 opts.inJustDecodeBounds = false;// 由于已经得到了缩放比例 ,让位图工厂真正的解析这个位图 119 // 由于前面 我们已经解析了这个输入流, 需要重新初始化这个输入流 120 is = getContentResolver().openInputStream(uri); 121 Bitmap bitmap = BitmapFactory.decodeStream(is, new Rect(), opts); 122 System.out.println("位图的高度:"+bitmap.getHeight()); 123 System.out.println("位图的宽度:"+bitmap.getWidth()); 124 125 126 alterBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); 127 128 canvas = new Canvas(alterBitmap); 129 130 canvas.drawBitmap(bitmap, new Matrix(), new Paint()); 131 132 //设置的是原图在内存中的拷贝 133 iv.setImageBitmap(alterBitmap); 134 } 135 } catch (Exception e) { 136 e.printStackTrace(); 137 } 138 super.onActivityResult(requestCode, resultCode, data); 139 } 140 141 public void saveImage(View view){ 142 try { 143 File file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg"); 144 FileOutputStream stream = new FileOutputStream(file); 145 alterBitmap.compress(CompressFormat.JPEG, 100, stream); 146 Toast.makeText(this, "保存成功", 1).show(); 147 // 模拟系统发送一个 sd卡挂载的广播 148 Intent intent = new Intent(); 149 intent.setAction(Intent.ACTION_MEDIA_MOUNTED); 150 intent.setData(Uri.parse("file:"+Environment.getExternalStorageDirectory().getAbsolutePath())); 151 sendBroadcast(intent); 152 } catch (FileNotFoundException e) { 153 Toast.makeText(this, "保存失败", 1).show(); 154 e.printStackTrace(); 155 } 156 } 157 }