自定义控件的用法
来源:互联网 发布:黑魂女性捏脸数据 编辑:程序博客网 时间:2024/04/28 08:54
效果图:圆角头像就是个自定义控件,它是个继承自ImageView的类,使用方法是在xml里写完整的包名,类名
<LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center"
android:orientation="horizontal" >
<com.handaer.hbdpurse.widget.RoundImageView
android:id="@+id/iv_info_icon"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/user_head_bg"
android:scaleType="centerInside"
android:src="@drawable/default_head_icon" />
<TextView
android:id="@+id/tv_info_phone"
style="@style/shadow_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textColor="#ffffff"
android:textSize="26sp" />
</LinearLayout>
public class RoundImageView extends ImageView {
//构造方法重载方法一
public RoundImageView(Context context) {
this(context, null, 0);
}
//构造方法重载方法二
public RoundImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
//构造方法重载方法三
public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
getRoundedCornerBitmap(((BitmapDrawable) getDrawable()).getBitmap());
}
//设置一个位图作为这个ImageView内容。
@Override
public void setImageBitmap(Bitmap bm) {
getRoundedCornerBitmap(bm);//获得圆角图片的方法
}
private void setImageBitmap(Bitmap bm, boolean isRound) {
if (isRound) {
super.setImageBitmap(bm);
}
}
//设置一个drawable作为这个ImageView内容。
@Override
public void setImageResource(int resId) {
Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(),
resId);
getRoundedCornerBitmap(originalBitmap);
}
/**
* 获得圆角图片的方法
*
* @param bitmap
* @param roundPx
* @return
*/
private void getRoundedCornerBitmap(Bitmap bitmap) {
//获得宽高
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int size = Math.min(w, h);
Bitmap output = Bitmap
.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Paint paint = new Paint(1);
paint.setColor(0xfff6f6f6);
RectF rectf = new RectF(0F, 0F, size, size);
canvas.drawOval(rectf, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
canvas.drawBitmap(bitmap, 0, 0, paint);
this.setImageBitmap(output, true);
}
}
- 自定义控件的用法
- android中自定义控件的用法
- android中自定义控件的用法
- Android自定义控件之虚线的用法
- TypedArray的用法和declare-styleable:自定义控件的属性
- ShadowLayout的用法--自定义控件--按钮---水波纹的效果
- TypedArray的用法和declare-styleable:自定义控件的属性
- ScriptControl控件的用法--让VB执行自定义字符串命令
- PropertyGrid用法,有助于自定义控件的property实现.
- ExpandLayout用法和封装自定义控件的一些小记
- MVC中@Html.Action的用法(类似自定义控件)
- 自定义控件-1.基本用法
- 自定义控件-1.基本用法
- 自定义控件-3.加强用法
- android 自定义控件TextInputSelectBox用法
- 安卓自定义控件、自定义xml里的属性的用法
- Android常用控件之Toast与AlertDialog的常规用法和自定义用法
- 自定义控件的自定义属性
- @RequestMapping 用法详解之地址映射
- Java对对象的4种引用
- Cache的设计和实现 LRU Cache
- python中列表、元祖和字典的用法
- 奋斗者的觉醒
- 自定义控件的用法
- 3个开源TTS(三)flite的简要分析与espeak的选择
- MFC 窗口框架类CFrameWnd的派生类CMainFrame类
- Linux mount命令
- Android 中的接口回调
- MFC资源学习
- Single Number -- LeetCode
- 【intent】确定Intent能否解析
- 流式多处理器(streamingMultiproeessor,SM)与流处理器(strcamingProcessor, sp)