转载: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 {
@Override
protected 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"/> <!-- 定义圆角弧度 -->
<corners
android: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);
} @Override
protected 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();
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"
> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="给ImageView添加边框的两种方式"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dip"
/>
<ImageView
android: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.ImageViewSubClass
android: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>

ImageView设置边框的两种方式的更多相关文章

  1. java中设置代理的两种方式

    1 前言 有时候我们的程序中要提供可以使用代理访问网络,代理的方式包括http.https.ftp.socks代理.比如在IE浏览器设置代理. 那我们在我们的java程序中使用代理呢,有如下两种方式. ...

  2. 【iOS开发-72】设置状态栏的两种方式、程序生命周期以及更好地理解几大类(对象)之间的关系

    (1)设置状态栏的2种方式 --第一种方式就是我们在控制器中设置,系统默认就是交给视图控制器去管理的,这样不同视图控制器能够自己定义不同的状态栏例如以下: -(BOOL)prefersStatusBa ...

  3. CSS设置透明的两种方式

    1..demo{ background-color:transparent; } 2..demo{ background-color:rgba(0,0,0,0.5); //最后一个参数是用来设置透明度 ...

  4. [Android]解决3gwap联网失败:联网请求在设置代理与直连两种方式的切换

    [Android]解决3gwap联网失败:联网请求在设置代理与直连两种方式的切换 问题现象: 碰到一个问题,UI交互表现为:联通号码在3gwap网络环境下资源一直无法下载成功. 查看Log日志,打印出 ...

  5. HTML中设置背景图的两种方式

    HTML中设置背景图的两种方式 1.background    background:url(images/search.png) no-repeat top; 2.background-image ...

  6. [Swift]Alamofire:设置网络请求超时时间【timeout】的两种方式

    两种方式作用相同,是同一套代码的两种表述. 第一种方式:集聚. 直接设置成员属性(全局属性),这种方法不能灵活修改网络请求超时时间timeout. 声明为成员属性: // MARK: - 设置为全局变 ...

  7. linux 设置开机启动项两种方式

    原文链接:http://blog.csdn.net/karchar/article/details/52489572 有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务. 在解问题之前 ...

  8. /*透明度设置的两种方式,以及hover的用法,fixed,(relative,absolute)这两个一起用*/

    <!DOCTYPE html> /*透明度设置的两种方式,以及hover的用法,fixed,(relative,absolute)这两个一起用*/ <html lang=" ...

  9. flask框架--设置配置文件的几种方式 与Flask两种配置路由的方式

    设置配置文件的几种方式 ==========方式一:============ app.config['SESSION_COOKIE_NAME'] = 'session_lvning' #这种方式要把所 ...

随机推荐

  1. 网站常用UI整理

    1.UI插件包使用   Bootstrap 地址:http://v3.bootcss.com/getting-started/     Bootstrap buttons 按钮 地址:http://w ...

  2. VirtualPathProvider的使用

    解决的问题,加载数据库中的UI public class Global : System.Web.HttpApplication { protected void Application_Start( ...

  3. 关于C#开发WEB项目TextBox控件的自适应高问题解决办法!

    前两天做WEB开发时候遇到了一个小问题TextBox 控件要根据输入的内容多少自动改变大小,并且这个大小要求是在本页面内最大化.也就是 Width="100%" Height=&q ...

  4. post与get区别

    学习中,遇到get和post的提交方式,搜索整理了一下其区别: 关键词: PHP,Post,Get,区别 转载文章一: PHP中post与get的区别 Post 方法通过 HTTP post 机制,将 ...

  5. SQL Server

    1.通过触发器来级联删除: 具体的触发器代码如下: Create TRIGGER [dbo].[DeleteRelatedProducts] ON [dbo].[ProductCategory]  A ...

  6. ASP.NET MVC 和 Ruby 相结合的Web框架Oak

    在http://www.asp.net/mvc/open-source 上有个项目Oak: Frictionless development for ASP.NET MVC single page w ...

  7. 剑指Offer面试题:6.用两个栈实现队列

    一.题目:用两个栈实现队列 题目:用两个栈实现一个队列.队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能. 原文是使用 ...

  8. DevExpress GridControl使用方法

    一.如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二.如何新增一条记录 (1).gridView.AddN ...

  9. SQL Server 数据库设计规范

    数据库设计规范 1.简介 数据库设计是指对一个给定的应用环境,构造最优的数据库模式,建立数据库及其他应用系统,使之能有效地存储数据,满足各种用户的需求.数据库设计过程中命名规范很是重要,命名规范合理的 ...

  10. dofile执行ANDROID APK里面的文件

    我使用dofile执行APK文件是不行的,比如 dofile("assets/res/flist")只能先拷贝到writablePath然后再dofile拿到数据后再清除这个临时文 ...