ImageView设置边框的两种方式
转载: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设置边框的两种方式的更多相关文章
- java中设置代理的两种方式
1 前言 有时候我们的程序中要提供可以使用代理访问网络,代理的方式包括http.https.ftp.socks代理.比如在IE浏览器设置代理. 那我们在我们的java程序中使用代理呢,有如下两种方式. ...
- 【iOS开发-72】设置状态栏的两种方式、程序生命周期以及更好地理解几大类(对象)之间的关系
(1)设置状态栏的2种方式 --第一种方式就是我们在控制器中设置,系统默认就是交给视图控制器去管理的,这样不同视图控制器能够自己定义不同的状态栏例如以下: -(BOOL)prefersStatusBa ...
- CSS设置透明的两种方式
1..demo{ background-color:transparent; } 2..demo{ background-color:rgba(0,0,0,0.5); //最后一个参数是用来设置透明度 ...
- [Android]解决3gwap联网失败:联网请求在设置代理与直连两种方式的切换
[Android]解决3gwap联网失败:联网请求在设置代理与直连两种方式的切换 问题现象: 碰到一个问题,UI交互表现为:联通号码在3gwap网络环境下资源一直无法下载成功. 查看Log日志,打印出 ...
- HTML中设置背景图的两种方式
HTML中设置背景图的两种方式 1.background background:url(images/search.png) no-repeat top; 2.background-image ...
- [Swift]Alamofire:设置网络请求超时时间【timeout】的两种方式
两种方式作用相同,是同一套代码的两种表述. 第一种方式:集聚. 直接设置成员属性(全局属性),这种方法不能灵活修改网络请求超时时间timeout. 声明为成员属性: // MARK: - 设置为全局变 ...
- linux 设置开机启动项两种方式
原文链接:http://blog.csdn.net/karchar/article/details/52489572 有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务. 在解问题之前 ...
- /*透明度设置的两种方式,以及hover的用法,fixed,(relative,absolute)这两个一起用*/
<!DOCTYPE html> /*透明度设置的两种方式,以及hover的用法,fixed,(relative,absolute)这两个一起用*/ <html lang=" ...
- flask框架--设置配置文件的几种方式 与Flask两种配置路由的方式
设置配置文件的几种方式 ==========方式一:============ app.config['SESSION_COOKIE_NAME'] = 'session_lvning' #这种方式要把所 ...
随机推荐
- C语言_第五章__实践(密码转换)
1. 要求 输入China 输出 Glmre #include <stdio.h> #include <stdlib.h> int main() { char c ; c ...
- nginx android app 慢网络请求超时
最近遇到了android 在慢网络下面请求服务器报 java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by ...
- MVC项目中ExecutionTimeout不生效的解决方案
我们做web服务器端开发时,经常会遇到一个需求场景,因为某些耗时处理造成页面的响应处理时间超长,技术角度就想能否给页面处理程序一个指定的超时时间,服务端处理程序执行时间超过这个指定的超时时间则中断处理 ...
- USER STORIES AND USE CASES - DON’T USE BOTH
We’re in Orlando for a working session as part of the Core Team building BABOK V3 and over dinner th ...
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- Spring4 实例
结构目录如下: 其中: dao层和entity层都属于hibernate的的管辖.entity层里面装的是每张表对应的持久化类.dao层里面装的是"底层操作数据库的行为",仅仅只是 ...
- Usaco*Brownie Slicing
Description Bessie烘焙了一块巧克力蛋糕.这块蛋糕是由R*C(1 <= R,C <= 500)个小的巧克力蛋糕组成的. 第i行,第j列的蛋糕有N_ij(1 <= N_ ...
- JavaScript鼠标经过图片的放大镜效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 一些linux包相关命令
针对centos 查看CentOS版本方法: lsb_release -a #result------------ LSB Version: :base-4.0-amd64:base-4.0-noar ...
- 推荐几个Android自定义的进度条(转载)
CustomLoading ElasticDownload Circle-Progress-View lzyzsdCircleProgress SquareProgressBar materialis ...