更改Calendar背景图(使用系统映像选择器)

最近提出的申请,主接口是一个历,网上有很多第三方的日历控件,有使用ImageView实现,有使用GridView实现,它的优点是控制的灵活性.

而我选择使用本机CalendarView,关于这个控件,详细能够參考:http://android.toolib.net/reference/android/widget/CalendarView.html

缺点是没有提供周分隔线的自己定义图片的方法,以下的代码是android画周分隔线的代码,最初我想要重写这种方法来自己定义图片.后来我发现这个

方法是定义在CalendarView的私有内部类WeekView中的...有兴趣的同学能够研究一下怎么实现,顺便请@我 ^ ^

        /**
         * Draws a horizontal line for separating the weeks.
         *
         * @param canvas The canvas to draw on.
         */
        private void drawWeekSeparators(Canvas canvas) {
            // If it is the topmost fully visible child do not draw separator line
            int firstFullyVisiblePosition = mListView.getFirstVisiblePosition();
            if (mListView.getChildAt(0).getTop() < 0) {
                firstFullyVisiblePosition++;
            }
            if (firstFullyVisiblePosition == mWeek) {
                return;
            }
            mDrawPaint.setColor(mWeekSeparatorLineColor);
            mDrawPaint.setStrokeWidth(mWeekSeperatorLineWidth);
            float startX;
            float stopX;
            if (isLayoutRtl()) {
                startX = 0;
                stopX = mShowWeekNumber ? mWidth - mWidth / mNumCells : mWidth;
            } else {
                startX = mShowWeekNumber ? mWidth / mNumCells : 0;
                stopX = mWidth;
            }
            canvas.drawLine(startX, 0, stopX, 0, mDrawPaint);
        }

以下我来说怎么通过选择图片来替换CalendarView的背景:

<CalendarView
	    android:id="@+id/date"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:focusedMonthDateColor="@android:color/holo_purple"
	    android:background="@android:color/background_light"
	    android:showWeekNumber="false"
	    android:selectedWeekBackgroundColor="@color/touming"
	    android:weekSeparatorLineColor="@color/touming"
	    android:dateTextAppearance="@android:style/TextAppearance.Large"
	    android:shownWeekCount="5"
	    android:unfocusedMonthDateColor="@color/pink"
	    android:selectedDateVerticalBar="@drawable/xxxx"
	    />
首先定义一个CalendarView,然后在你的java代码中获取它-->

mCalendarView = (CalendarView) findViewById(R.id.date);

然后你能够弄一个button或者其他的什么,绑定上它的事件,来触发我们切换到pic选择-->

Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 0);

这种方法选择完,会回调这种方法,所以你要重写这个-->

        @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==0&&resultCode==RESULT_OK&&null!=data){
			Uri selectedBGPath = data.getData();
			//store BGpath to BGSP
			BGSP sp = new BGSP(MainActivity.this);
			sp.writeOneString("bgPath", selectedBGPath.toString());
			//-----------------------------------------------------
			this.setCalendarBG(selectedBGPath);
		}
	}

当中data是你选择的图片的Uri,大概是(Content:\... 好像是这种),为了又一次打开应用时能够保存设置,我们要用SharedPreferences来保存这个路径.

下来我们要设置CalendarView的背景-->

        //设置选择的URI到背景
	@SuppressLint("NewApi")
	private void setCalendarBG(Uri uri){
		Drawable drw = ImageOperations(uri,"");
		mCalendarView.setBackground(drw);
	}

当中能够看到把uri转换成Drawable的方法,这种方法是我在网上copy的,可是在我这没有问题:

private Drawable ImageOperations(Uri url, String saveFilename) {
	InputStream is = null;
        try {
        	is = getContentResolver().openInputStream(url);
            Drawable d = Drawable.createFromStream(is, saveFilename);
            return d;
        } catch (IOException e) {
            return null;
        }finally{
        	try {
        	if(is!=null){
			is.close();
       		}
        	} catch (IOException e) {
        		e.printStackTrace();
        	}
        }
}

当然is是我关闭的...

最后在activity启动的时候,增加以下的代码,用来推断应用是否已经自己定义过背景:

        /**
	 * 初始化日历背景
	 */
	private void initCalendarBG(){
		BGSP sp = new BGSP(MainActivity.this);
		String bgpath = sp.getOneString("bgPath");
		if(!bgpath.equals(BGSP.DEFAULT_VALUE)){
			this.setCalendarBG(Uri.parse(bgpath));
		}
	}

至此,就基本成型了...

写的比較乱,假设有谁想详细了解,就在以下评论一下 ^ ^

给你们看看效果是这种:(另外我自己定义了日期选择两边的bar...)



版权声明:本文博主原创文章,博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/zfyouxi/p/4842014.html