1. 程式人生 > >Android 自定義日曆控制元件

Android 自定義日曆控制元件

有圖有真像:


日曆控制元件View:

/**
 * 日曆控制元件 功能:獲得點選的日期區間
 * 
 */
public class CalendarView extends View implements View.OnTouchListener {
	private final static String TAG = "anCalendar";
	private Date selectedStartDate;
	private Date selectedEndDate;
	private Date curDate; // 當前日曆顯示的月
	private Date today; // 今天的日期文字顯示紅色
	private Date downDate; // 手指按下狀態時臨時日期
	private Date showFirstDate, showLastDate; // 日曆顯示的第一個日期和最後一個日期
	private int downIndex; // 按下的格子索引
	private Calendar calendar;
	private Surface surface;
	private int[] date = new int[42]; // 日曆顯示數字
	private int curStartIndex, curEndIndex; // 當前顯示的日曆起始的索引
	//private boolean completed = false; // 為false表示只選擇了開始日期,true表示結束日期也選擇了
	//給控制元件設定監聽事件
	private OnItemClickListener onItemClickListener;
	
	public CalendarView(Context context) {
		super(context);
		init();
	}

	public CalendarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	private void init() {
		curDate = selectedStartDate = selectedEndDate = today = new Date();
		calendar = Calendar.getInstance();
		calendar.setTime(curDate);
		surface = new Surface();
		surface.density = getResources().getDisplayMetrics().density;
		setBackgroundColor(surface.bgColor);
		setOnTouchListener(this);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		surface.width = getResources().getDisplayMetrics().widthPixels;
		surface.height = (int) (getResources().getDisplayMetrics().heightPixels*2/5);
//		if (View.MeasureSpec.getMode(widthMeasureSpec) == View.MeasureSpec.EXACTLY) {
//			surface.width = View.MeasureSpec.getSize(widthMeasureSpec);
//		}
//		if (View.MeasureSpec.getMode(heightMeasureSpec) == View.MeasureSpec.EXACTLY) {
//			surface.height = View.MeasureSpec.getSize(heightMeasureSpec);
//		}
		widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(surface.width,
				View.MeasureSpec.EXACTLY);
		heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(surface.height,
				View.MeasureSpec.EXACTLY);
		setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		Log.d(TAG, "[onLayout] changed:"
				+ (changed ? "new size" : "not change") + " left:" + left
				+ " top:" + top + " right:" + right + " bottom:" + bottom);
		if (changed) {
			surface.init();
		}
		super.onLayout(changed, left, top, right, bottom);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.d(TAG, "onDraw");
		// 畫框
		canvas.drawPath(surface.boxPath, surface.borderPaint);
		// 年月
		//String monthText = getYearAndmonth();
		//float textWidth = surface.monthPaint.measureText(monthText);
		//canvas.drawText(monthText, (surface.width - textWidth) / 2f,
		//		surface.monthHeight * 3 / 4f, surface.monthPaint);
		// 上一月/下一月
		//canvas.drawPath(surface.preMonthBtnPath, surface.monthChangeBtnPaint);
		//canvas.drawPath(surface.nextMonthBtnPath, surface.monthChangeBtnPaint);
		// 星期
		float weekTextY = surface.monthHeight + surface.weekHeight * 3 / 4f;
		// 星期背景
//		surface.cellBgPaint.setColor(surface.textColor);
//		canvas.drawRect(surface.weekHeight, surface.width, surface.weekHeight, surface.width, surface.cellBgPaint);
		for (int i = 0; i < surface.weekText.length; i++) {
			float weekTextX = i
					* surface.cellWidth
					+ (surface.cellWidth - surface.weekPaint
							.measureText(surface.weekText[i])) / 2f;
			canvas.drawText(surface.weekText[i], weekTextX, weekTextY,
					surface.weekPaint);
		}
		
		// 計算日期
		calculateDate();
		// 按下狀態,選擇狀態背景色
		drawDownOrSelectedBg(canvas);
		// write date number
		// today index
		int todayIndex = -1;
		calendar.setTime(curDate);
		String curYearAndMonth = calendar.get(Calendar.YEAR) + ""
				+ calendar.get(Calendar.MONTH);
		calendar.setTime(today);
		String todayYearAndMonth = calendar.get(Calendar.YEAR) + ""
				+ calendar.get(Calendar.MONTH);
		if (curYearAndMonth.equals(todayYearAndMonth)) {
			int todayNumber = calendar.get(Calendar.DAY_OF_MONTH);
			todayIndex = curStartIndex + todayNumber - 1;
		}
		for (int i = 0; i < 42; i++) {
			int color = surface.textColor;
			if (isLastMonth(i)) {
				color = surface.borderColor;
			} else if (isNextMonth(i)) {
				color = surface.borderColor;
			}
			if (todayIndex != -1 && i == todayIndex) {
				color = surface.todayNumberColor;
			}
			drawCellText(canvas, i, date[i] + "", color);
		}
		super.onDraw(canvas);
	}

	private void calculateDate() {
		calendar.setTime(curDate);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		int dayInWeek = calendar.get(Calendar.DAY_OF_WEEK);
		Log.d(TAG, "day in week:" + dayInWeek);
		int monthStart = dayInWeek;
		if (monthStart == 1) {
			monthStart = 8;
		}
		monthStart -= 1;  //以日為開頭-1,以星期一為開頭-2
		curStartIndex = monthStart;
		date[monthStart] = 1;
		// last month
		if (monthStart > 0) {
			calendar.set(Calendar.DAY_OF_MONTH, 0);
			int dayInmonth = calendar.get(Calendar.DAY_OF_MONTH);
			for (int i = monthStart - 1; i >= 0; i--) {
				date[i] = dayInmonth;
				dayInmonth--;
			}
			calendar.set(Calendar.DAY_OF_MONTH, date[0]);
		}
		showFirstDate = calendar.getTime();
		// this month
		calendar.setTime(curDate);
		calendar.add(Calendar.MONTH, 1);
		calendar.set(Calendar.DAY_OF_MONTH, 0);
		// Log.d(TAG, "m:" + calendar.get(Calendar.MONTH) + " d:" +
		// calendar.get(Calendar.DAY_OF_MONTH));
		int monthDay = calendar.get(Calendar.DAY_OF_MONTH);
		for (int i = 1; i < monthDay; i++) {
			date[monthStart + i] = i + 1;
		}
		curEndIndex = monthStart + monthDay;
		// next month
		for (int i = monthStart + monthDay; i < 42; i++) {
			date[i] = i - (monthStart + monthDay) + 1;
		}
		if (curEndIndex < 42) {
			// 顯示了下一月的
			calendar.add(Calendar.DAY_OF_MONTH, 1);
		}
		calendar.set(Calendar.DAY_OF_MONTH, date[41]);
		showLastDate = calendar.getTime();
	}

	/**
	 * 
	 * @param canvas
	 * @param index
	 * @param text
	 */
	private void drawCellText(Canvas canvas, int index, String text, int color) {
		int x = getXByIndex(index);
		int y = getYByIndex(index);
		surface.datePaint.setColor(color);
		float cellY = surface.monthHeight + surface.weekHeight + (y - 1)
				* surface.cellHeight + surface.cellHeight * 3 / 4f;
		float cellX = (surface.cellWidth * (x - 1))
				+ (surface.cellWidth - surface.datePaint.measureText(text))
				/ 2f;
		canvas.drawText(text, cellX, cellY, surface.datePaint);
	}

	/**
	 * 
	 * @param canvas
	 * @param index
	 * @param color
	 */
	private void drawCellBg(Canvas canvas, int index, int color) {
		int x = getXByIndex(index);
		int y = getYByIndex(index);
		surface.cellBgPaint.setColor(color);
		float left = surface.cellWidth * (x - 1) + surface.borderWidth;
		float top = surface.monthHeight + surface.weekHeight + (y - 1)
				* surface.cellHeight + surface.borderWidth;
		canvas.drawRect(left, top, left + surface.cellWidth
				- surface.borderWidth, top + surface.cellHeight
				- surface.borderWidth, surface.cellBgPaint);
	}

	private void drawDownOrSelectedBg(Canvas canvas) {
		// down and not up
		if (downDate != null) {
			drawCellBg(canvas, downIndex, surface.cellDownColor);
		}
		// selected bg color
		if (!selectedEndDate.before(showFirstDate)
				&& !selectedStartDate.after(showLastDate)) {
			int[] section = new int[] { -1, -1 };
			calendar.setTime(curDate);
			calendar.add(Calendar.MONTH, -1);
			findSelectedIndex(0, curStartIndex, calendar, section);
			if (section[1] == -1) {
				calendar.setTime(curDate);
				findSelectedIndex(curStartIndex, curEndIndex, calendar, section);
			}
			if (section[1] == -1) {
				calendar.setTime(curDate);
				calendar.add(Calendar.MONTH, 1);
				findSelectedIndex(curEndIndex, 42, calendar, section);
			}
			if (section[0] == -1) {
				section[0] = 0;
			}
			if (section[1] == -1) {
				section[1] = 41;
			}
			for (int i = section[0]; i <= section[1]; i++) {
				drawCellBg(canvas, i, surface.cellSelectedColor);
			}
		}
	}

	private void findSelectedIndex(int startIndex, int endIndex,
			Calendar calendar, int[] section) {
		for (int i = startIndex; i < endIndex; i++) {
			calendar.set(Calendar.DAY_OF_MONTH, date[i]);
			Date temp = calendar.getTime();
			// Log.d(TAG, "temp:" + temp.toLocaleString());
			if (temp.compareTo(selectedStartDate) == 0) {
				section[0] = i;
			}
			if (temp.compareTo(selectedEndDate) == 0) {
				section[1] = i;
				return;
			}
		}
	}

	public Date getSelectedStartDate() {
		return selectedStartDate;
	}

	public Date getSelectedEndDate() {
		return selectedEndDate;
	}

	private boolean isLastMonth(int i) {
		if (i < curStartIndex) {
			return true;
		}
		return false;
	}

	private boolean isNextMonth(int i) {
		if (i >= curEndIndex) {
			return true;
		}
		return false;
	}

	private int getXByIndex(int i) {
		return i % 7 + 1; // 1 2 3 4 5 6 7
	}

	private int getYByIndex(int i) {
		return i / 7 + 1; // 1 2 3 4 5 6
	}

	// 獲得當前應該顯示的年月
	public String getYearAndmonth() {
		calendar.setTime(curDate);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		return year + "-" + surface.monthText[month];
	}
	
	//上一月
	public String clickLeftMonth(){
		calendar.setTime(curDate);
		calendar.add(Calendar.MONTH, -1);
		curDate = calendar.getTime();
		invalidate();
		return getYearAndmonth();
	}
	//下一月
	public String clickRightMonth(){
		calendar.setTime(curDate);
		calendar.add(Calendar.MONTH, 1);
		curDate = calendar.getTime();
		invalidate();
		return getYearAndmonth();
	}

	private void setSelectedDateByCoor(float x, float y) {
		// change month
//		if (y < surface.monthHeight) {
//			// pre month
//			if (x < surface.monthChangeWidth) {
//				calendar.setTime(curDate);
//				calendar.add(Calendar.MONTH, -1);
//				curDate = calendar.getTime();
//			}
//			// next month
//			else if (x > surface.width - surface.monthChangeWidth) {
//				calendar.setTime(curDate);
//				calendar.add(Calendar.MONTH, 1);
//				curDate = calendar.getTime();
//			}
//		}
		// cell click down
		if (y > surface.monthHeight + surface.weekHeight) {
			int m = (int) (Math.floor(x / surface.cellWidth) + 1);
			int n = (int) (Math
					.floor((y - (surface.monthHeight + surface.weekHeight))
							/ Float.valueOf(surface.cellHeight)) + 1);
			downIndex = (n - 1) * 7 + m - 1;
			Log.d(TAG, "downIndex:" + downIndex);
			calendar.setTime(curDate);
			if (isLastMonth(downIndex)) {
				calendar.add(Calendar.MONTH, -1);
			} else if (isNextMonth(downIndex)) {
				calendar.add(Calendar.MONTH, 1);
			}
			calendar.set(Calendar.DAY_OF_MONTH, date[downIndex]);
			downDate = calendar.getTime();
		}
		invalidate();
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			setSelectedDateByCoor(event.getX(), event.getY());
			break;
		case MotionEvent.ACTION_UP:
			if (downDate != null) {
//				if (!completed) {
//					if (downDate.before(selectedStartDate)) {
//						selectedEndDate = selectedStartDate;
//						selectedStartDate = downDate;
//					} else {
//						selectedEndDate = downDate;
//					}
//					completed = true;
//				} else {
//					selectedStartDate = selectedEndDate = downDate;
//					completed = false;
//				}
				selectedStartDate = selectedEndDate = downDate;
				//響應監聽事件
				onItemClickListener.OnItemClick(selectedStartDate);
				// Log.d(TAG, "downdate:" + downDate.toLocaleString());
				//Log.d(TAG, "start:" + selectedStartDate.toLocaleString());
				//Log.d(TAG, "end:" + selectedEndDate.toLocaleString());
				downDate = null;
				invalidate();
			}
			break;
		}
		return true;
	}
	
	//給控制元件設定監聽事件
	public void setOnItemClickListener(OnItemClickListener onItemClickListener){
		this.onItemClickListener =  onItemClickListener;
	}
	//監聽介面
	public interface OnItemClickListener {
		void OnItemClick(Date date);
	}

	/**
	 * 
	 * 1. 佈局尺寸 2. 文字顏色,大小 3. 當前日期的顏色,選擇的日期顏色
	 */
	private class Surface {
		public float density;
		public int width; // 整個控制元件的寬度
		public int height; // 整個控制元件的高度
		public float monthHeight; // 顯示月的高度
		//public float monthChangeWidth; // 上一月、下一月按鈕寬度
		public float weekHeight; // 顯示星期的高度
		public float cellWidth; // 日期方框寬度
		public float cellHeight; // 日期方框高度	
		public float borderWidth;
		public int bgColor = Color.parseColor("#FFFFFF");
		private int textColor = Color.BLACK;
		//private int textColorUnimportant = Color.parseColor("#666666");
		private int btnColor = Color.parseColor("#666666");
		private int borderColor = Color.parseColor("#CCCCCC");
		public int todayNumberColor = Color.RED;
		public int cellDownColor = Color.parseColor("#CCFFFF");
		public int cellSelectedColor = Color.parseColor("#99CCFF");
		public Paint borderPaint;
		public Paint monthPaint;
		public Paint weekPaint;
		public Paint datePaint;
		public Paint monthChangeBtnPaint;
		public Paint cellBgPaint;
		public Path boxPath; // 邊框路徑
		//public Path preMonthBtnPath; // 上一月按鈕三角形
		//public Path nextMonthBtnPath; // 下一月按鈕三角形
		public String[] weekText = { "Sun","Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
		public String[] monthText = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
		   
		public void init() {
			float temp = height / 7f;
			monthHeight = 0;//(float) ((temp + temp * 0.3f) * 0.6);
			//monthChangeWidth = monthHeight * 1.5f;
			weekHeight = (float) ((temp + temp * 0.3f) * 0.7);
			cellHeight = (height - monthHeight - weekHeight) / 6f;
			cellWidth = width / 7f;
			borderPaint = new Paint();
			borderPaint.setColor(borderColor);
			borderPaint.setStyle(Paint.Style.STROKE);
			borderWidth = (float) (0.5 * density);
			// Log.d(TAG, "borderwidth:" + borderWidth);
			borderWidth = borderWidth < 1 ? 1 : borderWidth;
			borderPaint.setStrokeWidth(borderWidth);
			monthPaint = new Paint();
			monthPaint.setColor(textColor);
			monthPaint.setAntiAlias(true);
			float textSize = cellHeight * 0.4f;
			Log.d(TAG, "text size:" + textSize);
			monthPaint.setTextSize(textSize);
			monthPaint.setTypeface(Typeface.DEFAULT_BOLD);
			weekPaint = new Paint();
			weekPaint.setColor(textColor);
			weekPaint.setAntiAlias(true);
			float weekTextSize = weekHeight * 0.6f;
			weekPaint.setTextSize(weekTextSize);
			weekPaint.setTypeface(Typeface.DEFAULT_BOLD);
			datePaint = new Paint();
			datePaint.setColor(textColor);
			datePaint.setAntiAlias(true);
			float cellTextSize = cellHeight * 0.5f;
			datePaint.setTextSize(cellTextSize);
			datePaint.setTypeface(Typeface.DEFAULT_BOLD);
			boxPath = new Path();
			//boxPath.addRect(0, 0, width, height, Direction.CW);
			//boxPath.moveTo(0, monthHeight);
			boxPath.rLineTo(width, 0);
			boxPath.moveTo(0, monthHeight + weekHeight);
			boxPath.rLineTo(width, 0);
			for (int i = 1; i < 6; i++) {
				boxPath.moveTo(0, monthHeight + weekHeight + i * cellHeight);
				boxPath.rLineTo(width, 0);
				boxPath.moveTo(i * cellWidth, monthHeight);
				boxPath.rLineTo(0, height - monthHeight);
			}
			boxPath.moveTo(6 * cellWidth, monthHeight);
			boxPath.rLineTo(0, height - monthHeight);
			//preMonthBtnPath = new Path();
			//int btnHeight = (int) (monthHeight * 0.6f);
			//preMonthBtnPath.moveTo(monthChangeWidth / 2f, monthHeight / 2f);
			//preMonthBtnPath.rLineTo(btnHeight / 2f, -btnHeight / 2f);
			//preMonthBtnPath.rLineTo(0, btnHeight);
			//preMonthBtnPath.close();
			//nextMonthBtnPath = new Path();
			//nextMonthBtnPath.moveTo(width - monthChangeWidth / 2f,
			//		monthHeight / 2f);
			//nextMonthBtnPath.rLineTo(-btnHeight / 2f, -btnHeight / 2f);
			//nextMonthBtnPath.rLineTo(0, btnHeight);
			//nextMonthBtnPath.close();
			monthChangeBtnPaint = new Paint();
			monthChangeBtnPaint.setAntiAlias(true);
			monthChangeBtnPaint.setStyle(Paint.Style.FILL_AND_STROKE);
			monthChangeBtnPaint.setColor(btnColor);
			cellBgPaint = new Paint();
			cellBgPaint.setAntiAlias(true);
			cellBgPaint.setStyle(Paint.Style.FILL);
			cellBgPaint.setColor(cellSelectedColor);
		}
	}
}

實現日曆控制元件:
        <RelativeLayout
            android:id="@+id/layout_calendar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:visibility="visible" >

            <TextView
                android:id="@+id/calendarCenter"
                style="@style/main_bar_text_style"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_margin="8dp" />

            <ImageButton
                android:id="@+id/calendarLeft"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:padding="8dp"
                android:contentDescription="@null"
                android:src="@drawable/calendar_month_left" 
                android:background="@null"/>

            <ImageButton
                android:id="@+id/calendarRight"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:padding="8dp"
                android:contentDescription="@null"
                android:src="@drawable/calendar_month_right" 
                android:background="@null"/>

            <com.techrare.view.CalendarView
                android:id="@+id/calendar"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/calendarCenter" />
        </RelativeLayout>
     <style name="main_bar_text_style">
         <item name="android:textColor">@color/white</item>
         <item name="android:textStyle">bold</item>
         <item name="android:textSize">18sp</item>
     </style>



上一月圖片:

呼叫 calendar.clickLeftMonth(); 
     

下一月:


呼叫 calendar.clickRightMonth(); 

日曆控制元件中的一些功能(可以自己加):
//獲取日曆控制元件物件
calendar = (CalendarView)findViewById(R.id.calendar);
//獲取日曆中年月 ya[0]為年,ya[1]為月(格式大家可以自行在日曆控制元件中改)
String[] ya = calendar.getYearAndmonth().split("-"); 
//點選上一月 同樣返回年月 
String leftYearAndmonth = calendar.clickLeftMonth(); 
String[] lya = leftYearAndmonth.split("-");
//點選下一月
String rightYearAndmonth = calendar.clickRightMonth(); 
String[] rya = rightYearAndmonth.split("-");
//設定控制元件監聽,可以監聽到點選的每一天(大家也可以在控制元件中自行設定)
calendar.setOnItemClickListener(new calendarItemClickListener());
class calendarItemClickListener implements OnItemClickListener{
	@Override
	public void OnItemClick(Date date) {
		Toast.makeText(getApplicationContext(), date+"", Toast.LENGTH_SHORT).show();
	}
 }

這些功能只是配合專案中的需要來新增的,大家如有其他需求,可以自行在控制元件中新增,應該也不怎麼困難!

相關推薦

Android 定義日曆控制元件

有圖有真像: 日曆控制元件View: /** * 日曆控制元件 功能:獲得點選的日期區間 * */ public class CalendarView extends View implements View.OnTouchListener { priv

Android UI-定義日曆控制元件

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

Android帶日程安排的定義日曆控制元件

前言:       本文實現的是,自定義的日曆控制元件,可以新增日程安排。       本篇是基於網上原始碼做的相應修改,本文只列出修改的內容,需要看原始碼博文的請到: 一、效果展示    二、日曆控制元件的修改      1、星期顯示中文           此處

android定義開關控制元件-SlideSwitch

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

android-定義組合控制元件(EditText+選項)

一.前言 在開發中,或許一個業務需求中會出現很多系統控制元件組合成的佈局,並且經常需要複用。比如在一個表單中,裡面有個編輯框EditText右側還需要有個選項卡按鈕,需要有編輯框的輸入功能也需要有右側選項卡的點選事件,同時這兩個控制元件也存在一定關聯,且在一個介

Android定義複合控制元件

       在Android中,複合控制元件是非常常見的,下面以建立一個標題欄為例,講解建立自定義複合控制元件的過程。        以下圖為例:我們要建立一個標題欄,這個標題欄是由左邊的Button、右邊的Button以及中間的TextView複合而成的,而我們希望能夠

android定義view控制元件之一圓角背景TextView

繼昨天寫了一個TextView可以包括兩種不同的風格字型,而保證可以換行的情況下的自定義View。今天的正文還是寫一個自定義的TextView。 一慣風格首先亮出實現效果,這最是直接不過的了。看下圖: 其實不通過寫一個自定義view的方式也可以實現這個效果,但是就需你在你

android 定義組合控制元件 頂部導航欄

    在軟體開發過程中,經常見到,就是APP 的標題欄樣式幾乎都是一樣的,只是文字不同而已,兩邊圖示不同。為了減少重複程式碼,提高效率, 方便大家使用,我們把標題欄通過組合的方式定義成一個控制元件。 例下圖:                                

Android 定義RatingBar控制元件,顯示不全問題

最近專案要用到 自定義RatingBar控制元件 但是自定義好了樣式發現,星星只顯示一半,於是在網上找各種解決方法。 最後竟然是直接把資源圖片,移動到較高解析度的資料夾裡面。 我的解決做法如下,如果有更好的方法,請留言告知。 一,先寫一個drawable,設定好backg

Android定義倒計時控制元件

序: 最近越來越多的APP都是用手機號註冊,一是為了方便使用者記憶,二是為了增加使用者賬戶的安全性。在我們進行交易操作或者修改密碼等操作的時候,這時候需要輸入簡訊驗證碼。這個控制元件會需要有倒計時的功能,這裡主要總結常見的幾種實現方式。 1.Android中實現倒計時

Android定義電池控制元件

一、背景最近公司有個業務,需要自定義一個電池控制元件,因此自己按照UI圖編寫了一個自定義View。二、效果三、實現過程首先看下視覺給出的UI效果圖 從這裡我們可以看得出來,要自定義這個電池View的話,分為3部分來繪製。第一部分是電池頭第二部分是電池邊框第三部分是電池電量3.

Android定義組合控制元件之實現CheckBox變化

前言:自定義組合控制元件最大的好處就是複用性,可以為我們節省不少程式碼量,但是一般我們自定義組合控制元件的時候,封裝出來的控制元件不管是自己用還是別人用也好,封裝的程式碼最好是易讀性強,便於修改,沒有必要封裝太多的屬性,一般控制在兩三個屬性為最佳,畢竟我們不是Google.

定義日曆控制元件Calendar樣式

日曆樣式,圖放上來了,之後補充下講解吧預設樣式顯示是“標題年月+顯示日”,圖1右箭頭是滑鼠附上去時的顏色。點選頂上的標題,“標題年月+顯示日”切換為“標題年+顯示月”;“標題該年+顯示月”切換為“標題年範圍+顯示年”。預設沒有選中當日,這個可以在樣式裡設定回來,有註釋應該可以

【我的Android進階之旅】Android定義電池控制元件

一、背景 最近公司有個業務,需要自定義一個電池控制元件,因此自己按照UI圖編寫了一個自定義View。 二、效果 三、實現過程 首先看下視覺給出的UI效果圖 從這裡我們可以看得出來,要自定義這個電池View的話,分為3部分來

android 定義倒計時控制元件(圓形倒計時顯示)

先上效果圖 - 倒計時結束 程式碼塊 attr.xml 控制元件需要用到的屬性: <?xml version="1.0" encoding="utf-8"?> <resources> <de

手擼一個定義日曆控制元件

引言 日曆控制元件在android開發中也是比較常見的一個控制元件,並且目前大部分開源的日曆控制元件也已經做得很漂亮,很完善了,功能也相當豐富; 今天這個日曆控制元件就是我在別人的基礎上進行修改了的,首先很感謝這個開源庫(https://github.com/c

android 定義組合控制元件

1.要實現這個效果,用多個控制元件組合起來,要用到自定義控制元件 程式碼實現 1.自己寫一個佈局檔案 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android

Android定義時鐘控制元件

專案要求訪問網路是等待狀態要做時鐘的樣子,經過不懈努力,終於寫出來了,現分享出來,功能比較全,直接拷貝程式碼就可以用,僅供有這種需求的碼農們參考,如果採納,請點個贊,謝謝支援。 效果圖 主Activity,主要是在訪問介面的時候開啟時鐘,介面訪問結束關閉時鐘。 pa

android定義組合控制元件圖片輪播+小圓點+點選跳轉廣告頁面

1.寫一個佈局,用於自定義組合控制元件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and

android開發:定義組合控制元件

內容介紹 本文記錄,自定義組合控制元件,為了可以程式碼複用,減少程式碼量 配置控制元件屬性檔案 開啟res/values/目錄下的arss.xml檔案,新增下面屬性程式碼,如果沒有建立arrs.xml檔案。 <?xml version="1.0" enc