
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 myImageView extends ImageView {
private String namespace="http://shadow.com"
private int color
public myImageView(Context context, AttributeSet attrs) {
super(context, attrs)
// TODO Auto-generated constructor stub
color=Color.parseColor(attrs.getAttributeValue(namespace, "BorderColor"))
}
/* (non-Javadoc)
* @see android.widget.ImageView#onDraw(android.graphics.Canvas)
*/
@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)
paint.setStyle(Paint.Style.STROKE)
canvas.drawRect(rec, paint)
}
这里要注意的是super.onDraw(canvas)在前,否则边框可能会被图片所覆盖。
xml写法
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:shadow="http://shadow.com"
android:background="@drawable/bg_newslist"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:id="@id/LinerLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<shadow.widget.myImageView
android:id="@id/newsitemPicWithBorder"
shadow:BorderColor="GRAY"
android:layout_width="80px"
android:layout_height="60px"
android:layout_alignParentRight="true"
android:src="@drawable/image_loading"
android:layout_centerInParent="true"
android:layout_marginRight="3px"
></shadow.widget.myImageView>
</LinearLayout>
设置边框颜色 shadow:BorderColor="GRAY"
myImageView imageView=(myImageView)findViewById(....)
imageView.set....//给imageView赋值
当在做项目app的开发时,UI会经常设计一些不同形状的头像显示,一般常见的形状为圆形,当然 也有省事的正方形,还有一些UI会设计头像为其他形状,本文讲解的就是头像或者类似排行榜之类的六边形,六边形也是常见的形状,以下要讲的就是如何绘制六边形,并给六边形加个边框.我这里给ImageView添加分类,将分类的方法写在分类中,方便创建多个多边形,这里也少不了使用UIBezierPath
在UIImageView+KWSexangle.h公开一个方法
这样就可以绘制完成带有边框的六边形ImageView了,当然带不带边框就有你决定了.
近来App中按钮要做统一话,如图所示框住部分为可点击区域,显示区域则为白色圆角框区域~~
根据上图表示,实现思路有
2.3 对button的layer做处理
基于2.2可知,对layer层直接做处理是可行的,只是由于button对titlelabel做的处理,导致titlelabel的frame和文本的高宽一致,不能直接使用其layer处理,但是如果我们得知了titlelabel的frame,直接对此frame进行宽高的增加,中心点不变,来生成一个新的layer放到button上。这样处理下,圆角边框仅仅和button的内容相关,则button可以任意设置宽高,进而点击区域就不会被button的内容展示限制,简直完美。
红色为可点击区域,虽然在项目中 没有用到 需要将button的imageview和titlelabel一起框起来的需求,但是在实际实现中,也将其实现进去了。
PS:同时也实现了安卓button自带的点击背景色改变效果~ 没办法,老大说要和安卓一样。。我有一句·····
如有问题-联系邮箱 357863248@qq.com .
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)