Android:给图片加边框

论坛 期权论坛 脚本     
匿名技术用户   2021-1-6 07:38   59   0

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

?
1
2
3
4
5
6
7
8
9
//设置颜色
public void setColour( int color){
co = color;
}
//设置边框宽度
public void setBorderWidth( int width){
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
package xiaosi.imageborder;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
public class ImageBorderActivity extends Activity {
/** Called when the activity is first created. */
private myImageView image = null ;
private myImageView image1 = null ;
@Override
public void onCreate(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
package xiaosi.imageborder;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ImageView;
public class myImageView extends ImageView {
private int co;
private int borderwidth;
public myImageView(Context context) {
super (context);
}
public myImageView(Context context, AttributeSet attrs,
int defStyle) {
super (context, attrs, defStyle);
}
public myImageView(Context context, AttributeSet attrs) {
super (context, attrs);
}
//设置颜色
public void setColour( int color){
co = color;
}
//设置边框宽度
public void setBorderWidth( int width){
borderwidth = width;
}
@Override
protected void onDraw(Canvas canvas) {
super .onDraw(canvas);
// 画边框
Rect rec = canvas.getClipBounds();
rec.bottom--;
rec.right--;
Paint paint = new Paint();
//设置边框颜色
paint.setColor(co);
paint.setStyle(Paint.Style.STROKE);
//设置边框宽度
paint.setStrokeWidth(borderwidth);
canvas.drawRect(rec, paint);
}
}
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP