Android图片写成圆形ImageView
来源:互联网 发布:开淘宝店物流怎么弄 编辑:程序博客网 时间:2024/05/16 01:15
//定义圆形ImageView类,直接调用即可public class RoundImageView extends android.support.v7.widget.AppCompatImageView { public RoundImageView(Context context) { super(context); // TODO Auto-generated constructor stub } public RoundImageView(Context context, AttributeSet attrs) { super(context, attrs); } public RoundImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onDraw(Canvas canvas) { Drawable drawable = getDrawable(); if (drawable == null) { return; } if (getWidth() == 0 || getHeight() == 0) { return; } Bitmap b = ((BitmapDrawable)drawable).getBitmap(); if(null == b) { return; } Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true); int w = getWidth(), h = getHeight(); Bitmap roundBitmap = getCroppedBitmap(bitmap, w); canvas.drawBitmap(roundBitmap, 0,0, null); } public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) { Bitmap sbmp; if(bmp.getWidth() != radius || bmp.getHeight() != radius) sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false); else sbmp = bmp; Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xffa19774; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight()); paint.setAntiAlias(true); paint.setFilterBitmap(true); paint.setDither(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(Color.parseColor("#BAB399")); canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f, sbmp.getWidth() / 2+0.1f, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(sbmp, rect, rect, paint); return output; }}
<LinearLayout android:id="@+id/linearLayout_DaShenZhuYe_2" android:layout_width="match_parent" android:layout_height="72dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:background="#FFFFFF" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:text="粉丝贡献榜" android:textSize="15dp" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical"> <com.example.qd.mydemo.RoundImageView android:id="@+id/right_login_head1" style="@style/image_dashen_photo" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/right_login_head2" android:layout_toStartOf="@+id/right_login_head2" android:src="@drawable/back" /> <com.example.qd.mydemo.RoundImageView android:id="@+id/right_login_head2" style="@style/image_dashen_photo" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/right_login_head3" android:layout_toStartOf="@+id/right_login_head3" android:src="@drawable/back" /> <com.example.qd.mydemo.RoundImageView android:id="@+id/right_login_head3" style="@style/image_dashen_photo" android:layout_marginRight="5dp" android:layout_toLeftOf="@+id/imageView_DaShenZhuYe" android:layout_toStartOf="@+id/imageView_DaShenZhuYe" android:src="@drawable/back" /> <TextView android:id="@+id/imageView_DaShenZhuYe" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentRight="true" android:layout_marginRight="5dp" android:gravity="center" android:text="更多" android:textColor="#000000" android:textSize="10dp" /> </RelativeLayout></LinearLayout>
阅读全文
0 0
- Android图片写成圆形ImageView
- Android ImageView圆形头像 图片完全解析
- Android ImageView实现圆角,圆形图片
- android自定义ImageView实现圆形图片
- Android 圆形、圆角图片ImageView
- Android ImageView圆形头像 图片完全解析
- Android之自定义ImageView圆形图片
- android 自定义ImageView实现圆形压缩图片
- Xamarin Android Circle ImageView 圆形图片实现
- Android ImageView圆形头像 图片完全解析
- Android自定义ImageView实现圆形图片
- android 自定义ImageView显示圆形图片
- imageview显示圆形图片
- ImageView 圆形图片
- 自定义圆形图片 ImageView
- ImageView图片圆形显示
- ImageView自定义圆形图片
- Android:继承ImageView 实现 圆角图片,圆形图片处理。
- 最简单的Vuex示例
- 新思固件整合方法
- ModelMapper
- c++需要补充的知识点整理
- osgEarth矢量属性过滤案例 40. feature_custom_filters.earth
- Android图片写成圆形ImageView
- java8 toMap(key重复如何解决)
- Maven
- HDU 6134 Battlestation Operational 2017多校8 莫比乌斯反演
- show slave status\G中的Read_Master_Log_Pos和Relay_Log_Pos的(大小)关系
- 人事管理系统第一天之整体设计
- MVP模式实例讲解
- 分布式系统相关概念
- 解决IDEA run()时找不到加载类的问题