Android:通过自定义ImageView实现带边框的ImageView

来源:互联网 发布:win7网吧系统优化 编辑:程序博客网 时间:2024/05/21 09:37
  • 因为项目中有这样的需求,又不知道怎么解决。于是就在网上找资料,然后看到一个比较全面的介绍:
    http://evan0625.iteye.com/blog/1128249 ;里面的东西没有去验证。只是用了他提到的自定义ImageView的方式。结果很坑爹。写东西,不写全。直接放进项目根本不能用。一直报错。
  • 于是,我又翻山越岭,然后发现还要写一个attrs.xml。但是我又不会写。于是又翻山越岭,然后在:http://blog.csdn.net/xiaanming/article/details/42833893 里面看到了他写了一个attrs.xml刚好有一个设置边框的属性。于是我就将那个抄下来。然后结合上一个自定义ImageView,果然ok了。
  • 为了下次使用不那么麻烦,就将代码放这里了:
    首先是attrs.xml
<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="SingleTouchView">        <attr name="frameColor" format="color" />        <!-- 边框颜色 -->    </declare-styleable></resources>

接着就是自定义的ImageView

import android.annotation.SuppressLint;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Rect;import android.util.AttributeSet;import android.widget.ImageView;import com.zyitong.MobGuard_Pad.R;@SuppressLint("DrawAllocation")public class BorderImageView extends ImageView {    private static final int DEFAULT_FRAME_COLOR = 0xff0000;    private int frameColor;    public BorderImageView(Context context, AttributeSet attrs) {        super(context, attrs);        obtainStyledAttributes(attrs);    }    /**     * 获取自定义属性     *      * @param attrs     */    private void obtainStyledAttributes(AttributeSet attrs) {        TypedArray mTypedArray = getContext().obtainStyledAttributes(attrs,                R.styleable.SingleTouchView);        frameColor = mTypedArray.getColor(                R.styleable.SingleTouchView_frameColor, DEFAULT_FRAME_COLOR);        mTypedArray.recycle();    }    /*     * (non-Javadoc) <a href="http://my.oschina.net/u/244147" target="_blank"     * rel="nofollow">@see</a>     * android.widget.ImageView#onDraw(android.graphics.Canvas)     */    @SuppressLint("DrawAllocation")    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        // 画边框        Rect rec = canvas.getClipBounds();        rec.bottom--;        rec.right--;        Paint paint = new Paint();        paint.setColor(frameColor);        paint.setStyle(Paint.Style.STROKE);        canvas.drawRect(rec, paint);    }}

题外话:这个自定义的ImageView我也算是看懂了,就是将画笔放在控件的右下角,然后一点一点往左上角画,至于为什么没有画满控件,而是只是画了边框,倒是没有明白。

然后就是在自己的布局文件里面去使用了:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:duck="http://schemas.android.com/apk/res/com.aaa.bbb"    android:id="@+id/item_lay1_root"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <RelativeLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center" >            <com.aaa.bbb.ui.BorderImageView                android:id="@+id/item_lay1_iv"                android:layout_width="150dp"                android:layout_height="150dp"                android:layout_gravity="center"                android:layout_margin="0.1dp"                android:contentDescription="@null"                android:minHeight="50dp"                android:minWidth="50dp"                android:scaleType="centerCrop"                android:src="@drawable/bj_qq"                android:padding="2dp"                duck:frameColor="@color/grey" />            <CheckBox                android:id="@+id/item_lay1_cb"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_alignParentBottom="true"                android:layout_alignParentRight="true"                android:clickable="false"                android:visibility="gone" />        </RelativeLayout>        <ProgressBar            android:id="@+id/img_pb"            style="?android:attr/progressBarStyleHorizontal"            android:layout_width="fill_parent"            android:layout_height="2dp"            android:background="@drawable/btn_head" />    </LinearLayout></LinearLayout>

注意:xmlns:duck="http://schemas.android.com/apk/res/com.aaa.bbb"这里面的com.aaa.bbb
一定是当前应用的包名,否则没效果,或者直接报错。

通过以上3步,就算是圆满完成了给ImageView设置一个边框了。至于怎么控制边框的宽度,可能还需要去设置一下画笔的宽度,这个就不是很了解了。

0 0
原创粉丝点击