zoukankan      html  css  js  c++  java
  • ImageView设置边框的两种方式

    转载:http://www.2cto.com/kf/201308/239945.html

    package cc.testimageviewbounds;
     
    import android.os.Bundle;
    import android.app.Activity;
    /**
     * Demo描述:
     * 给ImageView添加边框的两种实现方式
     * 
     * 方式一:
     * 利用自定义的shape-->即此处的imageviewboundshape.xml
     * 且为ImageView设置background,即代码:
     * android:background="@drawable/imageviewboundshape"
     * 
     * 方式二:
     * 自定义ImageView
     *
     */
    public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    }

    方法一:

    <?xml version="1.0" encoding="utf-8"?>
        <!-- 定义矩形rectangle -->
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:shape="rectangle"  >
        
        <!-- 设置边框的大小和颜色 -->
        <stroke android:width="3dip" android:color="#ff0000" /> 
         
        <!-- 设置矩形内的颜色,此处为透明色 -->
        <solid android:color="@android:color/transparent"/>
         
        <!-- 定义圆角弧度 -->
        <corners
            android:bottomLeftRadius="4dp"
            android:bottomRightRadius="4dp"
            android:topLeftRadius="4dp"
            android:topRightRadius="4dp"
        />
         
    </shape>

    方法二:

    package cc.testimageviewbounds;
     
    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 ImageViewSubClass extends ImageView {
     
        public ImageViewSubClass(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
     
        public ImageViewSubClass(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
     
        public ImageViewSubClass(Context context) {
            super(context);
        }
         
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
             
            //获取控件需要重新绘制的区域
            Rect rect=canvas.getClipBounds();
            rect.bottom--;
            rect.right--;
            Paint paint=new Paint();
            paint.setColor(Color.RED);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(3);
            canvas.drawRect(rect, paint);
        }
        
    }

    布局文件:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       >
     
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="给ImageView添加边框的两种方式"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="65dip"
         />
        <ImageView
            android:id="@+id/firstImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="150dip"
            android:background="@drawable/imageviewboundshape"
            />
        <cc.testimageviewbounds.ImageViewSubClass
             android:id="@+id/secondImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="250dip"
            />
         
    </RelativeLayout>
  • 相关阅读:
    LeetCode周赛#209
    CodeBlocks相关配置
    LeetCode双周赛#36
    LCCUP 2020 秋季编程大赛 补题
    while和do while区别
    完全背包--piggy-bank
    数组定义在哪里???重要!!!
    01背包---点菜问题
    java源程序命名规则
    hdu1159-----最长公共子序列LCS
  • 原文地址:https://www.cnblogs.com/AndroidJotting/p/4778739.html
Copyright © 2011-2022 走看看