如何给ImageView加边框
1个回答
推荐于2016-03-16 · 知道合伙人软件行家
关注
展开全部
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);在前,否则边框可能会被图片所覆盖。
xml写法
<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>
</LinearLayout>
设置边框颜色 shadow:BorderColor="GRAY"
myImageView imageView=(myImageView)findViewById(....);
imageView.set....//给imageView赋值
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);在前,否则边框可能会被图片所覆盖。
xml写法
<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>
</LinearLayout>
设置边框颜色 shadow:BorderColor="GRAY"
myImageView imageView=(myImageView)findViewById(....);
imageView.set....//给imageView赋值
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询