需要重写ImageView

1 //设置颜色

2 public void setColour(int color){
3 co = color;
4 }
5 //设置边框宽度
6 public void setBorderWidth(int width){
7
8 borderwidth = width;
9 }

具体实现:

01 package xiaosi.imageborder;
02
03 import android.app.Activity;
04 import android.graphics.Color;
05 import android.os.Bundle;
06
07 public class ImageBorderActivity extends Activity {
08 /** Called when the activity is first created. */
09 private myImageView image = null;
10 private myImageView image1 = null;
11 @Override
12 public void onCreate(Bundle savedInstanceState) {
13 super.onCreate(savedInstanceState);
14 setContentView(R.layout.main);
15
16 image = (myImageView)findViewById(R.id.iamge);
17 image.setColour(Color.YELLOW);
18 image.setBorderWidth(10);
19 image1 = (myImageView)findViewById(R.id.iamge1);
20 image1.setColour(Color.GREEN);
21 image1.setBorderWidth(5);
22 }
23 }

main.xml

01 <LinearLayout
02 xmlns:android="http://schemas.android.com/apk/res/android"
03 android:background="@drawable/playerbackground"
04 android:layout_width="fill_parent"
05 android:layout_height="fill_parent">
06 <xiaosi.imageborder.myImageView
07 android:id="@+id/iamge"
08 android:layout_width="200px"
09 android:layout_height="230px"
10 android:layout_alignParentRight="true"
11 android:src="@drawable/v"
12 android:layout_centerInParent="true"
13 android:layout_marginRight="3px"
14 />
15 <xiaosi.imageborder.myImageView
16 android:id="@+id/iamge1"
17 android:layout_width="200px"
18 android:layout_height="230px"
19 android:layout_alignParentRight="true"
20 android:src="@drawable/v"
21 android:layout_centerInParent="true"
22 android:layout_marginRight="3px"
23 />
24 </LinearLayout>
01 package xiaosi.imageborder;
02
03 import android.content.Context;
04 import android.graphics.Canvas;
05 import android.graphics.Paint;
06 import android.graphics.Rect;
07 import android.util.AttributeSet;
08 import android.widget.ImageView;
09
10 public class myImageView extends ImageView {
11
12 private int co;
13 private int borderwidth;
14 public myImageView(Context context) {
15 super(context);
16 }
17 public myImageView(Context context, AttributeSet attrs,
18 int defStyle) {
19 super(context, attrs, defStyle);
20 }
21
22 public myImageView(Context context, AttributeSet attrs) {
23 super(context, attrs);
24 }
25 //设置颜色
26 public void setColour(int color){
27 co = color;
28 }
29 //设置边框宽度
30 public void setBorderWidth(int width){
31
32 borderwidth = width;
33 }
34 @Override
35 protected void onDraw(Canvas canvas) {
36 super.onDraw(canvas);
37 // 画边框
38 Rect rec = canvas.getClipBounds();
39 rec.bottom--;
40 rec.right--;
41 Paint paint = new Paint();
42 //设置边框颜色
43 paint.setColor(co);
44 paint.setStyle(Paint.Style.STROKE);
45 //设置边框宽度
46 paint.setStrokeWidth(borderwidth);
47 canvas.drawRect(rec, paint);
48 }
49 }

更多相关文章

  1. 配置颜色文件color.xml
  2. Android(安卓)ListView实现Table行列效果
  3. Android实现沉浸式状态栏
  4. Android图形处理-Drawabble
  5. Android修改ActionBar背景颜色和标题,菜单颜色 (清晰)
  6. Android(安卓)OpenGL学习笔记(一)
  7. android画图——颜色过滤
  8. Dialog自定义Style
  9. 背景透明的 Dialog

随机推荐

  1. android通过HTTP协议上传文件至远程服务
  2. Anroid使用DefaultHttpClient访问Wap网络
  3. WebView的基本使用方法及 WebViewClient
  4. android 5.0 以上获取栈顶应用包名以及程
  5. android中AppWidgetManager
  6. Android(安卓)动画系统汇总
  7. 如何用 Gradle 构建 Android(安卓)工程项
  8. 自己动手编译Android(LineageOS)源码
  9. MTK(Android(安卓)N)设置SDCard为默认存储
  10. protobuf使用(一)android ndk 编译 protobu