android以不规则图片为边框,切割另外的图片

转载自:http://blog.csdn.net/ctpxyza/article/details/11094655




  最近工作上遇到了一个将一个图片按照相框进行裁剪的问题,花了一个下午研究了下,在此整理一下,以便后用。


          +            =     

   (相片)                                              (相框)                                             (结果图)

一个按照边框裁剪的类,因为相框通体为蓝色,只是不同区域的透明度不同,所以,选用了根据透明度来筛选边界上的点的方法。

另外,这个类也提供了判断一个点是否是在相框内部的方法:
和当前像素点同一水平线上的各点,假如左右两侧均有相框蓝色边界上的点的话,并且当前点不在边框上则认为当前点在相框的内部
反之,则认为当前点在相框的外部。
[java]  view plain copy
  1. package com.example.test_filter;  
  2.   
  3. import android.graphics.Bitmap;  
  4. import android.graphics.Color;  
  5. import android.graphics.Matrix;  
  6. import android.graphics.Bitmap.Config;  
  7.   
  8. public class AlphaFilter implements Filter{  
  9.   
  10.     public static final int INSIDE_MODE = 0;  
  11.     public static final int EDGE_MODE = 1;  
  12.     public static final int OUTSIDE_MODE = 2;  
  13.       
  14.     public int findNonOpaque(int x, int y, int width, int height, int[] srcPixels) {  
  15.         if(width < height) {  
  16.             return findNonOpaqueByX(x, y, width, height, srcPixels);  
  17.         } else {  
  18.             return findNonOpaqueByY(x, y, width, height, srcPixels);  
  19.         }  
  20.     }  
  21.       
  22.     /* 
  23.      * 横向检测左右两侧边界 
  24.      */  
  25.     public int findNonOpaqueByX(int x, int y, int width, int height, int[] srcPixels) {  
  26.         int mode = OUTSIDE_MODE;  
  27.         //当前点左右两侧均有边界点出现,则认为当前点在内部或者边框中  
  28.         if(findNonOpaqueByXLeft(x, y, width, height, srcPixels) && findNonOpaqueByXRight(x, y, width, height, srcPixels)) {  
  29.             mode = INSIDE_MODE;  
  30.         }  
  31.         int pos = y*width+x;  
  32.         if(isMatch(pos, srcPixels)) {  
  33.             mode = EDGE_MODE;  
  34.         }  
  35.         return mode;  
  36.     }  
  37.       
  38.     /* 
  39.      * 检测与当前点y坐标相同的左侧各点是否有边界存在 
  40.      */  
  41.     public boolean findNonOpaqueByXLeft(int x, int y, int width, int height, int[] srcPixels) {  
  42.         for(int i=0; i < x; i++) {  
  43.             int pos = y*width + i;  
  44.             if(isMatch(pos, srcPixels)) {  
  45.                 return true;  
  46.             }  
  47.         }  
  48.         return false;  
  49.     }  
  50.       
  51.     /* 
  52.      * 检测与当前点y坐标相同的右侧各点是否有边界存在 
  53.      */  
  54.     public boolean findNonOpaqueByXRight(int x, int y, int width, int height, int[] srcPixels) {  
  55.         for(int i= x+1; i < width; i++) {  
  56.             int pos = y*width + i;  
  57.             if(isMatch(pos, srcPixels)) {  
  58.                 return true;  
  59.             }  
  60.         }  
  61.         return false;  
  62.     }  
  63.       
  64.     /* 
  65.      * 纵向检测上下两侧的边界 
  66.      */  
  67.     public int findNonOpaqueByY(int x, int y, int width, int height, int[] srcPixels) {  
  68.         int mode = OUTSIDE_MODE;  
  69.         //当前点上下两侧均有边界点出现,则认为当前点在内部或者边框中  
  70.         if(findNonOpaqueByYTop(x, y, width, height, srcPixels) && findNonOpaqueByYBottom(x, y, width, height, srcPixels)) {  
  71.             mode = INSIDE_MODE;  
  72.         }  
  73.         int pos = y*width+x;  
  74.         if(isMatch(pos, srcPixels)) {  
  75.             mode = EDGE_MODE;  
  76.         }  
  77.         return mode;  
  78.     }  
  79.       
  80.     /* 
  81.      * 检测与当前点x坐标相同的上方各点是否有边界存在 
  82.      */  
  83.     public boolean findNonOpaqueByYTop(int x, int y, int width, int height, int[] srcPixels) {  
  84.         for(int i=0; i < y; i++) {  
  85.             int pos = i*width + x;  
  86.             if(isMatch(pos, srcPixels)) {  
  87.                 return true;  
  88.             }  
  89.         }  
  90.         return false;  
  91.     }  
  92.       
  93.     /* 
  94.      * 检测与当前点x坐标相同的下方各点是否有边界存在 
  95.      */  
  96.     public boolean findNonOpaqueByYBottom(int x, int y, int width, int height, int[] srcPixels) {  
  97.         for(int i=y+1; i < height; i++) {  
  98.             int pos = i*width + x;  
  99.             if(isMatch(pos, srcPixels)) {  
  100.                 return true;  
  101.             }  
  102.         }  
  103.         return false;  
  104.     }  
  105.       
  106.     public boolean isMatch(int pos, int[]srcPixels) {  
  107.         int color = srcPixels[pos];  
  108.         int alpha = Color.alpha(color);  
  109.         //检测是否是边界,针对背景图片选用透明度进行过滤  
  110.         if(alpha >= 94 && alpha < 255) {  
  111.             return true;  
  112.         }  
  113.         return false;  
  114.     }  
  115.       
  116.     /** 
  117.      * 图片效果叠加 
  118.      * @param bmp 要裁剪的图片 
  119.      * @param filter 边框 
  120.      * @return 
  121.      */  
  122.     public Bitmap overlay(Bitmap bmp, Bitmap filter)  
  123.     {  
  124.         int width = bmp.getWidth();  
  125.         int height = bmp.getHeight();  
  126.         Bitmap overlay = filter;  
  127.         Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_4444);  
  128.         bitmap.setHasAlpha(true);   
  129.           
  130.         // 对边框图片进行缩放  
  131.         int w = overlay.getWidth();  
  132.         int h = overlay.getHeight();  
  133.         float scaleX = width * 1F / w;  
  134.         float scaleY = height * 1F / h;  
  135.         Matrix matrix = new Matrix();  
  136.         matrix.postScale(scaleX, scaleY);  
  137.           
  138.         Bitmap overlayCopy = Bitmap.createBitmap(overlay, 00, w, h, matrix, true);  
  139.           
  140.         int[] srcPixels = new int[width * height];  
  141.         int[] layPixels = new int[width * height];  
  142.         bmp.getPixels(srcPixels, 0, width, 00, width, height);  
  143.         overlayCopy.getPixels(layPixels, 0, width, 00, width, height);  
  144.           
  145.         int pos = 0;  
  146.         for (int i = 0; i < height; i++)  
  147.         {  
  148.             for (int k = 0; k < width; k++)  
  149.             {  
  150.                 pos = i * width + k;  
  151.                   
  152.                 int mode = findNonOpaque(k, i, width, height, layPixels);  
  153.                 if(mode == INSIDE_MODE) {  
  154.                     srcPixels[pos] = srcPixels[pos];  
  155.                     continue;  
  156.                 } else if(mode == EDGE_MODE){  
  157.                     srcPixels[pos] = layPixels[pos];  
  158.                 } else{  
  159.                     srcPixels[pos] = 0;  
  160.                     continue;  
  161.                 }  
  162.                   
  163.             }  
  164.         }  
  165.         bitmap.setPixels(srcPixels, 0, width, 00, width, height);  
  166.         return bitmap;  
  167.     }  
  168. }  


[java]  view plain copy
  1. package com.example.test_filter;  
  2.   
  3. import android.os.AsyncTask;  
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.graphics.Bitmap;  
  7. import android.graphics.BitmapFactory;  
  8. import android.widget.ImageView;  
  9.   
  10. public class MainActivity extends Activity {  
  11.   
  12.     ImageView filter;  
  13.     AlphaFilter alphaFilter;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.         filter = (ImageView) findViewById(R.id.filter);  
  19.         alphaFilter = new AlphaFilter();  
  20.     }  
  21.   
  22.     @Override  
  23.     protected void onResume() {  
  24.         Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.album_photo_0);  
  25.         Bitmap frame = BitmapFactory.decodeResource(getResources(), R.drawable.first_album_frame);  
  26.         new BitmapFilter().execute(bitmap, frame);  
  27.         super.onResume();  
  28.     }  
  29.   
  30.     class BitmapFilter extends AsyncTask<Bitmap, Bitmap, Bitmap> {  
  31.   
  32.         @Override  
  33.         protected Bitmap doInBackground(Bitmap... params) {  
  34.             Bitmap bitmap = alphaFilter.overlay(params[0], params[1]);  
  35.             return bitmap;  
  36.         }  
  37.   
  38.         @Override  
  39.         protected void onPostExecute(Bitmap result) {  
  40.             filter.setImageBitmap(result);  
  41.             super.onPostExecute(result);  
  42.         }  
  43.           
  44.           
  45.     }  
  46. }  

  最近工作上遇到了一个将一个图片按照相框进行裁剪的问题,花了一个下午研究了下,在此整理一下,以便后用。


          +            =     

   (相片)                                              (相框)                                             (结果图)

一个按照边框裁剪的类,因为相框通体为蓝色,只是不同区域的透明度不同,所以,选用了根据透明度来筛选边界上的点的方法。

另外,这个类也提供了判断一个点是否是在相框内部的方法:
和当前像素点同一水平线上的各点,假如左右两侧均有相框蓝色边界上的点的话,并且当前点不在边框上则认为当前点在相框的内部
反之,则认为当前点在相框的外部。
[java]  view plain copy
  1. package com.example.test_filter;  
  2.   
  3. import android.graphics.Bitmap;  
  4. import android.graphics.Color;  
  5. import android.graphics.Matrix;  
  6. import android.graphics.Bitmap.Config;  
  7.   
  8. public class AlphaFilter implements Filter{  
  9.   
  10.     public static final int INSIDE_MODE = 0;  
  11.     public static final int EDGE_MODE = 1;  
  12.     public static final int OUTSIDE_MODE = 2;  
  13.       
  14.     public int findNonOpaque(int x, int y, int width, int height, int[] srcPixels) {  
  15.         if(width < height) {  
  16.             return findNonOpaqueByX(x, y, width, height, srcPixels);  
  17.         } else {  
  18.             return findNonOpaqueByY(x, y, width, height, srcPixels);  
  19.         }  
  20.     }  
  21.       
  22.     /* 
  23.      * 横向检测左右两侧边界 
  24.      */  
  25.     public int findNonOpaqueByX(int x, int y, int width, int height, int[] srcPixels) {  
  26.         int mode = OUTSIDE_MODE;  
  27.         //当前点左右两侧均有边界点出现,则认为当前点在内部或者边框中  
  28.         if(findNonOpaqueByXLeft(x, y, width, height, srcPixels) && findNonOpaqueByXRight(x, y, width, height, srcPixels)) {  
  29.             mode = INSIDE_MODE;  
  30.         }  
  31.         int pos = y*width+x;  
  32.         if(isMatch(pos, srcPixels)) {  
  33.             mode = EDGE_MODE;  
  34.         }  
  35.         return mode;  
  36.     }  
  37.       
  38.     /* 
  39.      * 检测与当前点y坐标相同的左侧各点是否有边界存在 
  40.      */  
  41.     public boolean findNonOpaqueByXLeft(int x, int y, int width, int height, int[] srcPixels) {  
  42.         for(int i=0; i < x; i++) {  
  43.             int pos = y*width + i;  
  44.             if(isMatch(pos, srcPixels)) {  
  45.                 return true;  
  46.             }  
  47.         }  
  48.         return false;  
  49.     }  
  50.       
  51.     /* 
  52.      * 检测与当前点y坐标相同的右侧各点是否有边界存在 
  53.      */  
  54.     public boolean findNonOpaqueByXRight(int x, int y, int width, int height, int[] srcPixels) {  
  55.         for(int i= x+1; i < width; i++) {  
  56.             int pos = y*width + i;  
  57.             if(isMatch(pos, srcPixels)) {  
  58.                 return true;  
  59.             }  
  60.         }  
  61.         return false;  
  62.     }  
  63.       
  64.     /* 
  65.      * 纵向检测上下两侧的边界 
  66.      */  
  67.     public int findNonOpaqueByY(int x, int y, int width, int height, int[] srcPixels) {  
  68.         int mode = OUTSIDE_MODE;  
  69.         //当前点上下两侧均有边界点出现,则认为当前点在内部或者边框中  
  70.         if(findNonOpaqueByYTop(x, y, width, height, srcPixels) && findNonOpaqueByYBottom(x, y, width, height, srcPixels)) {  
  71.             mode = INSIDE_MODE;  
  72.         }  
  73.         int pos = y*width+x;  
  74.         if(isMatch(pos, srcPixels)) {  
  75.             mode = EDGE_MODE;  
  76.         }  
  77.         return mode;  
  78.     }  
  79.       
  80.     /* 
  81.      * 检测与当前点x坐标相同的上方各点是否有边界存在 
  82.      */  
  83.     public boolean findNonOpaqueByYTop(int x, int y, int width, int height, int[] srcPixels) {  
  84.         for(int i=0; i < y; i++) {  
  85.             int pos = i*width + x;  
  86.             if(isMatch(pos, srcPixels)) {  
  87.                 return true;  
  88.             }  
  89.         }  
  90.         return false;  
  91.     }  
  92.       
  93.     /* 
  94.      * 检测与当前点x坐标相同的下方各点是否有边界存在 
  95.      */  
  96.     public boolean findNonOpaqueByYBottom(int x, int y, int width, int height, int[] srcPixels) {  
  97.         for(int i=y+1; i < height; i++) {  
  98.             int pos = i*width + x;  
  99.             if(isMatch(pos, srcPixels)) {  
  100.                 return true;  
  101.             }  
  102.         }  
  103.         return false;  
  104.     }  
  105.       
  106.     public boolean isMatch(int pos, int[]srcPixels) {  
  107.         int color = srcPixels[pos];  
  108.         int alpha = Color.alpha(color);  
  109.         //检测是否是边界,针对背景图片选用透明度进行过滤  
  110.         if(alpha >= 94 && alpha < 255) {  
  111.             return true;  
  112.         }  
  113.         return false;  
  114.     }  
  115.       
  116.     /** 
  117.      * 图片效果叠加 
  118.      * @param bmp 要裁剪的图片 
  119.      * @param filter 边框 
  120.      * @return 
  121.      */  
  122.     public Bitmap overlay(Bitmap bmp, Bitmap filter)  
  123.     {  
  124.         int width = bmp.getWidth();  
  125.         int height = bmp.getHeight();  
  126.         Bitmap overlay = filter;  
  127.         Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_4444);  
  128.         bitmap.setHasAlpha(true);   
  129.           
  130.         // 对边框图片进行缩放  
  131.         int w = overlay.getWidth();  
  132.         int h = overlay.getHeight();  
  133.         float scaleX = width * 1F / w;  
  134.         float scaleY = height * 1F / h;  
  135.         Matrix matrix = new Matrix();  
  136.         matrix.postScale(scaleX, scaleY);  
  137.           
  138.         Bitmap overlayCopy = Bitmap.createBitmap(overlay, 00, w, h, matrix, true);  
  139.           
  140.         int[] srcPixels = new int[width * height];  
  141.         int[] layPixels = new int[width * height];  
  142.         bmp.getPixels(srcPixels, 0, width, 00, width, height);  
  143.         overlayCopy.getPixels(layPixels, 0, width, 00, width, height);  
  144.           
  145.         int pos = 0;  
  146.         for (int i = 0; i < height; i++)  
  147.         {  
  148.             for (int k = 0; k < width; k++)  
  149.             {  
  150.                 pos = i * width + k;  
  151.                   
  152.                 int mode = findNonOpaque(k, i, width, height, layPixels);  
  153.                 if(mode == INSIDE_MODE) {  
  154.                     srcPixels[pos] = srcPixels[pos];  
  155.                     continue;  
  156.                 } else if(mode == EDGE_MODE){  
  157.                     srcPixels[pos] = layPixels[pos];  
  158.                 } else{  
  159.                     srcPixels[pos] = 0;  
  160.                     continue;  
  161.                 }  
  162.                   
  163.             }  
  164.         }  
  165.         bitmap.setPixels(srcPixels, 0, width, 00, width, height);  
  166.         return bitmap;  
  167.     }  
  168. }  


[java]  view plain copy
  1. package com.example.test_filter;  
  2.   
  3. import android.os.AsyncTask;  
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.graphics.Bitmap;  
  7. import android.graphics.BitmapFactory;  
  8. import android.widget.ImageView;  
  9.   
  10. public class MainActivity extends Activity {  
  11.   
  12.     ImageView filter;  
  13.     AlphaFilter alphaFilter;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.         filter = (ImageView) findViewById(R.id.filter);  
  19.         alphaFilter = new AlphaFilter();  
  20.     }  
  21.   
  22.     @Override  
  23.     protected void onResume() {  
  24.         Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.album_photo_0);  
  25.         Bitmap frame = BitmapFactory.decodeResource(getResources(), R.drawable.first_album_frame);  
  26.         new BitmapFilter().execute(bitmap, frame);  
  27.         super.onResume();  
  28.     }  
  29.   
  30.     class BitmapFilter extends AsyncTask<Bitmap, Bitmap, Bitmap> {  
  31.   
  32.         @Override  
  33.         protected Bitmap doInBackground(Bitmap... params) {  
  34.             Bitmap bitmap = alphaFilter.overlay(params[0], params[1]);  
  35.             return bitmap;  
  36.         }  
  37.   
  38.         @Override  
  39.         protected void onPostExecute(Bitmap result) {  
  40.             filter.setImageBitmap(result);  
  41.             super.onPostExecute(result);  
  42.         }  
  43.           
  44.           
  45.     }  
  46. }  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在Android Studio中的圆形边框中添加图片,您可以按照以下步骤进行操作: 1. 在res/drawable文件夹中创建一个XML文件(例如circlular_border.xml),并添加以下代码: ``` <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="50dp"/> <stroke android:width="2dp" android:color="@color/black"/> <solid android:color="@color/white"/> </shape> ``` 2. 在布局文件中添加ImageView控件,并为其设置背景为刚刚创建的XML文件: ``` <ImageView android:id="@+id/imageView" android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/circular_border" /> ``` 3. 在Java代码中加载您想要添加的图片,并将其设置为ImageView的源(src): ``` ImageView imageView = findViewById(R.id.imageView); imageView.setImageResource(R.drawable.your_image_file); ``` 这样,您就可以在圆形边框中添加图片了。请注意,如果您想要实现圆形ImageView,可以使用CircleImageView库。 ### 回答2: 在Android Studio中添加圆形边框并在其中添加图片的步骤如下: 1. 首先,在drawable文件夹下创建一个圆形边框的XML文件。例如,创建一个名为circle_border.xml的文件。 2. 在circle_border.xml文件中,使用shape标签定义一个圆形的形状,并设置边框的颜色和宽度。例如,可以使用solid标签设置边框的颜色,stroke标签设置边框的宽度。同时,设置一个size属性来限定圆形的大小。以下是circle_border.xml文件的示例代码: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#FF0000" /> <stroke android:color="#000000" android:width="2dp" /> <size android:width="100dp" android:height="100dp" /> </shape> ``` 3. 在布局文件中,添加一个ImageView元素,并为它设置drawable属性为前面创建的circle_border.xml文件。例如,可以将drawable属性设置为@drawable/circle_border。以下是一个示例代码: ```xml <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/circle_border" /> ``` 4. 最后,在Java代码中,使用Glide或者Picasso等图片加载库来加载要显示的图片,并将其设置到ImageView元素中。以下是一个使用Glide库加载图片的示例代码: ```java ImageView imageView = findViewById(R.id.imageView); Glide.with(this) .load(R.drawable.your_image) .into(imageView); ``` 以上就是在Android Studio中添加圆形边框并添加图片的步骤。通过创建圆形边框的XML文件,并将该边框作为ImageView的drawable属性,再加载图片到ImageView中,即可实现圆形边框中添加图片的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值