每日一记—获取Bing每日一图实现Android欢迎页(一)

日期:2018.9.11


最近在做的这个项目里使用了欢迎页,只显示一张图片太过於单调,没有服务器(编程小白,没钱买服务器)也不能调用服务器的资源,于是想到是不是可以获取其他网站的图片,便想到Bing每日一图的图片能不能获取到手机上作为欢迎界面,网上查了一下,果然有很多教程,于是自己也做了一下,不废话,直接上代码。


第一部分:欢迎页代码

新建一个SplashActivity并自动生成一个activity_splash.xml布局文件,布局文件代码如下:一个ImageView用于显示背景图片,注意这里的scaleType属性,设置为centerCrop,之前在《每日一记—ImageView的scaleType属性和获取当前手机日期、时间的方法》已经介绍过,这个属性值的意思是按比例扩大图片的size居中显示,使得图片长 (宽)等于或大于View的长(宽)(图片会发生正常的裁剪);一个TextView用于显示软件版本号

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/iv_background1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="#ff00aaff"/>
    <TextView
        android:id="@+id/tv_version"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:text="V1.0"/>
</RelativeLayout>

SplashActivity.java中的代码如下:代码中AnalysisUtils.isFolderExists用于判断文件夹是否存在,如果不存在就直接创建一个,AnalysisUtils.fileIsExists用于判断文件是否存在,这里如果不存在的话,跳转到获取being每日一图的代码,cutPictureUtils.decodeUriAsBitmap用于将图片的URL转换为bitmap;接下来就是获取程序包信息;最后就是运用Timer和TimerTask配合实现眼石3秒的作用,延时结束后直接跳转到登录界面,另外在跳转时我加入了一个自动登录判断,将登录状态isLogin保存在了SharedPreferences中。

    private TextView tv_version;
    private ImageView iv_background;
    private ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        //设置为此界面为竖屏(因为是欢迎界面)
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();
    }

 private void init(){
        tv_version= findViewById(R.id.tv_version);
        iv_background=findViewById(R.id.iv_background1);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        // 如果想加入时分秒,pattern设为"yyyy年MM月dd日 HH:mm:ss"
        Date date = new Date(System.currentTimeMillis());
        String fileName = simpleDateFormat.format(date) + ".jpg";
        Uri imgUri= Uri.parse("file:///sdcard/crazystudy/"+ fileName);
        if(AnalysisUtils.isFolderExists("/sdcard/crazystudy/")){
            if (AnalysisUtils.fileIsExists(imgUri.getPath())){
                // 显示出来
                CutPictureUtils cutPictureUtils=new CutPictureUtils(SplashActivity.this,
                        "");
                iv_background.setImageBitmap(cutPictureUtils.decodeUriAsBitmap(imgUri));
            }else {
                //获取bing背景图片
            }
        }
        try{
            //获取程序包信息
            PackageInfo info=getPackageManager().getPackageInfo(getPackageName(),0);
            tv_version.setText("V"+info.versionName);
        }catch (PackageManager.NameNotFoundException e){
            tv_version.setText("V");
        }
        //让此界面延迟3秒后再跳转,timer中有一个线程,这个线程不断执行task
        Timer timer = new Timer();
        //TimerTask类表示一个在指定时间内执行的task
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                //打开后判断是否是登录状态,如果是,则自动登录
                SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
                boolean isLogin=sp.getBoolean("isLogin",false);
                Intent intent;
                if (isLogin){
                    Intent data = new Intent();
                    data.putExtra("isLogin",true);
                    setResult(RESULT_OK,data);
                    intent=new Intent(SplashActivity.this,MainActivity.class);
                }else {
                    intent=new Intent(SplashActivity.this,LoginActivity.class);
                }
                startActivity(intent);
                SplashActivity.this.finish();
            }
        };
        timer.schedule(task,3000);      //设置这个task在延迟2秒之后自动执行
    }

使用到的两个Utils文件代码如下:

AnalysisUtils.java

/**
     * 判断文件夹是否存在,不存在则创建这个文件夹
     * @param strFolder
     * @return
     */
    public static boolean isFolderExists(String strFolder) {
        File file = new File(strFolder);
        if (!file.exists()) {
            if (file.mkdir()) {
                return true;
            } else
                return false;
        }
        return true;
    }

    /**
     * 判断文件是否存在(可以是文件夹或者是文件)
     * @param strFile
     * @return
     */
    public static boolean fileIsExists(String strFile)
    {
        try
        {
            File f=new File(strFile);
            if(!f.exists())
            {
                return false;
            }
        }
        catch (Exception e)
        {
            return false;
        }

        return true;
    }

cutPictureUtils.java

/**
     * 通过URL获得图片
     * @param uri
     * @return 返回图片
     */
    public Bitmap decodeUriAsBitmap(Uri uri){
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(activity.getContentResolver().openInputStream(uri));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        return bitmap;
    }

 

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