1

我是 android 新手,并试图将三个图像合并到画布中。正如你将在我的代码中看到的那样,使用三个图像。但在画布中只有两个图像显示第一个和最后一个,看起来像中间图像(沙漠)在画布中绘制时丢失。这是代码

代码:-

package com.example.combineimage;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.os.Bundle;
import android.os.IInterface;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class CombineActivity extends Activity {

    private Bitmap image1,image2,image3,combo_image,image1newsize,image2newsize,image3newsize,merge_image;
    Context mcontext;
    private InputStream inputis1,inputis2,inputis3;
    private Button click_to_combine;
    private final int fixedimage_size=100;
    private ImageView final_imageview;
    int blurValue = 5;
    ArrayList<Bitmap> myimage_list;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_combine);
        mcontext=CombineActivity.this;

        click_to_combine=(Button) findViewById(R.id.button_click);

        final_imageview=(ImageView) findViewById(R.id.imageView1);

        myimage_list=new ArrayList<Bitmap>();

        try{
            inputis1=mcontext.getResources().openRawResource(R.drawable.koala);
        image1=BitmapFactory.decodeStream(inputis1);
        image1newsize=resize_image(image1);
        inputis2=mcontext.getResources().openRawResource(R.drawable.desert);
         image2 = BitmapFactory.decodeStream(inputis2);
         image2newsize=resize_image(image2);
         inputis3 =mcontext.getResources().openRawResource(R.drawable.jellyfish);
         image3=BitmapFactory.decodeStream(inputis3);
         image3newsize=resize_image(image3);

         myimage_list.add(image1newsize);
         myimage_list.add(image2newsize);
         myimage_list.add(image3newsize);
        }
        finally
        {
            try {
                inputis2.close();
                inputis2.close();
                inputis3.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        click_to_combine.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                merge_image=merge_given_image(myimage_list);

                final_imageview.setImageBitmap(merge_image);
            }
        });

    }

    protected Bitmap merge_given_image(ArrayList<Bitmap> myimage_list2) {
        // TODO Auto-generated method stub
        Rect srcrect,destrect;
        Bitmap combobitmap;
        int total_width=0,total_height=0;
        int total_image=myimage_list2.size();
        int count=0;
        Canvas mcanvas;
        Bitmap temp_bitmap=null;
        for(int i=0;i<total_image;i++)
        {
             temp_bitmap=myimage_list2.get(i);
            total_width=total_width+temp_bitmap.getWidth();
            Log.e("Total size", ".."+total_width+total_image);

        }
        total_height=temp_bitmap.getHeight();
        Log.e("Total size", ".."+total_height);
        combobitmap=Bitmap.createBitmap(total_width, total_height, Bitmap.Config.ARGB_8888);
        mcanvas=new Canvas(combobitmap);

        for(int i=0;i<total_image;i++)
        {
            srcrect=new Rect(0, 0, myimage_list2.get(i).getWidth(), myimage_list2.get(i).getHeight());
            destrect=new Rect(srcrect);
            if(i!=0)
            {
                destrect.offset(myimage_list2.get(i-1).getWidth(), 0);

                count=count+1;
                Log.e("COUNT", ".."+count);
            }

            mcanvas.drawBitmap(myimage_list2.get(i), srcrect, destrect, null);

        }

        return combobitmap;
    }

    private Bitmap resize_image(Bitmap image_org) {


        int org_width=image_org.getWidth();
        int org_height=image_org.getHeight();

        int newheight=fixedimage_size;
        int newwidth=fixedimage_size;

        float scalewidth=((float)newwidth)/org_width;
        float scaleheight=((float)newheight)/org_height;

        Matrix matrix=new Matrix();

        matrix.postScale(scalewidth, scaleheight);

        Bitmap newsizeimage=Bitmap.createBitmap(image_org, 0, 0, org_width, org_height, matrix, true);

        return newsizeimage;
        // TODO Auto-generated method stub



    }

    protected Bitmap merge_given_image(Bitmap image_first, Bitmap image_second,
            Bitmap image_third) {
        // TODO Auto-generated method stub


        Bitmap combobitmap;
        //BlurMaskFilter blurfilter;
        //Paint paintblur=new Paint();

        int total_width,total_height;


        total_width=image_first.getWidth()+image_second.getWidth()+image_third.getWidth();
        total_height=image_first.getHeight();


        combobitmap=Bitmap.createBitmap(total_width, total_height, Bitmap.Config.ARGB_8888);
        Canvas combocanvas=new Canvas(combobitmap);


//      //Create background in White
//       Bitmap alpha = combobitmap.extractAlpha();
//       paintblur.setColor(Color.WHITE);
//       combocanvas.drawBitmap(combobitmap, 0, 0, paintblur);

//      //Create outer blur, in White
//       blurfilter = new BlurMaskFilter(blurValue, BlurMaskFilter.Blur.OUTER);
//       paintblur.setMaskFilter(blurfilter);
//       combocanvas.drawBitmap(combobitmap, 0, 0, paintblur);

//      //Create inner blur
//       blurfilter = new BlurMaskFilter(blurValue, BlurMaskFilter.Blur.INNER);
//       paintblur.setMaskFilter(blurfilter);
//       combocanvas.drawBitmap(combobitmap, 0, 0, paintblur);


        combocanvas.drawBitmap(image_first,0f, 0f, null);
        combocanvas.drawBitmap(image_second, (image_first.getWidth()/2), 0f, null);
        combocanvas.drawBitmap(image_third, image_second.getWidth(), 0f, null);

        return combobitmap;

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        image1.recycle();
        image1newsize.recycle();
        image2.recycle();
        image2newsize.recycle();
        image3.recycle();
        image3newsize.recycle();
        super.onPause();
    }


}

在上面的代码中,你会发现两种方法,一种具有三个位图,一种方法具有这三个图像的数组列表。所以在具有三个图像的方法中显示前两个图像的一半并进行一些修改,即combocanvas.drawBitmap(image_second, (image_first.getWidth()/2), 0f, null);但具有数组列表的方法仅显示两个图像.这里也是输出的快照输出快照。我关注并通过他的链接是 1:将图像与 rect draw合并 2:在画布中合并图像

但没有得到所需的输出,所以任何人都可以告诉我我在哪里做错了。任何帮助都会非常有帮助。谢谢

4

0 回答 0