- 论坛徽章:
- 0
|
android 带边框的ImageView
参考:http://topic.csdn.net/u/20110224 ... c5d9.html?558626276
http://topic.csdn.net/u/20110224 ... c5d9.html?558626276
http://407827531.iteye.com/blog/1177818
给一张图片加边框 大概有两做法
第一 :
我们知道ImageView里面 有两个属性 一个是background 一个是src 就可以用这两个来加边框
backgroud的图片比src的图片要稍大 这样 就可以把background多余的部分露出来 当边框了 呵呵
Java代码- 1.android:src= "@android:drawable/btn_default"
- 2. android:background= "#FFFF0000"
- 3. android:scaleType= "fitCenter"
- android:src= "@android:drawable/btn_default"
- android:background= "#FFFF0000"
- android:scaleType= "fitCenter"
复制代码 第二:
也是大多数人用到的方法 自定义一个ImageView
Java代码- 1.public class VideoBorderImageView extends ImageView {
- 2.
- 3. public VideoBorderImageView(Context context) {
- 4. super(context);
- 5. // TODO Auto-generated constructor stub
- 6. }
- 7.
- 8. public VideoBorderImageView(Context context, AttributeSet attrs,
- 9. int defStyle) {
- 10. super(context, attrs, defStyle);
- 11. // TODO Auto-generated constructor stub
- 12. }
- 13.
- 14. public VideoBorderImageView(Context context, AttributeSet attrs) {
- 15. super(context, attrs);
- 16. // TODO Auto-generated constructor stub
- 17. }
- 18.
- 19. @Override
- 20. protected void onDraw(Canvas canvas) {
- 21. // TODO Auto-generated method stub
- 22. super.onDraw(canvas);
- 23.
- 24. // 画边框
- 25. Rect rec = canvas.getClipBounds();
- 26. rec.bottom--;
- 27. rec.right--;
- 28. Paint paint = new Paint();
- 29. paint.setColor(Color.GRAY);
- 30. paint.setStyle(Paint.Style.STROKE);
- 31. paint.setStrokeWidth(5);
- 32. canvas.drawRect(rec, paint);
- 33. }
- public class VideoBorderImageView extends ImageView {
- public VideoBorderImageView(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- }
- public VideoBorderImageView(Context context, AttributeSet attrs,
- int defStyle) {
- super(context, attrs, defStyle);
- // TODO Auto-generated constructor stub
- }
- public VideoBorderImageView(Context context, AttributeSet attrs) {
- super(context, attrs);
- // TODO Auto-generated constructor stub
- }
- @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.GRAY);
- paint.setStyle(Paint.Style.STROKE);
- paint.setStrokeWidth(5);
- canvas.drawRect(rec, paint);
- }
复制代码 xml
Java代码- 1.<com.XXX.VideoBorderImageView
- 2. android:layout_marginTop="20dip"
- 3. android:layout_width="176px"
- 4. android:layout_height="144px"
- 5. android:id="@+id/iv_room_tab_video"/>
复制代码 |
|