使用android的bitmap类实现图片的拼接

这篇博客介绍了如何在Android中使用Bitmap类进行图片拼接操作。通过创建Bitmap对象,解码文件,缩放图片,利用Canvas在新的Bitmap上绘制并添加边距,最后展示在屏幕上。示例代码展示了如何在自定义View中实现这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package test.com;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class Demo_picActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
//        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(new picRe(this));
    }
    
    class picRe extends View
    {
		public picRe(Context context) 
		{
			super(context);
		}
		public picRe(Context context,String[] path)
		{
			super(context);
			this.path = path;
		}
		public picRe(Context context, String[] path, int width, int height)
		{
			super(context);
			this.path = path;
			this.width = width;
			this.height = height;
		}

		@Override
		protected void onDraw(Canvas canvas)
		{
			super.onDraw(canvas);
			int height_sum = border;
			Bitmap pic[] = new Bitmap[path.length];
			for (int i = 0; i < pic.length; i++)
			{
				pic[i] = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(path[i]), width, height, true);
				height_sum += pic[i].getHeight() + border;
			}
			Bitmap new_pic = Bitmap.createBitmap(width + border * 2, height_sum, Config.ARGB_8888);
			Canvas _reCanvas = new Canvas(new_pic);
			_reCanvas.drawColor(-1);
			for (int i = 0; i < pic.length; i++) 
			{
				_reCanvas.drawBitmap(pic[i], border, height * i + border * i + border, null);
				pic[i].recycle();
			}
			Matrix new_pic_Matrix = canvas.getMatrix();
			new_pic_Matrix.postScale(200.0f / (float)width, 200.0f / (float)width);
			canvas.setMatrix(new_pic_Matrix);
			canvas.drawBitmap(new_pic, 0, 150, null);
		}
		
		
		private String[] path = {"data/data/com.test/filse/you.jpg", "data/data/com.test/filse/test.gif", "data/data/com.test/filse/you.jpg"};
		private int width = 480;
		private int height = 480;
		private int border = 5;
    }
    
}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值