美文网首页
调用内置的Gallery回传两张图片并合为一体

调用内置的Gallery回传两张图片并合为一体

作者: 蛋蛋不哭 | 来源:发表于2016-04-18 21:50 被阅读42次

布局文件

    <Button
         android:id="@+id/gallery"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="打开画廊_1" />
      <Button
         android:id="@+id/gallery_1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="打开画廊_2" />

     <ImageView
         android:id="@+id/img"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         />

代码文件

public class GalleryActivity extends Activity implements OnClickListener{

    private Button gallery, gallery_1;
    private ImageView img;
    private Bitmap newbmp_1, newbmp_2;
    private boolean onebmp = false;
    private boolean twobmp = false;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        iviview();
    }

    private void iviview() {
        gallery = (Button) findViewById(R.id.gallery);
        gallery_1 = (Button) findViewById(R.id.gallery_1);
        img = (ImageView) findViewById(R.id.img);
        gallery.setOnClickListener(this);
        gallery_1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int whlie = -1;
        if (v == gallery) {
            whlie = 1;
        } else if (v == gallery_1) {
            whlie = 2;
        }
        Intent i = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, whlie);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            // 获得地址
            Uri imguri = data.getData();

            if (requestCode == 1) {
                newbmp_1 = loadbmp(imguri);
                onebmp = true;
            } else if (requestCode == 2) {
                newbmp_2 = loadbmp(imguri);
                twobmp = true;
            }
            // 合成
            if (onebmp && twobmp) {
                // 在位图上绘制一张图片
                Bitmap upbmp = Bitmap.createBitmap(newbmp_1.getWidth(),
                        newbmp_1.getHeight(), newbmp_1.getConfig());
                // 我们创建一张画布
                Canvas cv = new Canvas(upbmp);
                // 我们创建一个笔
                Paint paint = new Paint();
                // 我们要用笔进行绘制1
                cv.drawBitmap(newbmp_1, 0, 0, paint);
                // 修改画笔
                paint.setXfermode(new PorterDuffXfermode(
                        android.graphics.PorterDuff.Mode.MULTIPLY));
                // 我们要用笔进行绘制2
                cv.drawBitmap(newbmp_2, 0, 0, paint);

                img.setImageBitmap(upbmp);
            }
        }
    }

    //返回处理过后的bmp
    private Bitmap loadbmp(Uri uri) {
        Bitmap bmp = null;
        // 获得模拟器的宽和高
        Display imgdp = getWindowManager().getDefaultDisplay();
        int wd = imgdp.getWidth();
        int hd = imgdp.getHeight();
        // 我们要获得 图片的尺寸,如果图片很大 可能会导致图片溢出
        // 加载图片并非是图片本身
        BitmapFactory.Options bo = new BitmapFactory.Options();
        bo.inJustDecodeBounds = true;
        // 这里的流需要转换
        try {
            bmp = BitmapFactory.decodeStream(getContentResolver()
                    .openInputStream(uri), null, bo);
            // 现在我们获得图片与模拟器的比例
            int wdbili = (int) Math.ceil((float) bo.outWidth / (float) wd);
            int hdbili = (int) Math.ceil((float) bo.outHeight / (float) hd);
            // 判断比例大于1
            if (wdbili > 1 || hdbili > 1) {
                // 我们判断比例大小
                if (wdbili > hdbili) {
                    bo.inSampleSize = wdbili;
                } else {
                    bo.inSampleSize = hdbili;
                }

                // 处理完之后 我们还要从新加载图片
                bo.inJustDecodeBounds = false;
                bmp = BitmapFactory.decodeStream(getContentResolver()
                        .openInputStream(uri), null, bo);
                // 在位图上绘制一张图片
                // Bitmap upbmp = Bitmap.createBitmap(bmp.getWidth(),
                // bmp.getHeight(), bmp.getConfig());
                // 我们创建一张画布
                // Canvas cv = new Canvas(upbmp);
                // 我们创建一个笔
                // Paint paint = new Paint();

                // 图片颜色饱和度
                // ColorMatrix cm = new ColorMatrix();
                // cm.set(new float[]{
                // 1,0,0,0,0,
                // 0,1,0,0,0,
                // 0,0,1,0,0,
                // 0,0,0,1,0
                // });
                // paint.setColorFilter(new ColorMatrixColorFilter(cm));

                // 我们要用笔进行绘制
                // cv.drawBitmap(bmp, 0, 0, paint);
                // 进行显示
                // img.setImageBitmap(bmp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bmp;
    }
}

相关文章

网友评论

      本文标题:调用内置的Gallery回传两张图片并合为一体

      本文链接:https://www.haomeiwen.com/subject/yhpalttx.html