自定义控件

来源:互联网 发布:淘宝论文可靠吗 编辑:程序博客网 时间:2024/05/21 07:58

自定义属性

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());}@Overridepublic 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() (绘制内容) *  */@Overrideprotected 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() {@Overridepublic void handleMessage(Message msg){super.handleMessage(msg);int result = (Integer) msg.obj;switch (result){case DOWNREFRESH:{// 获得最后一个itemview = getChildAt(getChildCount() - 1);break;}case UPREFRESH:{// 获得第一个itemview = getChildAt(0);break;}default:break;}if (null != view){// 加载动画animation = AnimationUtils.loadAnimation(context,R.anim.select_sacle);view.startAnimation(animation);}}};}@Overridepublic void onScroll(AbsListView arg0, int arg1, int arg2, int arg3){// TODO Auto-generated method stubstartfirstItemIndex = 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() {@Overridepublic void run(){message = handler.obtainMessage(1, state);handler.sendMessage(message);}};run.run();}@Overridepublic void onScrollStateChanged(AbsListView arg0, int arg1){}}</span><span style="font-size: 24px;"></span>


0 0
原创粉丝点击