热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

从具有确切位置的两个imageView创建位图-CreatebitmapfromtwoimageViewwithexactlocation

MyTarget:Getonephotofromcameraorgallery,thensetittoimageViewA从相机或图库中获取一张照片,然后将其设置

My Target:

  • Get one photo from camera or gallery, then set it to imageViewA
  • 从相机或图库中获取一张照片,然后将其设置为imageViewA

  • Add a draggable mask imageViewB on top of imageViewA
  • 在imageViewA上添加一个可拖动的蒙版imageViewB

  • Users drag the mask imageViewB to anywhere in imageViewA
  • 用户将遮罩imageViewB拖动到imageViewA中的任何位置

  • Users then click the save button
  • 然后用户单击“保存”按钮

  • A combined bitmap of imageViewA and imageViewB should be created
  • 应创建imageViewA和imageViewB的组合位图

My Problem:

Everything works fine, except that the imageViewB in the combined bitmap is not positioned appropriately (see the attached picture) enter image description here

一切正常,除了组合位图中的imageViewB没有正确定位(参见附图)

Code (MainActivity.java):

public class MainActivity extends ActionBarActivity {
Bitmap bmMask = null;
Bitmap bmOriginal =  null;
Bitmap bmCombined = null;

ImageView normalImgView;
ImageView maskImgView;
ImageView combinedImgView;

static final int REQUEST_TAKE_PHOTO = 55;
static final int REQUEST_LOAD_IMAGE = 60;
String currentImagePath;
static final String TAG = "mover";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    combinedImgView = (ImageView) findViewById(R.id.combinedImgView);
    normalImgView = (ImageView) findViewById(R.id.normalImgView);

    maskImgView = (ImageView) findViewById(R.id.maskImgView);
    bmMask = ((BitmapDrawable) maskImgView.getDrawable()).getBitmap();


    Button addBtn = (Button) findViewById(R.id.addBtn);
    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            bmCombined = ProcessingBitmap();
            if(bmCombined!=null){
                combinedImgView.setImageBitmap(bmCombined);
            }
            else {
                Log.d(MainActivity.TAG, "combined bm is null");
            }
        }
    });

    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(MainActivity.TAG, "clicked");
            getImageFromCameraIntent();
        }
    });

    MultiTouchListener mTouchListener = new MultiTouchListener();
    mTouchListener.isRotateEnabled = false;
   //        mTouchListener.isTranslateEnabled = false;
    mTouchListener.isScaleEnabled = false;
    maskImgView.setOnTouchListener(mTouchListener);
}

// take image from camera
private void getImageFromGalleryIntent() {
    Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, REQUEST_LOAD_IMAGE);
}

// take image from camera
private void getImageFromCameraIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(this.getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

// create a image file for storing full size image taken from camera
private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    currentImagePath = image.getAbsolutePath();
    Log.d(MainActivity.TAG, "photo path: " + currentImagePath);

    return image;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_OK) {
        // photo returned from camera
        // update UI
        updatePhotoView(currentImagePath);

    } else if (requestCode == REQUEST_LOAD_IMAGE && resultCode == Activity.RESULT_OK) {
        // photo returned from gallery
        // update UI
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = this.getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        updatePhotoView(picturePath);

    } else {
        Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
    }
}

// scale down the image, then display it to image view
private void updatePhotoView(String photoPath) {


    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptiOns= new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(photoPath, bmOptions);


    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(photoPath, bmOptions);
    bmOriginal = bitmap;
    normalImgView.setImageBitmap(bmOriginal);
}

private Bitmap ProcessingBitmap(){

    Bitmap newBitmap = null;

    int w;
    w = bmOriginal.getWidth();

    int h;
    h = bmOriginal.getHeight();

    Bitmap.Config cOnfig= bmOriginal.getConfig();
    if(cOnfig== null){
        cOnfig= Bitmap.Config.ARGB_8888;
    }

    newBitmap = Bitmap.createBitmap(w, h, config);
    Canvas newCanvas = new Canvas(newBitmap);

    newCanvas.drawBitmap(bmOriginal, 0, 0, null);

    Paint paint = new Paint();

    float left = maskImgView.getLeft();
    float top = maskImgView.getTop();
    Log.d(MainActivity.TAG, "left is " + left);
    Log.d(MainActivity.TAG, "top is " + top);

    newCanvas.drawBitmap(bmMask, left, top, paint);

    return newBitmap;
}
}

Code (xml)



    

        

        




    
    



My Thought:

I guess the positioning issue occurs in the few lines of code listed below. I just have no idea how to get it right, yet.

我想定位问题出现在下面列出的几行代码中。我只是不知道如何做到这一点。

        float left = maskImgView.getLeft();
        float top = maskImgView.getTop();
        newCanvas.drawBitmap(bmMask, left, top, paint);

Any help is appreciated!

任何帮助表示赞赏!

1 个解决方案

#1


0  

In this case both of image view in same layout for ex.

在这种情况下,两个图像视图在相同的布局中为例如。





    

    

------->

RelativeLayout imgSection;

....

....

//On the save button click 
imgSection.buildDrawingCache();
imgSection.getDrawingCache();//it will return combine image bitmap

I hope this will help.....

我希望这个能帮上忙.....


推荐阅读
  • Android中Bitmap与Drawable的区别有哪些?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更 ... [详细]
  • 本文详细介绍了Android中的坐标系以及与View相关的方法。首先介绍了Android坐标系和视图坐标系的概念,并通过图示进行了解释。接着提到了View的大小可以超过手机屏幕,并且只有在手机屏幕内才能看到。最后,作者表示将在后续文章中继续探讨与View相关的内容。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 最近要做一个为视频设置封面的功能,这里展示一下简单的demo。demo效果这里直接将选取的视频某一时间的bitmap显示在视频下方。上面是视频,下面是所获取那一帧的截图。具体代码 ... [详细]
  • Android开发之网络图片查看方法BitmapFactory.decodeStream()学习01
    Android实现网络图片的查看有好几种方法,但这本身是一种很耗时的操作,可以通过直接获取和操作线程的方法,自己学习使用了BitmapFactory.decodeStream()在代码中自己写了注释, ... [详细]
  • Bitmap的二次采样一、二次采样(一)、意义和目的用BitmapFactory解码有一张图片时,有时会遇到错误,这往往是由于图片过大造成的。要想正常使用需要分配更少的内存空间来存 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • 本文介绍了MVP架构模式及其在国庆技术博客中的应用。MVP架构模式是一种演变自MVC架构的新模式,其中View和Model之间的通信通过Presenter进行。相比MVC架构,MVP架构将交互逻辑放在Presenter内部,而View直接从Model中读取数据而不是通过Controller。本文还探讨了MVP架构在国庆技术博客中的具体应用。 ... [详细]
  • Android自定义控件绘图篇之Paint函数大汇总
    本文介绍了Android自定义控件绘图篇中的Paint函数大汇总,包括重置画笔、设置颜色、设置透明度、设置样式、设置宽度、设置抗锯齿等功能。通过学习这些函数,可以更好地掌握Paint的用法。 ... [详细]
  • 在Android中进行图像处理的任务时,有时我们希望将处理后的结果以图像文件的格式保存在内部存储空间中,本文以此为目的,介绍将Bitmap对象的数据以P ... [详细]
  • 在Android中进行图像处理的任务时,有时我们希望将处理后的结果以图像文件的格式保存在内部存储空间中,本文以此为目的,介绍将Bitmap对象的数据以PNG格式保存下来的方法。1、添加权限由于 ... [详细]
  • 本文介绍了在Android开发中使用软引用和弱引用的应用。如果一个对象只具有软引用,那么只有在内存不够的情况下才会被回收,可以用来实现内存敏感的高速缓存;而如果一个对象只具有弱引用,不管内存是否足够,都会被垃圾回收器回收。软引用和弱引用还可以与引用队列联合使用,当被引用的对象被回收时,会将引用加入到关联的引用队列中。软引用和弱引用的根本区别在于生命周期的长短,弱引用的对象可能随时被回收,而软引用的对象只有在内存不够时才会被回收。 ... [详细]
  • 同一张PNG图片,在android studio中颜色值跟PS中不同
    我用PS画了一个简单的存为PNG的图片,40x40,320dpi,8颜色通道。其中线条色为(000000);导入androidstudio中取色,线条颜色变成了(020202)。图片属性显示的是 ... [详细]
  • try{FileOutputStreamoutnewFileOutputStream(sdcardpp.png);CU.BG_BMP.co ... [详细]
author-avatar
鄙人灵魔世界
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有