android给图片添加边框
16lz
2021-01-26
方案一:继承ImageView,在onDraw重写画图函数,在图片边延画条灰色的线
方案二:在灰色背景下,内缩2个像索,再内嵌个图片。
view plain- packagecom.xmz.activity;
- importandroid.content.Context;
- importandroid.graphics.Canvas;
- importandroid.graphics.Color;
- importandroid.graphics.Paint;
- importandroid.graphics.Rect;
- importandroid.util.AttributeSet;
- importandroid.widget.ImageView;
- publicclassImageViewBorderextendsImageView{
- privateStringnamespace="http://xmz.com";
- privateintcolor;
- publicImageViewBorder(Contextcontext,AttributeSetattrs){
- super(context,attrs);
- color=Color.parseColor(attrs.getAttributeValue(namespace,"BorderColor"));
- }
- @Override
- protectedvoidonDraw(Canvascanvas){
- super.onDraw(canvas);
- //画边框暂时去除小边框
- Rectrec=canvas.getClipBounds();
- rec.bottom--;
- rec.right--;
- Paintpaint=newPaint();
- paint.setColor(color);
- paint.setStyle(Paint.Style.STROKE);
- canvas.drawRect(rec,paint);
- }
- }
view plain
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:xmz="http://xmz.com"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="方案一:"
- />
- <com.xmz.activity.ImageViewBorder
- android:layout_width="124px"
- android:layout_height="180px"
- android:background="@drawable/movie"
- xmz:BorderColor="GRAY"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="方案二:"
- />
- <LinearLayout
- android:layout_width="124px"
- android:layout_height="180px"
- android:background="@drawable/image_bg"
- android:padding="2px">
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/movie"
- />
- </LinearLayout>
- </LinearLayout>
更多相关文章
- Android(安卓)Log 配置解决方案
- Android(安卓)UI设计中的三种特效
- 【翻译】4种经典的Android屏幕自适应解决方案
- android 热修复 HotFix 混淆apk生成patch包方案
- ionic 打包android@6出现xml/network_security_config not found
- android 获取视频缩略图终极解决方案(ffmpeg)
- Errors running builder 'Android(安卓)Resource Manager' on Pr
- Android(安卓)color
- Android(安卓)Studio 编译卡慢、卡顿的几种解决方案