如何按day_of_week更改背景图片

时间:2019-01-30 21:22:35

标签: android-studio

通过当前日期名称更改android studio java中背景图片的代码, 例如,如果今天是星期一,则背景图片是A图片;如果星期二,则b图片... thx

2 个答案:

答案 0 :(得分:0)

您可以检查星期几并更改图像。请尝试如下

private static void getDaysOfTheWeek() {
    Calendar cal = Calendar.getInstance(TimeZone.getDefault());
    cal.setTime(new Date());

    switch(cal.get(Calendar.DAY_OF_WEEK)){
        case Calendar.MONDAY:
            System.out.println("image 1");
            break;
        case Calendar.TUESDAY:
            System.out.println("image 2");
            break;
        case Calendar.WEDNESDAY:
            System.out.println("image 3");
            break;
        case Calendar.THURSDAY:
            System.out.println("image 4");
            break;
        case Calendar.FRIDAY:
            System.out.println("image 5");
            break;
        case Calendar.SATURDAY:
            System.out.println("image 6");
            break;
        case Calendar.SUNDAY:
            System.out.println("image 5");
            break;
    }
}

答案 1 :(得分:0)

创建一个布局文件,该文件同时显示用于更改背景的图像,并在其上显示TextView,该文本将显示Text并使用以下代码生成位图图像并将其设置为ImageView。希望这会对您有所帮助

 private Bitmap getCustomImage(@DrawableRes int imageId,String text) {

        View customView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.your_layout_file, null);
        ImageView imageView = (ImageView) customView.findViewById(R.id.imageView);
   TextView textView = (TextView) customView.findViewById(R.id.textView);
textView.setText(text);
        imageView.setImageResource(imageId);
        customView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        customView.layout(0, 0, customMarkerView.getMeasuredWidth(), customView.getMeasuredHeight());
        customView.buildDrawingCache();
        Bitmap returnedBitmap = Bitmap.createBitmap(customView.getMeasuredWidth(), customView.getMeasuredHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);
        canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
        Drawable drawable = customView.getBackground();
        if (drawable != null)
            drawable.draw(canvas);
        customView.draw(canvas);
        return returnedBitmap;
    }