热门标签 | 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开发之网络图片查看方法BitmapFactory.decodeStream()学习01
    Android实现网络图片的查看有好几种方法,但这本身是一种很耗时的操作,可以通过直接获取和操作线程的方法,自己学习使用了BitmapFactory.decodeStream()在代码中自己写了注释, ... [详细]
  • Android中Bitmap与Drawable的区别有哪些?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
  • Bitmap的二次采样一、二次采样(一)、意义和目的用BitmapFactory解码有一张图片时,有时会遇到错误,这往往是由于图片过大造成的。要想正常使用需要分配更少的内存空间来存 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • Android LED 数字字体的应用与实现
    本文介绍了一种适用于 Android 应用的 LED 数字字体(digital font),并详细描述了其在 UI 设计中的应用场景及其实现方法。这种字体常用于视频、广告倒计时等场景,能够增强视觉效果。 ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
  • 2023年京东Android面试真题解析与经验分享
    本文由一位拥有6年Android开发经验的工程师撰写,详细解析了京东面试中常见的技术问题。涵盖引用传递、Handler机制、ListView优化、多线程控制及ANR处理等核心知识点。 ... [详细]
  • ASP.NET MVC中Area机制的实现与优化
    本文探讨了在ASP.NET MVC框架中,如何通过Area机制有效地组织和管理大规模应用程序的不同功能模块。通过合理的文件夹结构和命名规则,开发人员可以更高效地管理和扩展项目。 ... [详细]
  • Iamtryingtodevelopanapponcanvas,Iamdrawingabitmaponthecanvas.Afterdrawing,iamtryin ... [详细]
  • 最近要做一个为视频设置封面的功能,这里展示一下简单的demo。demo效果这里直接将选取的视频某一时间的bitmap显示在视频下方。上面是视频,下面是所获取那一帧的截图。具体代码 ... [详细]
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社区 版权所有