自定義控件

自定義屬性

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>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章