转载:http://www.2cto.com/kf/201308/239945.html

package cc.testimageviewbounds;import android.os.Bundle;
import android.app.Activity;
/*** Demo描述:* 给ImageView添加边框的两种实现方式* * 方式一:* 利用自定义的shape-->即此处的imageviewboundshape.xml* 且为ImageView设置background,即代码:* android:background="@drawable/imageviewboundshape"* * 方式二:* 自定义ImageView**/
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);}
}

方法一:

<?xml version="1.0" encoding="utf-8"?><!-- 定义矩形rectangle -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"  ><!-- 设置边框的大小和颜色 --><stroke android:width="3dip" android:color="#ff0000" /> <!-- 设置矩形内的颜色,此处为透明色 --><solid android:color="@android:color/transparent"/><!-- 定义圆角弧度 --><cornersandroid:bottomLeftRadius="4dp"android:bottomRightRadius="4dp"android:topLeftRadius="4dp"android:topRightRadius="4dp"/></shape>

方法二:

package cc.testimageviewbounds;import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ImageView;public class ImageViewSubClass extends ImageView {public ImageViewSubClass(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public ImageViewSubClass(Context context, AttributeSet attrs) {super(context, attrs);}public ImageViewSubClass(Context context) {super(context);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);//获取控件需要重新绘制的区域Rect rect=canvas.getClipBounds();rect.bottom--;rect.right--;Paint paint=new Paint();paint.setColor(Color.RED);paint.setStyle(Paint.Style.STROKE);paint.setStrokeWidth(3);canvas.drawRect(rect, paint);}}

布局文件:

<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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="给ImageView添加边框的两种方式"android:layout_centerHorizontal="true"android:layout_marginTop="65dip"/><ImageViewandroid:id="@+id/firstImageView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_launcher"android:layout_centerHorizontal="true"android:layout_marginTop="150dip"android:background="@drawable/imageviewboundshape"/><cc.testimageviewbounds.ImageViewSubClassandroid:id="@+id/secondImageView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_launcher"android:layout_centerHorizontal="true"android:layout_marginTop="250dip"/></RelativeLayout>

转载于:https://www.cnblogs.com/AndroidJotting/p/4778739.html

ImageView设置边框的两种方式相关推荐

  1. html正方形边框,详解HTML设置边框的三种方式

    HTML设置边框的三种方式 border-width: 1px 2px 2px; border-style: solid dashed dotted; border-color:red green b ...

  2. HTML设置边框的三种方式

    HTML设置边框的三种方式 文章目录 HTML设置边框的三种方式 1.边框的组成: 2.复合样式 3.单一设置 这样代表上边框 右边框 下边框 左边框 分别对四条边框进行设置 两个值代表:上下边框 左 ...

  3. ImageView设置图片的几种方式

    ImageView设置图片的方式有很多种 一:在xml里面设置 设置前景:android:src="@drawable/xxx" 设置背景:android:background=& ...

  4. android线程优先级大小,android 设置线程优先级 两种方式

    1) android.os.Process.setThreadPriority (int priority)或android.os.Process.setThreadPriority (int tid ...

  5. html正方形边框,详解HTML设置边框的三种方式(html画方框用border)

    代码如下 <!DOCTYPE html> <html>     <head>         <style>             .square{ ...

  6. 给ImageView 添加阴影的两种方式

    1.通过设置背景图 background <ImageView                         android:id="@+id/img_cover"     ...

  7. JAVA设置代理的两种方式(HTTP和HTTPS)

    一.直接设置系统属性,设置后所有网络请求都有效 System.setProperty("proxyType", "4");System.setProperty( ...

  8. JS设置样式的两种方式

    a) ClassName/style------DOM的style属性只能获取标签中使用style设置的样式,无法获取潜入或者外部样式,style.cssText获取style里面的字符串(只能在行内 ...

  9. imageview设置边框

    对于imageview设置边框有几种方法,其中一种可以直接设置带边框的图片,但是一般做项目的时候获得都是服务器传过来的图片,此时将图片下载下来再设置边框,可以当然可以,但是维护太麻烦,此时可以用一种很 ...

  10. Android点击图片随机,android 设置图片随机出现-两种方式

    android 设置图片随机出现-两种方式, 第一种方式:得到图片对应的Drawable实例,通过setImageDrawable(drawable)实现 //ImageView对应的id ivBg ...

最新文章

  1. 《PHP对象、模式与实践》之高级特性
  2. Code for City 黑客松 | 成都站启动
  3. 2020Alibaba数学竞赛预选赛第二轮参考答案
  4. F. It‘s a bird! No, it‘s a plane! No, it‘s AaParsa!
  5. 微信小程序之发送模板消息(通过openid推送消息给用户)
  6. php不发送referer,php – 注意:未定义的索引:HTTP_REFERER
  7. 编译型语言VS解释型语言、动态语言、静态语言
  8. PHP二维数组:统计某个键名的相同键值个数
  9. Dijkstra 算法+fibonacci堆的逐步c实现
  10. 微信Java SDK开发文档
  11. 利用PTGui制作全景图并用Pano2VR生成SWF文件
  12. 数学分析高等代数考研试题不断更新中
  13. github python100天_GitHub - ychgithub/Python-100-Days: Python - 100天从新手到大师
  14. Could not find goal ‘assembly‘ in plugin org.apache.maven.plugins:maven-assembly-plugin: among avai
  15. unity静止人物素材制作动作,导入unity行走、跑步动画,并用Animator Controller加脚本进行控制
  16. 怎样去识别是否双线主机服务器的方法
  17. 关于abd.exe 报错的解决方法总结
  18. vs2022 c#调用interop.word 12.0版本也就是word2007实现首行缩进两个字符
  19. 买二送一跟买三免一有区别吗?
  20. 阿语python美多商城-商品-购物车之第7.1节购物车存储方案

热门文章

  1. App调试内存泄露之Context篇(下)-App调试内存泄露之Context篇(下)
  2. 牛皮啊!竟然可以为Dubbo接口生成文档了!
  3. Java高并发之设计模式,设计思想!
  4. 如何进入腾讯、网易、阿里这样的互联网公司,看到第二条我就秒懂了~
  5. 董明珠人设崩塌了吗?
  6. 月薪 1 万和 10 万的人,到底差在哪儿?
  7. linux中级之netfilter防火墙(firewalld)
  8. Python攻克之路-生成器
  9. String的indexOf()用于获取字符串中某个子字符串的位置
  10. Hive编程指南pdf