Android绘制进阶之六:触摸轨迹的绘制及图片的保存
因为很多代码前面五次进阶已经设计,在此不赘述。单列出核心代码。
第一部分:xml文件
一个按钮选择图片,一个按钮保存图片
代码如下:
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. <Button
8. android:id="@+id/pickImageBtn"
9. android:layout_width="wrap_content"
10. android:layout_height="wrap_content"
11. android:text="pickImage" />
12.
13. <ImageView
14. android:id="@+id/pickedImage"
15. android:layout_width="wrap_content"
16. android:layout_height="wrap_content"
17. android:src="@drawable/ic_launcher" />
18.
19. <Button
20. android:id="@+id/saveBtn"
21. android:layout_width="wrap_content"
22. android:layout_height="wrap_content"
23. android:text="Save" />
24.
25. </LinearLayout>
第二部分:初始化
代码如下:
1. public void onCreate(Bundle savedInstanceState) {
2. super.onCreate(savedInstanceState);
3. setContentView(R.layout.main);
4.
5. Button pickImageBtn = (Button) findViewById(R.id.pickImageBtn);
6. Button saveBtn = (Button) findViewById(R.id.saveBtn);
7. mImageView = (ImageView) findViewById(R.id.pickedImage);
8.
9.
10. pickImageBtn.setOnClickListener(this);
11. saveBtn.setOnClickListener(this);
12.
13.
14. }
第三部分:选择图片,监听Touch
代码如下:
1. public void onClick(View v) {
2. // TODO Auto-generated method stub
3. Log.d("bitmap", "has onClick");
4. switch (v.getId()) {
5. case R.id.pickImageBtn:
6. Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
7. startActivityForResult(intent, REQUEST_CODE);
8. break;
1. @Override
2. protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
3. // TODO Auto-generated method stub
4. super.onActivityResult(requestCode, resultCode, intent);
5. Log.d("bitmap", "requestCode is :" + requestCode);
6. if (resultCode == RESULT_OK) {
7. Log.d("bitmap", "has result ok");
8. Uri uri = intent.getData();
9.
10. int dw = getWindowManager().getDefaultDisplay().getWidth();
11. int dh = getWindowManager().getDefaultDisplay().getHeight();
12.
13. try {
14. BitmapFactory.Options opts = new BitmapFactory.Options();
15. opts.inJustDecodeBounds = true;//如果设置为true,本身不会返回
16. Bitmap chooseBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, opts);
17. int bw = opts.outWidth;//此时,chooseBitmap的值为null,但opts仍然获得其config
18. int bh = opts.outHeight;
19.
20. int widthRatio = (int) Math.ceil(bw / (float) dw);
21. int heightRatio = (int) Math.ceil(bh / (float) dh);
22.
23. if (widthRatio > 1 || heightRatio >1) {
24. if (widthRatio > heightRatio) {
25. opts.inSampleSize = widthRatio;//设置比例
26. } else {
27. opts.inSampleSize = heightRatio;
28. }
29. }
30. opts.inJustDecodeBounds = fa
补充:移动开发 , Android ,