展会信息港展会大全

Android 给ImageView自定义设置边框
来源:互联网   发布日期:2015-10-03 11:21:07   浏览:3064次  

导读: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_heig......

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)

* @seeandroid.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"

myImageViewimageView=(myImageView)findViewById(....);

imageView.set....//给imageView赋值

赞助本站

人工智能实验室

相关热词: 边框 ImageView 自定义

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港