Android:给图片加边框

来源:互联网 发布:淘宝手机充值漏洞 编辑:程序博客网 时间:2024/06/05 17:38

Android学习笔记进阶19之给图片加边框

?
1
2
3
4
5
6
7
8
9
//设置颜色
    publicvoidsetColour(intcolor){
        co = color;
    }
    //设置边框宽度
    publicvoidsetBorderWidth(intwidth){
         
        borderwidth = width;
    }
具体实现:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
packagexiaosi.imageborder;
 
importandroid.app.Activity;
importandroid.graphics.Color;
importandroid.os.Bundle;
 
publicclassImageBorderActivity extendsActivity {
    /** Called when the activity is first created. */
    privatemyImageView image = null;
    privatemyImageView image1 = null;
    @Override
    publicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        image = (myImageView)findViewById(R.id.iamge);
        image.setColour(Color.YELLOW);
        image.setBorderWidth(10);
        image1 = (myImageView)findViewById(R.id.iamge1);
        image1.setColour(Color.GREEN);
        image1.setBorderWidth(5);
    }
}
main.xml
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/playerbackground"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
     <xiaosi.imageborder.myImageView
         android:id="@+id/iamge"
         android:layout_width="200px"
         android:layout_height="230px"
         android:layout_alignParentRight="true"
         android:src="@drawable/v"
         android:layout_centerInParent="true"
         android:layout_marginRight="3px"
        />
     <xiaosi.imageborder.myImageView
         android:id="@+id/iamge1"
         android:layout_width="200px"
         android:layout_height="230px"
         android:layout_alignParentRight="true"
         android:src="@drawable/v"
         android:layout_centerInParent="true"
         android:layout_marginRight="3px"
        />
</LinearLayout>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
packagexiaosi.imageborder;
 
importandroid.content.Context;
importandroid.graphics.Canvas;
importandroid.graphics.Paint;
importandroid.graphics.Rect;
importandroid.util.AttributeSet;
importandroid.widget.ImageView;
 
publicclassmyImageView extendsImageView {
 
    privateintco;
    privateintborderwidth;
    publicmyImageView(Context context) {
        super(context);
    }
    publicmyImageView(Context context, AttributeSet attrs,
            intdefStyle) {
        super(context, attrs, defStyle);
    }
 
    publicmyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    //设置颜色
    publicvoidsetColour(intcolor){
        co = color;
    }
    //设置边框宽度
    publicvoidsetBorderWidth(intwidth){
         
        borderwidth = width;
    }
    @Override
    protectedvoidonDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 画边框
        Rect rec = canvas.getClipBounds();
        rec.bottom--;
        rec.right--;
        Paint paint = newPaint();
        //设置边框颜色
        paint.setColor(co);
        paint.setStyle(Paint.Style.STROKE);
        //设置边框宽度
        paint.setStrokeWidth(borderwidth);
        canvas.drawRect(rec, paint);
    }
}
0 0
原创粉丝点击