自定义控件
2015-09-29 17:59
253 查看
自定义属性
1.在values新建文件attr<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyButtonAttrs"> <!-- dimension --> <attr name="MyWidth" format="dimension"/> <!-- enum --> <attr name="MyVisibility"> <enum name="visible" value="0" /> <enum name="invisible" value="1" /> <enum name="gone" value="2" /> </attr> <!-- boolean --> <attr name="MyBoolean" format="boolean" /> <!-- reference :drawable --> <attr name="Mybackground" format="reference|color" /> <attr name="MyTextColor" format="color" /> </declare-styleable> </resources>
2.在布局文件中使用
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:steven="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.lessontest.ui.MyButton android:layout_width="wrap_content" android:layout_height="wrap_content" steven:MyWidth="10dp" steven:MyVisibility="invisible" steven:Mybackground="@drawable/ic_launcher" steven:MyBoolean="true" /> </RelativeLayout>
命名空间 xmlns:steven="http://schemas.android.com/apk/res-auto"
3.代码中获取属性值
public class MyButton extends View { /** 默认属性 **/ int MyWidth = 10; // int MyVisibility = 1; boolean MyBoolean = false; int Mybackground = R.drawable.ic_launcher; /** 代码创建 **/ public MyButton(Context context) { this(context, null); } /** xml创建,自定义属性 **/ public MyButton(Context context, AttributeSet attrs) { this(context, attrs, 0); } /** xml创建,自定义属性,有style **/ public MyButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // 获取配置的自定义属性------------------- TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyButtonAttrs); MyWidth = a .getDimensionPixelSize(R.styleable.MyButtonAttrs_MyWidth, -1); float dimension = a.getDimension(R.styleable.MyButtonAttrs_MyWidth, -1); System.out.println(dimension + "dimension"); MyVisibility = a.getInt(R.styleable.MyButtonAttrs_MyVisibility, 5); MyBoolean = a.getBoolean(R.styleable.MyButtonAttrs_MyBoolean, false); Mybackground = a.getResourceId(R.styleable.MyButtonAttrs_Mybackground, -1); System.out.println("" + toString()); } @Override public String toString() { return "MyButton [MyWidth=" + MyWidth + ", MyVisibility=" + MyVisibility + ", MyBoolean=" + MyBoolean + ", Mybackground=" + Mybackground + "]"; } /** * onMessure onLayout(ViewGroup) onDraw * * View onMeasure() (在这个方法里指定自己的宽高) ->onDraw() (绘制自己的内容) * * ViewGroup onMeasure() (指定自己的宽高, 所有子View的宽高)-> onLayout() (摆放所有子View) -> * onDraw() (绘制内容) * */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); } }
SmartIamgeView(loopj版)ImageView扩展:
加载图片,继承了ImageView,支持url加载图片,处理listview跳图的问题、oom等问题地址:https://github.com/loopj/android-smart-image-view
ScrollListView(偶尔看看II eoe)ListView扩展:
listView滑动的时候每个item添加动画酷炫的效果<span style="font-size:18px;">import android.content.Context; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.ListView; public class ScrollListView extends ListView implements OnScrollListener { /** * 下滑时刷新 */ private final int DOWNREFRESH = 1; /** * 上滑是刷新 */ private final int UPREFRESH = 0; /** * 初始状态下第一个 条目 */ private int startfirstItemIndex; /** * 初始状态下最后一个 条目 */ private int startlastItemIndex; /** * 滑动后的第一个条目 */ private int endfirstItemIndex; /** * 滑动后的最后一个条目 */ private int endlastItemIndex; private View view; private Animation animation; private Handler handler; private Runnable run; private Message message; public ScrollListView(Context context) { this(context, null); } public ScrollListView(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.listViewStyle); } public ScrollListView(final Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setOnScrollListener(this); handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); int result = (Integer) msg.obj; switch (result) { case DOWNREFRESH: { // 获得最后一个item view = getChildAt(getChildCount() - 1); break; } case UPREFRESH: { // 获得第一个item view = getChildAt(0); break; } default: break; } if (null != view) { // 加载动画 animation = AnimationUtils.loadAnimation(context, R.anim.select_sacle); view.startAnimation(animation); } } }; } @Override public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub startfirstItemIndex = arg1; startlastItemIndex = arg1 + arg2 - 1; // 判断向下或者向上滑动了 if ((endfirstItemIndex > startfirstItemIndex) && (endfirstItemIndex > 0)) { RunThread(UPREFRESH); } else if ((endlastItemIndex < startlastItemIndex) && (endlastItemIndex > 0)) { RunThread(DOWNREFRESH); } endfirstItemIndex = startfirstItemIndex; endlastItemIndex = startlastItemIndex; } private void RunThread(final int state) { run = new Runnable() { @Override public void run() { message = handler.obtainMessage(1, state); handler.sendMessage(message); } }; run.run(); } @Override public void onScrollStateChanged(AbsListView arg0, int arg1) { } }</span><span style="font-size: 24px;"> </span>
相关文章推荐
- 麻雀虽小五脏俱全 Dojo自定义控件应用
- WinForm自定义控件应用实例
- C++ 自定义控件的移植问题
- js实现图片加载时候逐渐出现的杂色效果
- 关于图片的预加载过程中隐藏未知的
- 百度地图自定义控件分享
- 用javascript添加控件自定义属性解析
- Android自定义控件之仿优酷菜单
- Android自定义表格控件满足人们对视觉的需求
- VC++ 自定义控件的建立及使用方法
- asp.net 自定义控件实现无刷新上传图片,立即显示缩略图,保存图片缩略图
- C#自定义控件添加右键菜单的方法
- asp.net DropDownList自定义控件,让你的分类更清晰
- 如何通过javascript操作web控件的自定义属性
- 自定义模板列在 PostBack 后消失的问题
- 【Apollo播放器】源码分析之图片加载模式
- android自定义控件实例
- android中播放gif动画之二
- Fresco学习初步
- android面试常见的handler机制 AIDL机制 高级控件UI 内存优化