ImageView自定义带边框

ImageView设置边框的两种方式

     1、重写imageView:

  1. package org.xxx.picUpload.util;  
  2.   
  3.   
  4.   
  5. import android.content.Context;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Color;  
  8. import android.graphics.Paint;  
  9. import android.graphics.Rect;  
  10. import android.util.AttributeSet;  
  11. import android.widget.ImageView;  
  12.   
  13. public class MyImageView extends ImageView {  
  14.       
  15.     public MyImageView(Context context) {  
  16.         super(context);  
  17.         // TODO Auto-generated constructor stub   
  18.     }  
  19.   
  20.     public MyImageView(Context context, AttributeSet attrs,  
  21.             int defStyle) {  
  22.         super(context, attrs, defStyle);  
  23.         // TODO Auto-generated constructor stub   
  24.     }  
  25.   
  26.     public MyImageView(Context context, AttributeSet attrs) {  
  27.         super(context, attrs);  
  28.         // TODO Auto-generated constructor stub   
  29.     }  
  30.       
  31.     @Override  
  32.     protected void onDraw(Canvas canvas) {   //这里就是重写的方法了,想画什么形状自己动手   
  33.         // TODO Auto-generated method stub   
  34.         super.onDraw(canvas);  
  35.           
  36.         // 画边框   
  37.         Rect rec = canvas.getClipBounds();  
  38.         rec.bottom--;  
  39.         rec.right--;  
  40.         Paint paint = new Paint();  
  41.         paint.setColor(Color.GRAY);   //颜色   
  42.         paint.setStyle(Paint.Style.STROKE);  
  43.         paint.setStrokeWidth(5);  
  44.         canvas.drawRect(rec, paint);  
  45.     }  
  46.       
  47. }  

在xml文件中直接引用

  1. <org.xxx.picUpload.util.MyImageView  
  2.         android:id="@+id/imageView"      
  3.         android:layout_width="400px"  
  4.         android:layout_height="200px"/

     2、增加XML文件


<?xml version="1.0" encoding="utf-8"?>
    <!-- 定义矩形rectangle -->
       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>
在imgeview配置:
<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"
        /> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值