Android 给ImageView自定义设置边框
16lz
2021-01-23
1.使用布局方式来实现:
<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/white"> <TableRow android:layout_width="wrap_content"> <ImageView android:id="@+id/img" android:layout_width="90px" android:layout_height="90px" android:layout_margin="2dip" android:scaleType="centerCrop" /> </TableRow> </TableLayout>
说明:1、是把ImageView放在一个TableLayout中来实现的。
2、要设置ImageView的大小,就是长和宽(宽必须要设置)。
3、设置android:scaleType="centerCrop"属性, 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)。
4、设置ImageView的android:layout_margin="2dip"属性。
5、设置TableLayout的android:background="@drawable/white"属性。
2,。通过自定义的方式来实现
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:shadow="http://shadow.com" android:background="@drawable/bg_newslist" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:id="@id/LinerLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" > <shadow.widget.myImageView android:id="@id/newsitemPicWithBorder" shadow:BorderColor="GRAY" android:layout_width="80px" android:layout_height="60px" android:layout_alignParentRight="true" android:src="@drawable/image_loading" android:layout_centerInParent="true" android:layout_marginRight="3px" ></shadow.widget.myImageView></shadow.widget.myImageView
java代码
package shadow.widget; 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 myImageView extends ImageView { private String namespace="http://shadow.com"; private int color; public myImageView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub color=Color.parseColor(attrs.getAttributeValue(namespace, "BorderColor")); } /* (non-Javadoc) * @see android.widget.ImageView#onDraw(android.graphics.Canvas) */ @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); //画边框 Rect rec=canvas.getClipBounds(); rec.bottom--; rec.right--; Paint paint=new Paint(); paint.setColor(color); paint.setStyle(Paint.Style.STROKE); canvas.drawRect(rec, paint); }
这里要注意的是super.onDraw(canvas);在前,否则边框可能会被图片所覆盖。
设置边框颜色 shadow:BorderColor="GRAY"
myImageView imageView=(myImageView)findViewById(....);imageView.set....//给imageView赋值
更多相关文章
- #Android源代码#android:onClick属性的底层调用
- 详解Android TextView属性ellipsize多行失效的解决思路
- android TextView控件属性列表
- [置顶] EditText属性大全
- android控件常用属性区别
- android:configChanges属性(转帖)