android 实现图片的边框

 

方案一:继承ImageView,在onDraw  重写画图函数,在图片边延画条灰色的线

方案二:在灰色背景下,内缩2个像索,再内嵌个图片。

 

  1. package com.xmz.activity;  
  2. import android.content.Context;  
  3. import android.graphics.Canvas;  
  4. import android.graphics.Color;  
  5. import android.graphics.Paint;  
  6. import android.graphics.Rect;  
  7. import android.util.AttributeSet;  
  8. import android.widget.ImageView;  
  9. public class ImageViewBorder extends ImageView {  
  10.      private String namespace="http://xmz.com";  
  11.      private int color;  
  12.        
  13.      public ImageViewBorder(Context context, AttributeSet attrs) {  
  14.          super(context, attrs);  
  15.          color=Color.parseColor(attrs.getAttributeValue(namespace, "BorderColor"));  
  16.     }  
  17.         
  18.       @Override  
  19.       protected void onDraw(Canvas canvas) {  
  20.             
  21.           super.onDraw(canvas);      
  22.           //画边框  暂时去除小边框  
  23.           Rect rec=canvas.getClipBounds();  
  24.           rec.bottom--;  
  25.           rec.right--;  
  26.           Paint paint=new Paint();  
  27.           paint.setColor(color);  
  28.           paint.setStyle(Paint.Style.STROKE);  
  29.           canvas.drawRect(rec, paint);  
  30.       }  
  31. }  
 

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:xmz="http://xmz.com"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="wrap_content"  
  7.     >  
  8.     <TextView    
  9.         android:layout_width="fill_parent"   
  10.         android:layout_height="wrap_content"  
  11.         android:text="方案一:"  
  12.     />  
  13.     <com.xmz.activity.ImageViewBorder  
  14.         android:layout_width="124px"  
  15.         android:layout_height="180px"  
  16.         android:background="@drawable/movie"  
  17.         xmz:BorderColor="GRAY"  
  18.     />  
  19.     <TextView    
  20.         android:layout_width="fill_parent"   
  21.         android:layout_height="wrap_content"  
  22.         android:text="方案二:"  
  23.     />  
  24.       
  25.     <LinearLayout  
  26.         android:layout_width="124px"  
  27.         android:layout_height="180px"  
  28.         android:background="@drawable/image_bg"  
  29.         android:padding="2px">  
  30.         <ImageView  
  31.             android:layout_width="wrap_content"   
  32.             android:layout_height="wrap_content"  
  33.             android:background="@drawable/movie"      
  34.         />  
  35.     </LinearLayout>  
  36. </LinearLayout>  
 

 

工程的资源下载:http://download.csdn.net/source/3386837


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值