繁体   English   中英

如何在Android中将两个imageview合并为一个图像?

[英]How to merge two imageview into one image in Android?

我正在创建一个应用程序,用于将一个图像视图放置在另一个图像视图上,并制作成单个图像并保存到SD卡中。 我运行该应用程序,它创建图像并保存到SD卡,但是该图像为空白。 这两个图像都不会显示在SD卡中保存的一个图像上。

这是我的代码。

@SuppressWarnings("deprecation")
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ImageView image2 = (ImageView) findViewById(R.id.ImageViewTwo);
        final ImageView image = (ImageView) findViewById(R.id.ImageViewOne);

        mTempDir = Environment.getExternalStorageDirectory()+"/"+"TestTemp";
        File mtempFile = new File(mTempDir);
        if(!mtempFile.exists())
        {
            mtempFile.mkdir();
        }

        mSaveImageName = "Test.png";
        mBackGround = Bitmap.createBitmap(100 , 100 ,Bitmap.Config.ARGB_8888);

        image2.buildDrawingCache();
        mBackImage  = image2.getDrawingCache();
        mBackImage  = Bitmap.createBitmap(100 , 100 ,Bitmap.Config.ARGB_8888);

        image.buildDrawingCache();
        mTopImage = image.getDrawingCache();
        mTopImage = Bitmap.createBitmap(100 , 100 , Bitmap.Config.ARGB_8888);

        mCanvas = new Canvas(mBackGround);

        //mCanvas.drawBitmap(mBackImage, (mCanvas.getWidth() / 2), 0, null);
        mCanvas.drawBitmap(mBackImage ,0f , 0f , null);
        mCanvas.drawBitmap(mTopImage, 12f , 12f , null);

        try
        {
            mBitmapDrawable = new BitmapDrawable(mBackGround);

            Bitmap mNewSaving = mBitmapDrawable .getBitmap();
            String ftoSave = mTempDir + mSaveImageName;
            File mFile = new File(ftoSave);
            mFileOutputStream = new FileOutputStream(mFile);
            mNewSaving.compress(CompressFormat.PNG, 95 , mFileOutputStream);
            mFileOutputStream.flush();
            mFileOutputStream.close();

        }
        catch(FileNotFoundException  e)
        {
            e.printStackTrace();
        } 
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.i(TAG, "Image Created");



        }

您有一些多余的行:

image2.buildDrawingCache();
mBackImage  = image2.getDrawingCache();
// delete next line
mBackImage  = Bitmap.createBitmap(100 , 100 ,Bitmap.Config.ARGB_8888);

image.buildDrawingCache();
mTopImage = image.getDrawingCache();
// delete next line
mTopImage = Bitmap.createBitmap(100 , 100 , Bitmap.Config.ARGB_8888);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM