自定义之一个图片根据另一个图片形状进行裁剪
2017-07-26 16:13
639 查看
大佬绕道,本人小白
先上一个原型图(gif 懒得弄)
怎么样 我这个原型图是不是很厉害 哈哈哈~~~
需求:
源图( 图 1 )
需要被裁剪成某个形状 ( 图 2 )
最终效果( 图 3 )
首先先引入资源文件 在values下创建名称为attrs的文件
配置attrs中的资源属性(因为我都加入注释了 所以看注释就应该懂了 我就不再这里多说了)
那个format 就是类型 里边有好多类型 具体的可以百度
<declare-styleable name="TailorDimView" > <!--被覆盖--> <attr name="image_dst" format="reference"></attr> <!--覆盖--> <attr name="image_src" format="reference"></attr> <!--透明度--> <attr name="trans" format="integer"></attr> </declare-styleable>
下一步在xml中引用 dst和src 是两张图片 具体会在下面讲到
<com.haiyunx.android.kxw.view.TailorDimView android:id="@+id/tdv_tailor_dim" android:layout_width="match_parent" android:scaleType="fitXY" tailor:image_dst="@mipmap/find_head_bg" tailor:image_src="@mipmap/test" tailor:trans="50" android:layout_height="165dp" />
先把公共的方法写出来 方便写出来
public TailorDimView setBitmap_Dst(Bitmap bitmap_dst) { this.B_Dst = bitmap_dst; return this; } public TailorDimView setBitmap_Src(Bitmap bitmap_src) { this.B_Src = bitmap_src; return this; } public TailorDimView setTransparency(int l){ this.transparency = l; return this; } public void refresh(){ initBitmap() ; }
调用 第一个是不设置透明度 默认100,第二个设置透明度 70 ———(100不透明 0全透明)
tdvTailorDim.setBitmap_Src(src).setBitmap_Dst(dst).refresh(); tdvTailorDim.setBitmap_Src(src).setBitmap_Dst(dst).setTransparency(70).refresh();
先继承一个ImageView ,可以看到构造方法中只有initAttrs和initBitmap
public class TailorDimView extends ImageView { // private int DEF_HEIGHT = xxxx; private int Dst = 0; //需要被裁剪什么形状 就传什么形状的图片 xml中使用 private int Src = 0; //源图片 xml中使用 private Bitmap B_Dst; //代码传入需要裁剪形状的图片 private Bitmap B_Src; //代码传入源图片 private Integer transparency; //透明度 默认100(不透明) public TailorDimView(Context context) { this(context, null); } public TailorDimView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public TailorDimView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initAttrs(context, attrs, defStyleAttr); initBitmap(); }
initAttrs(context, attrs, defStyleAttr) 获取资源, 需要注意的是 transparency 这个是设置图片的透明度的.获取方法是 .getInteger.上面两个方法是获取资源(也就是图片) 方法不一样 ,当时我就是在这弄了半天返回的一直是默认值….这块需要注意一下
private void initAttrs(Context context, AttributeSet attrs, int defStyleAttr) { TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.TailorDimView, defStyleAttr, 0); //获取xml 中需要被裁剪形状的图片 Dst = typedArray.getResourceId(R.styleable.TailorDimView_image_dst, 0); //获取xml 中源图片 Src = typedArray.getResourceId(R.styleable.TailorDimView_image_src, 0); //透明度 transparency = typedArray.getInteger(R.styleable.TailorDimView_trans, 100); typedArray.recycle(); }
initBitmap() 这部分才是最关键的地方 ,具体的看注释就应该明白了
private void initBitmap() { Bitmap bitmap_src = null; Bitmap bitmap_dst = null; //判断 有没有在代码中传入图片 优先级高于xml 设置 if (null != B_Src && null != B_Dst){ bitmap_src = B_Src; bitmap_dst = B_Dst; //代码没有设置 则获取xml中的图片 }else if (Dst != 0 && Src != 0) { bitmap_dst = BitmapFactory.decodeResource(getResources(), Dst); saveBitmap(bitmap_dst,"bitmap_dst"); bitmap_src = BitmapFactory.decodeResource(getResources(), Src); saveBitmap(bitmap_src,"bitmap_src"); }else{ //都没有 则抛出异常(英语是语文老师教的- -) Logger.e("bitmap_src and bitmap_dst is nul"); throw new NullPointerException(); } //用于设置 Canvas 的位图 Bitmap W_H = Bitmap.createBitmap(bitmap_dst.getWidth(), bitmap_dst.getHeight(), Bitmap.Config.ARGB_8888); //设置画布 Canvas canvas = new Canvas(W_H); //设置画笔 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); //设置 两张图片 覆盖的模式 (具体16中模式 百度---Canvas 覆盖模式) paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); //如果透明度 不是 100 则代码设置 if (transparency != 100) paint.setAlpha(transparency); //设置源图 canvas.drawBitmap(bitmap_src, 0, 0, null); //设置裁剪图片 canvas.drawBitmap(bitmap_dst, 0, 0, paint); //置空 paint.setXfermode(null); //设置显示图片 记住 一定是你设定的 V_H 之前我一直使用bitmap_dst 一直显示裁剪不成功 切记切记 setImageBitmap(W_H); saveBitmap(W_H,"merge"); saveBitmap(bitmap_dst,"bitmap_dst___!"); saveBitmap(bitmap_src,"bitmap_src___!"); }
大家应该看到了 savaBitmap 这是是我在测试的时候 看不同时候图片的形状
public void saveBitmap(Bitmap bm,String picName) { File file = new File("/sdcard/song/"); if(!file.exists()) file.mkdirs(); try { FileOutputStream fileOutputStream = new FileOutputStream(file.getPath() + "/"+picName+".jpg"); bm.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream); fileOutputStream.close(); System.out.println("saveBmp is here"); } catch (Exception e) { e.printStackTrace(); } }
最终效果图 我这个是带有 透明度的
这是我第二次在CSDN中写博客 总感觉写的乱乱的 以后我会改改的,羡慕他们设计漂亮的博客
相关文章推荐
- java一个对象根据另一个集合的顺序进行排序
- JS 使用 Android + IOS 相册 + 相机 进行自定义头像 + 限制裁剪后图片尺寸 + base64 string 与 图片互相转换
- android 调用系统自带的选择图片 然后进行自定义大小裁剪
- MFC异形窗口-多边形窗口-根据图片自定义窗口形状-CRgn
- cocos2d-lua 图片裁剪自定义形状
- 把一个图片裁剪成圆形或者自定义的图形
- 获取DataGridView绑定BindingSource的数据表,并根据树控件点击进行筛选数据表,下拉控件自定义排序问题初步解决方法,解决了该行属于另一个表的错误提示.
- ORACLE 对一个表进行循环查数,再根据MO供给数量写入另一个新表
- HTML 在画布中对图片,图片上另一个图层上的点,根据点绘制的路线进行缩放1
- ios-对图片进行裁剪得到一个新图片和保存到相册
- matlab 根据一个目录下多个文件夹中的多个注释名字找个对应图片拷贝到另一个文件夹中
- [待解决]自定义头像时使用vue-cropper进行图片裁剪,得到的是base64格式的图片,如何对接file类型的api接口
- 【代码】将任意形状的图片裁剪成一个带圆环的头像(附效果图)
- Java基础---Java---IO流-----LineNumberReader方法及原理、自定义一个LineNumberReader、字节流、图片复制、mp3复制、
- 自定义一个图片shape
- 根据list对象的某一个属性进行排序
- 对png图片进行换色的一个函数
- mysql 连接查询指的是将两张表或多张表关联到一起进行查询,获取一个表的行与另一个表的行匹配的数据。常见的连接查询包括内连接(等值连接)、左(外)连接、右(外)连接和交叉连接(完全连接)等
- 如何创建根据一个表的结构创建另一个表。
- 安卓自定义按钮图片 和形状