将两张图片纵向排列,合并为一张图片。


public class PicMakeActivity extends Activity { 

 
 
 String pathBase = Environment.getExternalStorageDirectory() + "/"; 

 
 
 String path[] = new String[2]; 

 
 
 String rePath = pathBase + "aPicMake"; 

 

  @Override 

 

  public void onCreate(Bundle savedInstanceState) { 

 

  super.onCreate(savedInstanceState); 

 

  setContentView(R.layout.main); 

 
 

  path[0] = pathBase + "a0.jpg"; 

 

  path[1] = pathBase + "a1.jpg"; 

 

  System.out.println("path[0]: " + path[0]); 

 
 

  if(makeJPG(path,rePath)){ 

 
 
 Toast.makeText(this, "make picture successed !!!",3000).show(); 

 

  } 

 

  else{ 

 
 
 Toast.makeText(this, "make picture failed !!!",3000).show(); 

 

  } 

 

  } 

 
 

  boolean makeJPG(String[] path, String rePath){ 

 
 
 boolean result = false; 

 
 
 int outHeight = 0; 

 
 
 int outWidth = 0; 

 
 
 Bitmap dstBit = null; 

 
 
 for(int i = 0; i< path.length ;i++){ 

 
 
 Bitmap srcBit = BitmapFactory.decodeFile(path[i]); 

 
 
 if(srcBit.getWidth() > outWidth) 

 
 
 outWidth = srcBit.getWidth(); 

 
 
 outHeight += srcBit.getHeight(); 

 
 
 srcBit.recycle(); 

 
 
 } 

 
 
  

 
 
 dstBit = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888); 

 
 
 Canvas canvas = new Canvas(dstBit); 

 
 
 canvas.drawColor( Color.WHITE ); 

 
 
 Paint paint = new Paint(); 

 
 
 // 
 用来对位图进行滤波处理

paint.setFilterBitmap(true);



int top = 0; 

 
 
 for(int i = 0; i< path.length ;i++){ 

 
 
 Bitmap srcBit = BitmapFactory.decodeFile(path[i]); 

 
 
 Rect srcRect = new Rect(0, 0, srcBit.getWidth(), srcBit.getHeight()); 

 
 
 Rect dstRect = new Rect(); 

 
 
 dstRect.set(0, top, srcBit.getWidth(), srcBit.getHeight() + top); 

 
 
 //把图片srcBit上srcRect画到dstRect的区域内 

 
 
  
 canvas.drawBitmap(srcBit, srcRect, dstRect, paint); 

 
 
 top += srcBit.getHeight(); 

 
 
 srcBit.recycle(); 

 
 
 } 

 
 
 createPath(rePath); 

 
 
  
 File dstFile = new File(rePath + "/b.jpg"); 

 
 
 if(dstFile != null){ 

 
 
  
 try { 

 
 
 FileOutputStream fos = new FileOutputStream(dstFile);


// 经过图像变换之后的Bitmap里的数据可以保存到图像压缩文件里(JPG/PNG)。参数format可设置JPEG或PNG格式;quality可选择压缩质量;fOut是输出流(OutputStream),这里的FileOutputStream是OutputStream的一个子类。


result = dstBit. 
 compress( Bitmap.CompressFormat.JPEG, 100, fos); 

 
 
 } catch (FileNotFoundException e) { 

 
 
 // TODO Auto-generated catch block 

 
 
 e.printStackTrace(); 

 
 
 } 

 
 
 } 

 
 
 return result; 

 
 
  

 

  } 

 
 

  //创建目录(最多一级) 

 

  public File createPath(String path) { 

 
 
  

 
 
 File file = new File(path); 

 
 
 if (!file.exists()) { 

 
 
  
 file.mkdir(); 

 
 
 } 

 
 
 return file; 

 
 
 } 

 

  }



加权限:


<!--往sdcard中写入数据的权限 -->


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
 
 
 

   <!--在sdcard中创建/删除文件的权限 --> 
 
 
 

   <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>






在Android中,目前,我知道有两种出现锯齿的情况。


①当我们用Canvas绘制位图的时候,如果对位图进行了选择,则位图会出现锯齿。


②在用View的RotateAnimation做动画时候,如果View当中包含有大量的图形,也会出现锯齿。




我们分别以这两种情况加以考虑。




◆用Canvas绘制位的的情况。在用Canvas绘制位图时,一般地,我们使用drawBitmap函数家族,在这些函数中,都有一个Paint参数,要做到防止锯齿,我们就要使用到这个参数。


如下:


首先在你的构造函数中,需要创建一个Paint。Paint mPaint = new Paint();


然后,您需要设置两个参数: 1)mPaint.setAntiAlias(); 2)mPaint.setBitmapFilter(true)。第一个函数是用来防止边缘的锯齿,第二个函数是用来对位图进行滤波处理。


最后,在画图的时候,调用drawBitmap函数,只需要将整个Paint传入即可。




◆有时候,当你做RotateAnimation时,你会发现,讨厌的锯齿又出现了。这个时候,由于你不能控制位图的绘制,只能用其他方法来实现防止锯齿。


另外,如果你画的位图很多。不想每个位图的绘制都传入一个Paint。


还有的时候,你不可能控制每个窗口的绘制的时候,您就需要用下面的方法来处理——对整个Canvas进行处理。


1)在您的构造函数中,创建一个Paint滤波器。 PaintFlagsDrawFilter mSetfil = newPaintFlagsDrawFilter(0, Paint.FILTER_BITMAP_FLAG);第一个参数是你要清除的标志位,第二个参数是你要设置的标志位。此处设置为对位图进行滤波。


2)当你在画图的时候,如果是View,则在onDraw当中;如果是ViewGroup,则在dispatchDraw中调用如下函数:canvas.setDrawFilter( mSetfil );




★最后,另外,在Drawable类及其子类中,也有函数setFilterBitmap可以用来对Bitmap进行滤波处理,这样,当你选择Drawable时,会有抗锯齿的效果。