android如果给imageview做圆角,如果在原有的bitmap上加上一些修饰的drawable
来源:互联网 发布:ps4重新构建数据库 编辑:程序博客网 时间:2024/05/06 16:27
先上效果图:
Layout文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#99CC99" tools:context=".MainActivity" > <ImageView android:id="@+id/img1" android:layout_centerInParent="true" android:layout_width="50dp" android:layout_height="50dp" android:adjustViewBounds="true" android:scaleType="centerInside" android:src="@drawable/portrait" /> <ImageView android:id="@+id/img5" android:layout_centerVertical="true" android:layout_toLeftOf="@id/img1" android:layout_marginRight="20dp" android:layout_width="50dp" android:layout_height="50dp" android:adjustViewBounds="true" android:scaleType="fitXY" android:src="@drawable/portrait" /> <ImageView android:id="@+id/img6" android:layout_centerVertical="true" android:layout_toLeftOf="@id/img5" android:layout_marginRight="20dp" android:layout_width="50dp" android:layout_height="50dp" android:adjustViewBounds="true" android:scaleType="center" android:src="@drawable/portrait" /> <ImageView android:id="@+id/img4" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@id/img1" android:layout_width="50dp" android:layout_height="50dp" android:adjustViewBounds="true" android:scaleType="centerCrop" android:src="@drawable/portrait" /> <ImageView android:id="@+id/img7" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@id/img4" android:layout_width="50dp" android:layout_height="50dp" android:adjustViewBounds="true" android:scaleType="fitCenter" android:src="@drawable/portrait" /> <ImageView android:id="@+id/img2" android:layout_marginBottom="20dp" android:layout_centerHorizontal="true" android:layout_above="@id/img1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/img3" android:layout_marginTop="20dp" android:layout_centerHorizontal="true" android:layout_below="@id/img1" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
首先介绍两种把资源里的drawable转成bitmap的方式
第一种:
Bitmap bmp=BitmapFactory.decodeResource(this.getResources(), R.drawable.portrait);第二种:
Drawable d = this.getResources().getDrawable(R.drawable.portrait);Bitmap bmp = drawableToBitmap(d);public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888 ); Canvas canvas = new Canvas(bitmap); //canvas.setBitmap(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap;}
把Bitmap加上圆角的方法:
private static RectF rrbRectf = new RectF();private static Path rrbRath = new Path();private static final int RRB_DEFAULT_SIZE = 10;public static Bitmap outputBmp(Context ctx, Bitmap src, boolean isCover) {Bitmap v = null;if (isCover) {v = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.v);}else{v = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.tv);}Bitmap bmp = null;int arcLength = RRB_DEFAULT_SIZE;if (src != null && arcLength > 0) {int width = src.getWidth();int height = src.getHeight();// Utils.loge("width:" + width + "height:" + height);Bitmap newBitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_4444);Canvas canvas = new Canvas(newBitmap);rrbRath.reset();rrbRectf.set(0, 0, width, height);<strong>rrbRath.addRoundRect(rrbRectf, arcLength, arcLength,Path.Direction.CW); //这里是加上圆角</strong>canvas.clipPath(rrbRath);canvas.drawBitmap(src, 0, 0, new Paint(Paint.ANTI_ALIAS_FLAG));if (newBitmap != null && v != null) {int width1 = newBitmap.getWidth();int height1 = newBitmap.getHeight();int width2 = v.getWidth();int height2 = v.getHeight();bmp = Bitmap.createBitmap(width1 + (width2 / 2), height1+ (height2 / 2), Bitmap.Config.ARGB_4444);bmp.eraseColor(Color.TRANSPARENT);Canvas canvas2 = new Canvas(bmp);canvas2.drawBitmap(newBitmap, 0, 0, new Paint(Paint.ANTI_ALIAS_FLAG));<strong>canvas2.drawBitmap(v, width1 - (width2 / 2), height1- (height2 / 2), new Paint(Paint.ANTI_ALIAS_FLAG)); //这里是在图片右下角加上v</strong>newBitmap.recycle();v.recycle();return bmp;}src.recycle();}return bmp;}
scaletype的种类分为三类matrix(默认)、fit-X类、和center类。matrix就不多说。fit-X类中,
fitStart、fitCenter和fitEnd之间的都是根据需要使原图改变对ImgView进行适应,不剪裁,按matrix进行绘制,但它们
的区别在于基准不同。fitStart的基准为最上角的点(即matrix方式开始的点)fitCenter的基准点为中间的点
(matrix方式中可以使图片居中的点),而fitEnd的基准点为右下角的点(即matrix方式最后绘制点)。center类
中,center、centerCrop、centerInside都是以原图的几何中心点和ImagView的几何中心点为基准,且只绘制
ImagView大小的图像,不同的是是否保持原图大小和绘图的目标不同、采取的手段不同
CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置FIT_XY / fitXY 把图片不按比例扩大/缩小到View的大小显示MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。
0 0
- android如果给imageview做圆角,如果在原有的bitmap上加上一些修饰的drawable
- android在如果绘制自定义的bitmap,然后返回给ImageView
- android中,如果使用imageButton可以在drawable 中设置一个selector,但是imageView设置不起作用
- 如果在自己的网页加上企业邮局登录接口
- Android Studio如果添加的是aar,要在gradle中,后面加上@aar
- 如果APP已经启动,在新的task里重新启动APP,不影响原有的task。。。
- android如果给TextView或EditText的email链接加下划线,并在点击在email连接上可以弹框显示
- Android的Bitmap与Drawable
- Android 销毁ImageView的bitmap
- bitmap和drawable的一些简单操作
- 如果在BlackBerry上实现动态分页的效果
- 如果能站在巨人的肩膀上
- 在github上如果管理自己的版本仓库
- Bitmap加载于imageView的一些坑
- rc文件如果加上自己的宏定义怎么办
- 如果更改android 4.4.4上的launcher3默认布局
- ImageView,Bitmap,Drawable
- android中如果在手机显示logo不清楚的问题
- 八月的我。。。
- hdu 4609 3-idiots 【FFT快速傅立叶变换】
- UVA10716 - Evil Straw Warts Live
- 微信分享获得积分 jquery微信分享获得积分 原理
- 全栈工程师的悲与欢
- android如果给imageview做圆角,如果在原有的bitmap上加上一些修饰的drawable
- 递归理解
- 查找字符串出现次数
- 本周学习积累(pgp加密,通道机,secureCRT)
- 由内存复制函数看assert宏的使用
- 不要62(杭电2089)
- getCurrentSession与getSession之关系
- QT 表格设置左上角按钮
- oracle 发送邮件