欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android 每日一句(英语)

程序员文章站 2022-07-12 14:12:38
...

前言:添加了学习模块,所以做了一个每日一句的功能,主要是在天行数据申请了一个每日一句的api,之后用okgo,调取,并解析成对象。获取文字、图片、音频等。在线显示、播放。同时做了一个底部弹出日历选择。

效果图:

Android 每日一句(英语)Android 每日一句(英语)

/**网络请求封装**/
implementation 'com.lzy.net:okgo:3.0.4'
/**日历选择器**/
implementation 'com.prolificinteractive:material-calendarview:1.4.3'

Json 格式:

{
    "code": 200,
    "msg": "success",
    "newslist": [
        {
            "id": 3725,
            "content": "Passion is a key ingredient to the study and practice of law and of life.",
            "note": "激情是学习和实践法律不可或缺的要素,同时也是生命中不可或缺的要素。",
            "tts": "https://edu-wps.ks3-cn-beijing.ksyun.com/audio/17c662155203497977f9536ac2bac6e1.mp3",
            "imgurl": "https://edu-wps.ks3-cn-beijing.ksyun.com/image/88a1b7e9b02be6388329a4be911f7293.png",
            "date": "2020-04-06"
        }
    ]
}

 网络获取:

public class HttpUtils {
    public static final String TAG = "HttpUtils";

    public static void getEnglishSentence(Context context, String date , StringCallback callback) throws IOException {
        OkGo.<String>get(UrlsConfig.EVERYDAY_ENGLISH_API)
                .tag(context)
                .params("key", ApiConfig.API_AppKey)
                .params("date", date)
                .execute(callback);
    }
}

EverydayEnglishFragment.java

public class EverydayEnglishFragment extends BaseFragment {
    public static final String TAG = "EverydayEnglishFragment";
    @BindView(R.id.tv_year)
    TextView tv_year;
    @BindView(R.id.tv_month)
    TextView tv_month;
    @BindView(R.id.tv_day)
    TextView tv_day;
    @BindView(R.id.calendar_left)
    ImageView calendar_left;
    @BindView(R.id.calendar_right)
    ImageView calendar_right;
    @BindView(R.id.img_english)
    SimpleDraweeView imgEnglish;


    private Calendar calendar;
    EnglishSentence.NewslistBean mNewslistBean;


    public static Fragment getInstance() {
        Fragment fragment = new EverydayEnglishFragment();
        return fragment;
    }


    @Override
    protected int contentViewID() {
        return R.layout.fragment_everyday_english;
    }

    @Override
    protected void initialize() {
        EventBus.getDefault().register(this);
        calendar = Calendar.getInstance();
        setTextDate(calendar);
        getHttpData(calendar);
        return;
    }

    private void setTextDate(Calendar calendar) {
        tv_year.setText(calendar.get(Calendar.YEAR) + "");
        tv_month.setText(calendar.get(Calendar.MONTH) + 1 + "");
        tv_day.setText(calendar.get(Calendar.DAY_OF_MONTH) + "");
        changeArrowImage(calendar);
    }


    private void getHttpData(Calendar calendar) {
        String date = FormatUtils.getDateTimeString(calendar.getTime(), FormatUtils.template_Date);
        try {
            StringCallback callback = new StringCallback() {
                @Override
                public void onSuccess(Response<String> response) {
                    try {
                        String json = response.body().toString();
                        JSONObject jsonObject = new JSONObject(json);
                        int code = jsonObject.getInt("code");
                        if (code == 200) {
                            JSONArray jsonArray = jsonObject.getJSONArray("newslist");
                            List<EnglishSentence.NewslistBean> newslistBeanList = new Gson().fromJson(jsonArray.toString(),
                                    new TypeToken<List<EnglishSentence.NewslistBean>>() {
                                    }.getType());
                            LogUtils.d(TAG, newslistBeanList.get(0).toString());
                            mNewslistBean = newslistBeanList.get(0);
                            setTextImg(mNewslistBean);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onError(Response<String> response) {
                    super.onError(response);
                }
            };
            HttpUtils.getEnglishSentence(getContext(), date, callback);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void changeArrowImage(Calendar calendar){
        if (DateUtils.isTody(calendar)){
            calendar_left.setImageResource(R.mipmap.arrow_left);
            calendar_right.setImageResource(R.mipmap.right_gay);
        } else {
            calendar_left.setImageResource(R.mipmap.arrow_left);
            calendar_right.setImageResource(R.mipmap.arrow_right);
        }
    }

    private void setTextImg(EnglishSentence.NewslistBean newslistBean) {
        if (newslistBean.getImgurl() != null){
            imgEnglish.setImageURI(newslistBean.getImgurl());
        }

    }

    @OnClick({R.id.english_mediaPlayer, R.id.calendar_left, R.id.calendar_right, R.id.calender_select})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.english_mediaPlayer:
                if (mNewslistBean == null){
                    return;
                }
                onPlayer(mNewslistBean);
                break;
            case R.id.calendar_left:
                calendar.add(Calendar.DATE, -1);
                setTextDate(calendar);
                getHttpData(calendar);
                break;
            case R.id.calendar_right:
                if (DateUtils.isTody(calendar)){
                    return;
                }
                calendar.add(Calendar.DATE, 1);
                setTextDate(calendar);
                getHttpData(calendar);
                break;
            case R.id.calender_select:
                startActivity(CalenderSelectActivity.class);
                getActivity().overridePendingTransition(R.anim.bottom_in,R.anim.bottom_silent);
                break;
            default:
        }
    }

    private void onPlayer(EnglishSentence.NewslistBean newslistBean) {
        if (newslistBean.getTts() != null) {
            try {
                MediaPlayer mediaPlayer = new MediaPlayer();
                mediaPlayer.setDataSource(newslistBean.getTts());
                mediaPlayer.prepare();
                mediaPlayer.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(CalenderEvent calenderEvent) {
        switch (calenderEvent.getMessage()) {
            case ConstantConfig.CALENDER_SELECT:
                calendar = calenderEvent.getCalendar();
                setTextDate(calendar);
                getHttpData(calendar);
                break;
            default:
        }

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        OkGo.getInstance().cancelAll();
    }
}

日历选择

public class CalenderSelectActivity extends BaseActivity implements OnDateSelectedListener {
    @BindView(R.id.materialCalendarView)
    MaterialCalendarView widget;

    @Override
    protected int contentViewID() {
        return R.layout.activity_calender_select;
    }

    @Override
    protected void initialize() {
        StatusBarUtils.setTransparent(this);
        getSupportActionBar().hide();
        setTopTitle("",false);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        widget.setSelectedDate(CalendarDay.today());
        widget.state().edit().setMaximumDate(CalendarDay.today()).commit();
        widget.setOnDateChangedListener(this);
    }

    @Override
    public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
        EventBus.getDefault().post(new CalenderEvent(ConstantConfig.CALENDER_SELECT,date.getCalendar()));
        finish();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.bottom_silent,R.anim.bottom_out);
    }
}

弹入 弹出 动画XML bottom_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="25"
        android:toYDelta="100%p"
        android:duration="1000"
        />
</set>

bottom_silent.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="0"
        android:toYDelta="0"
        android:duration="1800"
    />
</set>

 

相关标签: Android